Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

separate test cases into separate files #98

Merged
merged 2 commits into from
May 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .taprc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
check-coverage: false

files:
- test/*.js
- test/**/*.test.js
66 changes: 66 additions & 0 deletions test/communication.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
'use strict'

const { test } = require('tap')
const Fastify = require('fastify')
const fastifyKafka = require('..')
const {
generateTopicName,
generateGroupId,
getDefaultOptions
} = require('./utils')

test('communication', t => {
t.plan(7)
const options = getDefaultOptions()
const group = generateGroupId()
options.consumer['group.id'] = group

const topicName = generateTopicName()

const producerFastify = Fastify({ logger: true })
const consumerFastify = Fastify({ logger: true })

t.teardown(() => producerFastify.close())
t.teardown(() => consumerFastify.close())

consumerFastify
.register(fastifyKafka, { ...options, producer: undefined })
.after(err => {
t.error(err)

consumerFastify.kafka.consumer.on('error', t.fail)
consumerFastify.kafka.subscribe(topicName)

consumerFastify.kafka.on(topicName, (msg, commit) => {
t.equal(msg.value.toString(), 'hello world!')
commit()

t.ok(true)
})

consumerFastify.kafka.consume()
})

producerFastify
.register(fastifyKafka, { ...options, consumer: undefined })
.after(err => {
t.error(err)

producerFastify.kafka.producer.on('error', t.fail)
producerFastify.kafka.push({
topic: topicName,
payload: 'hello world!',
key: 'testKey'
})

t.ok(true)
})

producerFastify.ready(err => {
t.error(err)

consumerFastify.ready(err => {
t.error(err)
})
})
})
68 changes: 68 additions & 0 deletions test/consume-callback.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'use strict'

const { test } = require('tap')
const Fastify = require('fastify')
const fastifyKafka = require('..')
const { getDefaultOptions, generateGroupId, generateTopicName } = require('./utils')

test('consume callback', t => {
const options = getDefaultOptions()
const group = generateGroupId()
options.consumer['group.id'] = group
options.consumer.event_cb = true

const topicName = generateTopicName()

const producerFastify = Fastify()
const consumerFastify = Fastify()

t.teardown(() => producerFastify.close())
t.teardown(() => consumerFastify.close())

consumerFastify
.register(fastifyKafka, { ...options, producer: undefined })
.after(err => {
t.error(err)

consumerFastify.kafka.consumer.on('error', t.fail)
consumerFastify.kafka.subscribe(topicName)

consumerFastify.kafka.on(topicName, t.fail)

function onConsume (err, message) {
t.error(err)
t.match(message, {
topic: topicName,
value: Buffer.from('hello world!'),
key: Buffer.from('testKey')
})

t.end()
}

consumerFastify.kafka.consume(onConsume)
})

producerFastify
.register(fastifyKafka, { ...options, consumer: undefined })
.after(err => {
t.error(err)

producerFastify.kafka.producer.on('error', t.fail)
producerFastify.kafka.push({
topic: topicName,
payload: 'hello world!',
key: 'testKey'
})

t.ok(true)
})

producerFastify.ready(err => {
t.error(err)

consumerFastify.ready(err => {
t.error(err)
})
})
})
3 changes: 1 addition & 2 deletions test/consumer.js → test/consumer.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict'

const t = require('tap')
const { test } = require('tap')
const log = require('abstract-logging')
const test = t.test

const Consumer = require('../lib/consumer')

Expand Down
25 changes: 25 additions & 0 deletions test/export.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict'

const { test } = require('tap')
const fastifyKafka = require('..')
const defaultExport = require('..').default
const { fastifyKafka: namedExport } = require('..')

test('export', function (t) {
t.plan(3)

t.test('module export', function (t) {
t.plan(1)
t.equal(typeof fastifyKafka, 'function')
})

t.test('default export', function (t) {
t.plan(1)
t.equal(typeof defaultExport, 'function')
})

t.test('named export', function (t) {
t.plan(1)
t.equal(typeof namedExport, 'function')
})
})
73 changes: 73 additions & 0 deletions test/multiple-topics.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'use strict'

const { test } = require('tap')
const Fastify = require('fastify')
const fastifyKafka = require('..')
const { getDefaultOptions, generateGroupId, generateTopicName } = require('./utils')

test('multiple topics', t => {
t.plan(9)
const options = getDefaultOptions()
const group = generateGroupId()
options.consumer['group.id'] = group

const topicName1 = generateTopicName()
const topicName2 = generateTopicName()

const producerFastify = Fastify()
const consumerFastify = Fastify()

t.teardown(() => producerFastify.close())
t.teardown(() => consumerFastify.close())

consumerFastify
.register(fastifyKafka, { ...options, producer: undefined })
.after(err => {
t.error(err)

consumerFastify.kafka.consumer.on('error', t.fail)
consumerFastify.kafka.subscribe([topicName1, topicName2])

consumerFastify.kafka.on(topicName1, (msg, commit) => {
t.equal(msg.value.toString(), 'topic1')
commit()
t.ok(true)
})

consumerFastify.kafka.on(topicName2, (msg, commit) => {
t.equal(msg.value.toString(), 'topic2')
commit()
t.ok(true)
})

consumerFastify.kafka.consume()
})

producerFastify
.register(fastifyKafka, { ...options, consumer: undefined })
.after(err => {
t.error(err)

producerFastify.kafka.producer.on('error', t.fail)
producerFastify.kafka.push({
topic: topicName1,
payload: 'topic1',
key: 'testKey'
})
producerFastify.kafka.push({
topic: topicName2,
payload: 'topic2',
key: 'kafkaKey'
})

t.ok(true)
})

producerFastify.ready(err => {
t.error(err)

consumerFastify.ready(err => {
t.error(err)
})
})
})
3 changes: 1 addition & 2 deletions test/producer.js → test/producer.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict'

const t = require('tap')
const { test } = require('tap')
const log = require('abstract-logging')
const test = t.test

const Producer = require('../lib/producer')

Expand Down
Loading