Skip to content

Commit 1d9f848

Browse files
authored
chore: migrate to node test runner (#142)
* chore: communication test * chore: consume callback * chore: consumer * chore: export * chore: install c8 * chore: multiple topics * chore: procuder * chore: producer * chore: import utils * fix: extract util for withResolvers
1 parent dca4a41 commit 1d9f848

8 files changed

+125
-88
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"lint": "eslint",
1212
"lint:fix": "eslint --fix",
1313
"test": "npm run test:unit && npm run test:typescript",
14-
"test:unit": "tap",
14+
"test:unit": "c8 --100 node --test",
1515
"test:typescript": "tsd"
1616
},
1717
"repository": {
@@ -67,9 +67,9 @@
6767
"@fastify/pre-commit": "^2.1.0",
6868
"@types/node": "^22.0.0",
6969
"abstract-logging": "2.0.1",
70+
"c8": "^10.1.3",
7071
"fastify": "^5.0.0",
7172
"neostandard": "^0.12.0",
72-
"tap": "^20.0.3",
7373
"tsd": "^0.31.1"
7474
},
7575
"tsd": {

test/communication.test.js

+18-18
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
'use strict'
22

3-
const { test } = require('tap')
3+
const { test } = require('node:test')
44
const Fastify = require('fastify')
55
const fastifyKafka = require('..')
66
const {
77
generateTopicName,
88
generateGroupId,
9-
getDefaultOptions
9+
getDefaultOptions,
10+
withResolvers
1011
} = require('./utils')
1112

12-
test('communication', t => {
13-
t.plan(7)
13+
test('communication', async t => {
14+
t.plan(5)
1415
const options = getDefaultOptions()
1516
const group = generateGroupId()
1617
options.consumer['group.id'] = group
@@ -25,19 +26,22 @@ test('communication', t => {
2526
consumerFastify.close()
2627
})
2728

29+
const { promise, resolve } = withResolvers()
30+
2831
consumerFastify
2932
.register(fastifyKafka, { ...options, producer: undefined })
3033
.after(err => {
31-
t.error(err)
34+
t.assert.ok(!err)
3235

33-
consumerFastify.kafka.consumer.on('error', t.fail)
36+
consumerFastify.kafka.consumer.on('error', t.assert.fail)
3437
consumerFastify.kafka.subscribe(topicName)
3538

3639
consumerFastify.kafka.on(topicName, (msg, commit) => {
37-
t.equal(msg.value.toString(), 'hello world!')
40+
t.assert.deepStrictEqual(msg.value.toString(), 'hello world!')
3841
commit()
3942

40-
t.ok(true)
43+
t.assert.ok(true)
44+
resolve()
4145
})
4246

4347
consumerFastify.kafka.consume()
@@ -46,23 +50,19 @@ test('communication', t => {
4650
producerFastify
4751
.register(fastifyKafka, { ...options, consumer: undefined })
4852
.after(err => {
49-
t.error(err)
53+
t.assert.ok(!err)
5054

51-
producerFastify.kafka.producer.on('error', t.fail)
55+
producerFastify.kafka.producer.on('error', t.assert.fail)
5256
producerFastify.kafka.push({
5357
topic: topicName,
5458
payload: 'hello world!',
5559
key: 'testKey'
5660
})
5761

58-
t.ok(true)
62+
t.assert.ok(true)
5963
})
6064

61-
producerFastify.ready(err => {
62-
t.error(err)
63-
64-
consumerFastify.ready(err => {
65-
t.error(err)
66-
})
67-
})
65+
await producerFastify.ready()
66+
await consumerFastify.ready()
67+
return promise
6868
})

test/consume-callback.test.js

+20-23
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
'use strict'
22

3-
const { test } = require('tap')
3+
const { test } = require('node:test')
44
const Fastify = require('fastify')
55
const fastifyKafka = require('..')
6-
const { getDefaultOptions, generateGroupId, generateTopicName } = require('./utils')
6+
const { getDefaultOptions, generateGroupId, generateTopicName, withResolvers } = require('./utils')
77

8-
test('consume callback', t => {
8+
test('consume callback', async t => {
99
const options = getDefaultOptions()
1010
const group = generateGroupId()
1111
options.consumer['group.id'] = group
@@ -21,25 +21,25 @@ test('consume callback', t => {
2121
consumerFastify.close()
2222
})
2323

24+
const { promise, resolve } = withResolvers()
25+
2426
consumerFastify
2527
.register(fastifyKafka, { ...options, producer: undefined })
2628
.after(err => {
27-
t.error(err)
29+
t.assert.ok(!err)
2830

29-
consumerFastify.kafka.consumer.on('error', t.fail)
31+
consumerFastify.kafka.consumer.on('error', t.assert.fail)
3032
consumerFastify.kafka.subscribe(topicName)
3133

32-
consumerFastify.kafka.on(topicName, t.fail)
34+
consumerFastify.kafka.on(topicName, t.assert.fail)
3335

3436
function onConsume (err, message) {
35-
t.error(err)
36-
t.match(message, {
37-
topic: topicName,
38-
value: Buffer.from('hello world!'),
39-
key: Buffer.from('testKey')
40-
})
41-
42-
t.end()
37+
t.assert.ok(!err)
38+
t.assert.deepStrictEqual(message.topic, topicName)
39+
t.assert.deepStrictEqual(message.value.toString(), 'hello world!')
40+
t.assert.deepStrictEqual(message.key.toString(), 'testKey')
41+
42+
resolve()
4343
}
4444

4545
consumerFastify.kafka.consume(onConsume)
@@ -48,23 +48,20 @@ test('consume callback', t => {
4848
producerFastify
4949
.register(fastifyKafka, { ...options, consumer: undefined })
5050
.after(err => {
51-
t.error(err)
51+
t.assert.ok(!err)
5252

53-
producerFastify.kafka.producer.on('error', t.fail)
53+
producerFastify.kafka.producer.on('error', t.assert.fail)
5454
producerFastify.kafka.push({
5555
topic: topicName,
5656
payload: 'hello world!',
5757
key: 'testKey'
5858
})
5959

60-
t.ok(true)
60+
t.assert.ok(true)
6161
})
6262

63-
producerFastify.ready(err => {
64-
t.error(err)
63+
await producerFastify.ready()
64+
await consumerFastify.ready()
6565

66-
consumerFastify.ready(err => {
67-
t.error(err)
68-
})
69-
})
66+
return promise
7067
})

test/consumer.test.js

+25-10
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,76 @@
11
'use strict'
22

3-
const { test } = require('tap')
3+
const { test } = require('node:test')
44
const log = require('abstract-logging')
55

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

8+
const { withResolvers } = require('./utils')
9+
810
const options = {
911
'metadata.broker.list': '192.0.2.1:9092',
1012
'fetch.wait.max.ms': 10,
1113
'fetch.error.backoff.ms': 50,
1214
'group.id': 'new-group-id'
1315
}
1416

15-
test('unreachable brokers', t => {
17+
test('unreachable brokers', async t => {
1618
t.plan(1)
19+
const { promise, resolve } = withResolvers()
1720
const consumer = new Consumer(options, log, (err) => {
18-
t.ok(err)
21+
t.assert.ok(err)
22+
resolve()
1923
}, {}, { timeout: 200 })
2024
consumer.on('ready', (e) => {
21-
t.error(e)
25+
t.assert.ok(!e)
2226
})
27+
28+
return promise
2329
})
2430

2531
test('error event before connection', t => {
2632
t.plan(1)
33+
const { promise, resolve } = withResolvers()
2734
const consumer = new Consumer(options, log, (err) => {
28-
t.ok(err)
35+
t.assert.ok(err)
36+
resolve()
2937
}, {}, { timeout: 200 })
3038
consumer.consumer.emit('event.error', new Error('Test Error'))
39+
return promise
3140
})
3241

3342
test('error event after connection', t => {
3443
t.plan(2)
3544
const opts = { ...options, 'metadata.broker.list': '127.0.0.1:9092' }
45+
const { promise, resolve } = withResolvers()
3646
const consumer = new Consumer(opts, log, (err) => {
37-
t.error(err)
47+
t.assert.ok(!err)
3848
consumer.consumer.emit('event.error', new Error('Test Error'))
3949
})
4050
consumer.on('error', (s) => {
41-
t.ok(s)
51+
t.assert.ok(s)
52+
resolve()
4253
})
4354
t.after(() => {
4455
consumer.stop()
4556
})
57+
return promise
4658
})
4759

4860
test('empty message with data event', t => {
4961
t.plan(3)
5062
const opts = { ...options, 'metadata.broker.list': '127.0.0.1:9092' }
63+
const { promise, resolve } = withResolvers()
5164
const consumer = new Consumer(opts, log, (err) => {
52-
t.error(err)
53-
t.throws(() => consumer.consumer.emit('data'))
65+
t.assert.ok(!err)
66+
t.assert.throws(() => consumer.consumer.emit('data'))
5467
})
5568
consumer.on('error', (s) => {
56-
t.ok(s)
69+
t.assert.ok(s)
70+
resolve()
5771
})
5872
t.after(() => {
5973
consumer.stop()
6074
})
75+
return promise
6176
})

test/export.test.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
'use strict'
22

3-
const { test } = require('tap')
3+
const { test } = require('node:test')
44
const fastifyKafka = require('..')
55
const defaultExport = require('..').default
66
const { fastifyKafka: namedExport } = require('..')
77

8-
test('export', function (t) {
8+
test('export', async function (t) {
99
t.plan(3)
1010

11-
t.test('module export', function (t) {
11+
await t.test('module export', function (t) {
1212
t.plan(1)
13-
t.equal(typeof fastifyKafka, 'function')
13+
t.assert.deepStrictEqual(typeof fastifyKafka, 'function')
1414
})
1515

16-
t.test('default export', function (t) {
16+
await t.test('default export', function (t) {
1717
t.plan(1)
18-
t.equal(typeof defaultExport, 'function')
18+
t.assert.deepStrictEqual(typeof defaultExport, 'function')
1919
})
2020

21-
t.test('named export', function (t) {
21+
await t.test('named export', function (t) {
2222
t.plan(1)
23-
t.equal(typeof namedExport, 'function')
23+
t.assert.deepStrictEqual(typeof namedExport, 'function')
2424
})
2525
})

test/multiple-topics.test.js

+23-21
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use strict'
22

3-
const { test } = require('tap')
3+
const { test } = require('node:test')
44
const Fastify = require('fastify')
55
const fastifyKafka = require('..')
6-
const { getDefaultOptions, generateGroupId, generateTopicName } = require('./utils')
6+
const { getDefaultOptions, generateGroupId, generateTopicName, withResolvers } = require('./utils')
77

8-
test('multiple topics', t => {
9-
t.plan(9)
8+
test('multiple topics', async t => {
9+
t.plan(7)
1010
const options = getDefaultOptions()
1111
const group = generateGroupId()
1212
options.consumer['group.id'] = group
@@ -19,36 +19,43 @@ test('multiple topics', t => {
1919

2020
consumerFastify
2121
.register(fastifyKafka, { ...options, producer: undefined })
22-
.after(err => {
23-
t.error(err)
22+
.after(async err => {
23+
t.assert.ok(!err)
2424

2525
consumerFastify.kafka.consumer.on('error', () => {
26-
t.fail()
26+
t.assert.fail()
2727
})
2828
consumerFastify.kafka.subscribe([topicName1, topicName2])
2929

30+
const { promise: promiseTopic1, resolve: resolveTopic1 } = withResolvers()
31+
const { promise: promiseTopic2, resolve: resolveTopic2 } = withResolvers()
32+
3033
consumerFastify.kafka.on(topicName1, (msg, commit) => {
31-
t.equal(msg.value.toString(), 'topic1')
34+
t.assert.deepStrictEqual(msg.value.toString(), 'topic1')
3235
commit()
33-
t.ok(true)
36+
t.assert.ok(true)
37+
resolveTopic1()
3438
})
3539

3640
consumerFastify.kafka.on(topicName2, (msg, commit) => {
37-
t.equal(msg.value.toString(), 'topic2')
41+
t.assert.deepStrictEqual(msg.value.toString(), 'topic2')
3842
commit()
39-
t.ok(true)
43+
t.assert.ok(true)
44+
resolveTopic2()
4045
})
4146

4247
consumerFastify.kafka.consume()
48+
49+
await Promise.all([promiseTopic1, promiseTopic2])
4350
})
4451

4552
producerFastify
4653
.register(fastifyKafka, { ...options, consumer: undefined })
4754
.after(err => {
48-
t.error(err)
55+
t.assert.ok(!err)
4956

5057
producerFastify.kafka.producer.on('error', () => {
51-
t.fail()
58+
t.assert.fail()
5259
})
5360
producerFastify.kafka.push({
5461
topic: topicName1,
@@ -61,16 +68,11 @@ test('multiple topics', t => {
6168
key: 'kafkaKey'
6269
})
6370

64-
t.ok(true)
71+
t.assert.ok(true)
6572
})
6673

67-
producerFastify.ready(err => {
68-
t.error(err)
69-
70-
consumerFastify.ready(err => {
71-
t.error(err)
72-
})
73-
})
74+
await producerFastify.ready()
75+
await consumerFastify.ready()
7476

7577
t.after(() => {
7678
producerFastify.kafka.producer.stop()

0 commit comments

Comments
 (0)