Skip to content

Commit 47f6965

Browse files
authored
Migrate from tap to Node Test Runner (#573)
* test: migrate from tap to node "multipart-ajv-file.test.js" * test: migrate from tap to node multipart-attach-body.test.js * test: migrate from tap to node multipart-big-stream.test.js * test: migrate from tap to node multipart-body-schema.js * test: migrate from tap to node multipart-concat.test.js * test: migrate from tap to node multipart-disk.test.js * test: migrate from tap to node multipart-duplicate-save-request-file.test.js * test: migrate from tap to node multipart-empty-body.test.js * test: migrate from tap to node multipart-fileLimit.test.js * test: migrate from tap to node multipart-http2.test.js * fix * test: migrate from tap to node multipart-incomplete-upload.test.js * test: migrate from tap to node multipart-json.test.js * test: migrate from tap to node multipart-security.test.js * test: migrate from tap to node multipart-small-stream.test.js * test: migrate from tap to node multipart-update-options-types.test.js * test: migrate from tap to node multipart.test.js * test: migrate from tap to node stream-consumer.test.js * test: migrate from tap to node fix-313.test.js * test: migrate from tap to node generate-id.test.js * test: migrate from tap to node big.test.js * chore: remove tap * chore: add c8 for coverage * chore: remove -taprc * fix: update temporary directory path in multipart-disk.test.js * refactor: replace deepEqual with deepStrictEqual * fix: use ifError for error assertion * fix: standardize assertions using fail * fix: remove second argument to ifError
1 parent f84409e commit 47f6965

22 files changed

+597
-560
lines changed

.taprc

Lines changed: 0 additions & 2 deletions
This file was deleted.

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"@fastify/busboy": "^3.0.0",
1010
"@fastify/deepmerge": "^3.0.0",
1111
"@fastify/error": "^4.0.0",
12+
"c8": "^10.1.3",
1213
"fastify-plugin": "^5.0.0",
1314
"secure-json-parse": "^4.0.0"
1415
},
@@ -28,18 +29,16 @@
2829
"noop-stream": "^0.1.0",
2930
"pump": "^3.0.0",
3031
"readable-stream": "^4.5.2",
31-
"tap": "^18.6.1",
3232
"tsd": "^0.33.0"
3333
},
3434
"scripts": {
35-
"coverage": "npm run test:unit -- --coverage-report=html",
3635
"climem": "climem 8999 localhost",
3736
"lint": "eslint",
3837
"lint:fix": "eslint --fix",
3938
"start": "CLIMEM=8999 node -r climem ./examples/example",
4039
"test": "npm run test:unit && npm run test:typescript",
4140
"test:typescript": "tsd",
42-
"test:unit": "tap -t 120"
41+
"test:unit": "c8 --100 node --test"
4342
},
4443
"repository": {
4544
"type": "git",

test/big.test.js

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const test = require('tap').test
3+
const test = require('node:test')
44
const FormData = require('form-data')
55
const Fastify = require('fastify')
66
const multipart = require('..')
@@ -12,13 +12,13 @@ const crypto = require('node:crypto')
1212
const streamToNull = require('../lib/stream-consumer')
1313

1414
// skipping on Github Actions because it takes too long
15-
test('should upload a big file in constant memory', { skip: process.env.CI }, function (t) {
15+
test('should upload a big file in constant memory', { skip: process.env.CI }, function (t, done) {
1616
t.plan(10)
1717

1818
const fastify = Fastify()
1919
const hashInput = crypto.createHash('sha256')
2020

21-
t.teardown(fastify.close.bind(fastify))
21+
t.after(() => fastify.close())
2222

2323
fastify.register(multipart, {
2424
limits: {
@@ -28,23 +28,23 @@ test('should upload a big file in constant memory', { skip: process.env.CI }, fu
2828
})
2929

3030
fastify.post('/', async function (req, reply) {
31-
t.ok(req.isMultipart())
31+
t.assert.ok(req.isMultipart())
3232

3333
for await (const part of req.parts()) {
3434
if (part.file) {
35-
t.equal(part.type, 'file')
36-
t.equal(part.fieldname, 'upload')
37-
t.equal(part.filename, 'random-data')
38-
t.equal(part.encoding, '7bit')
39-
t.equal(part.mimetype, 'binary/octet-stream')
35+
t.assert.strictEqual(part.type, 'file')
36+
t.assert.strictEqual(part.fieldname, 'upload')
37+
t.assert.strictEqual(part.filename, 'random-data')
38+
t.assert.strictEqual(part.encoding, '7bit')
39+
t.assert.strictEqual(part.mimetype, 'binary/octet-stream')
4040

4141
await streamToNull(part.file)
4242
}
4343
}
4444

4545
const memory = process.memoryUsage()
46-
t.ok(memory.rss < 500 * 1024 * 1024)
47-
t.ok(memory.heapTotal < 500 * 1024 * 1024)
46+
t.assert.ok(memory.rss < 500 * 1024 * 1024)
47+
t.assert.ok(memory.heapTotal < 500 * 1024 * 1024)
4848

4949
reply.send()
5050
})
@@ -66,7 +66,7 @@ test('should upload a big file in constant memory', { skip: process.env.CI }, fu
6666
total -= n
6767

6868
if (total === 0) {
69-
t.pass('finished generating')
69+
t.assert.ok('finished generating')
7070
hashInput.end()
7171
this.push(null)
7272
}
@@ -88,12 +88,18 @@ test('should upload a big file in constant memory', { skip: process.env.CI }, fu
8888
method: 'POST'
8989
}
9090

91-
const req = http.request(opts, () => { fastify.close(noop) })
91+
const req = http.request(opts, (res) => {
92+
res.on('data', () => {})
93+
94+
res.on('end', () => {
95+
fastify.close(() => {
96+
done()
97+
})
98+
})
99+
})
92100

93101
pump(form, req, function (err) {
94-
t.error(err, 'client pump: no err')
102+
t.assert.ifError(err, 'client pump: no err')
95103
})
96104
})
97105
})
98-
99-
function noop () { }

test/fix-313.test.js

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const test = require('tap').test
3+
const test = require('node:test')
44
const FormData = require('form-data')
55
const Fastify = require('fastify')
66
const multipart = require('..')
@@ -17,38 +17,38 @@ test('should store file on disk, remove on response when attach fields to body i
1717
t.plan(25)
1818

1919
const fastify = Fastify()
20-
t.teardown(fastify.close.bind(fastify))
20+
t.after(() => fastify.close())
2121

2222
fastify.register(multipart, {
2323
attachFieldsToBody: true
2424
})
2525

2626
fastify.post('/', async function (req, reply) {
27-
t.ok(req.isMultipart())
27+
t.assert.ok(req.isMultipart())
2828

2929
const files = await req.saveRequestFiles()
3030

31-
t.ok(files[0].filepath)
32-
t.equal(files[0].type, 'file')
33-
t.equal(files[0].fieldname, 'upload')
34-
t.equal(files[0].filename, 'README.md')
35-
t.equal(files[0].encoding, '7bit')
36-
t.equal(files[0].mimetype, 'text/markdown')
37-
t.ok(files[0].fields.upload)
38-
t.ok(files[1].filepath)
39-
t.equal(files[1].type, 'file')
40-
t.equal(files[1].fieldname, 'upload')
41-
t.equal(files[1].filename, 'README.md')
42-
t.equal(files[1].encoding, '7bit')
43-
t.equal(files[1].mimetype, 'text/markdown')
44-
t.ok(files[1].fields.upload)
45-
t.ok(files[2].filepath)
46-
t.equal(files[2].type, 'file')
47-
t.equal(files[2].fieldname, 'other')
48-
t.equal(files[2].filename, 'README.md')
49-
t.equal(files[2].encoding, '7bit')
50-
t.equal(files[2].mimetype, 'text/markdown')
51-
t.ok(files[2].fields.upload)
31+
t.assert.ok(files[0].filepath)
32+
t.assert.strictEqual(files[0].type, 'file')
33+
t.assert.strictEqual(files[0].fieldname, 'upload')
34+
t.assert.strictEqual(files[0].filename, 'README.md')
35+
t.assert.strictEqual(files[0].encoding, '7bit')
36+
t.assert.strictEqual(files[0].mimetype, 'text/markdown')
37+
t.assert.ok(files[0].fields.upload)
38+
t.assert.ok(files[1].filepath)
39+
t.assert.strictEqual(files[1].type, 'file')
40+
t.assert.strictEqual(files[1].fieldname, 'upload')
41+
t.assert.strictEqual(files[1].filename, 'README.md')
42+
t.assert.strictEqual(files[1].encoding, '7bit')
43+
t.assert.strictEqual(files[1].mimetype, 'text/markdown')
44+
t.assert.ok(files[1].fields.upload)
45+
t.assert.ok(files[2].filepath)
46+
t.assert.strictEqual(files[2].type, 'file')
47+
t.assert.strictEqual(files[2].fieldname, 'other')
48+
t.assert.strictEqual(files[2].filename, 'README.md')
49+
t.assert.strictEqual(files[2].encoding, '7bit')
50+
t.assert.strictEqual(files[2].mimetype, 'text/markdown')
51+
t.assert.ok(files[2].fields.upload)
5252

5353
await access(files[0].filepath, fs.constants.F_OK)
5454
await access(files[1].filepath, fs.constants.F_OK)
@@ -63,8 +63,8 @@ test('should store file on disk, remove on response when attach fields to body i
6363
try {
6464
await access(request.tmpUploads[0], fs.constants.F_OK)
6565
} catch (error) {
66-
t.equal(error.code, 'ENOENT')
67-
t.pass('Temp file was removed after response')
66+
t.assert.strictEqual(error.code, 'ENOENT')
67+
t.assert.ok('Temp file was removed after response')
6868
ee.emit('response')
6969
}
7070
})
@@ -89,7 +89,7 @@ test('should store file on disk, remove on response when attach fields to body i
8989
form.pipe(req)
9090

9191
const [res] = await once(req, 'response')
92-
t.equal(res.statusCode, 200)
92+
t.assert.strictEqual(res.statusCode, 200)
9393
res.resume()
9494
await once(res, 'end')
9595
await once(ee, 'response')
@@ -99,7 +99,7 @@ test('should throw on saving request files when attach fields to body is true bu
9999
t.plan(3)
100100

101101
const fastify = Fastify()
102-
t.teardown(fastify.close.bind(fastify))
102+
t.after(() => fastify.close())
103103

104104
fastify.register(multipart, {
105105
attachFieldsToBody: true,
@@ -111,13 +111,13 @@ test('should throw on saving request files when attach fields to body is true bu
111111
})
112112

113113
fastify.post('/', async function (req, reply) {
114-
t.ok(req.isMultipart())
114+
t.assert.ok(req.isMultipart())
115115

116116
try {
117117
await req.saveRequestFiles()
118118
reply.code(200).send()
119119
} catch (error) {
120-
t.ok(error instanceof fastify.multipartErrors.FileBufferNotFoundError)
120+
t.assert.ok(error instanceof fastify.multipartErrors.FileBufferNotFoundError)
121121
reply.code(500).send()
122122
}
123123
})
@@ -140,7 +140,7 @@ test('should throw on saving request files when attach fields to body is true bu
140140
form.pipe(req)
141141

142142
const [res] = await once(req, 'response')
143-
t.equal(res.statusCode, 500)
143+
t.assert.strictEqual(res.statusCode, 500)
144144
res.resume()
145145
await once(res, 'end')
146146
})

test/generate-id.test.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
'use strict'
22

3-
const { test } = require('tap')
3+
const { test } = require('node:test')
44
const { generateId } = require('../lib/generateId')
55

66
test('returns', t => {
77
t.plan(3)
8-
t.type(generateId, 'function', 'is a function')
9-
t.type(generateId(), 'string', '~> returns a string')
10-
t.equal(generateId().length, 16, '~> has 16 characters (default)')
8+
t.assert.strictEqual(typeof generateId, 'function', 'is a function')
9+
t.assert.strictEqual(typeof generateId(), 'string', '~> returns a string')
10+
t.assert.strictEqual(generateId().length, 16, '~> has 16 characters (default)')
1111
})
1212

1313
test('length', t => {
@@ -18,18 +18,18 @@ test('length', t => {
1818
let tmp = ''
1919
for (; i < iterations; ++i) {
2020
tmp = generateId()
21-
t.equal(tmp.length, 16, `"${tmp}" is 16 characters`)
21+
t.assert.strictEqual(tmp.length, 16, `"${tmp}" is 16 characters`)
2222
}
2323
})
2424

2525
test('unique /1', t => {
2626
t.plan(1)
27-
t.not(generateId(), generateId(), '~> single')
27+
t.assert.notStrictEqual(generateId(), generateId(), '~> single')
2828
})
2929

3030
test('unique /2', t => {
3131
t.plan(1)
3232
const items = new Set()
3333
for (let i = 5e6; i--;) items.add(generateId())
34-
t.equal(items.size, 5e6, '~> 5,000,000 unique ids')
34+
t.assert.strictEqual(items.size, 5e6, '~> 5,000,000 unique ids')
3535
})

test/multipart-ajv-file.test.js

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
'use strict'
22

3-
const test = require('tap').test
43
const Fastify = require('fastify')
54
const FormData = require('form-data')
65
const http = require('node:http')
76
const multipart = require('..')
87
const { once } = require('node:events')
98
const fs = require('node:fs')
109
const path = require('node:path')
10+
const test = require('node:test')
1111

1212
const filePath = path.join(__dirname, '../README.md')
1313

14-
test('show modify the generated schema', async function (t) {
14+
test('show modify the generated schema', async t => {
1515
t.plan(4)
1616

1717
const fastify = Fastify({
@@ -20,7 +20,7 @@ test('show modify the generated schema', async function (t) {
2020
}
2121
})
2222

23-
t.teardown(fastify.close.bind(fastify))
23+
t.after(() => fastify.close())
2424

2525
await fastify.register(multipart, { attachFieldsToBody: true })
2626
await fastify.register(require('@fastify/swagger'), {
@@ -31,7 +31,7 @@ test('show modify the generated schema', async function (t) {
3131
}
3232
})
3333

34-
await fastify.post(
34+
fastify.post(
3535
'/',
3636
{
3737
schema: {
@@ -52,26 +52,24 @@ test('show modify the generated schema', async function (t) {
5252

5353
await fastify.ready()
5454

55-
t.match(fastify.swagger(), {
56-
paths: {
57-
'/': {
58-
post: {
59-
operationId: 'test',
60-
requestBody: {
61-
content: {
62-
'multipart/form-data': {
63-
schema: {
64-
type: 'object',
65-
properties: {
66-
field: { type: 'string', format: 'binary' }
67-
}
55+
t.assert.deepStrictEqual(fastify.swagger().paths, {
56+
'/': {
57+
post: {
58+
operationId: 'test',
59+
requestBody: {
60+
content: {
61+
'multipart/form-data': {
62+
schema: {
63+
type: 'object',
64+
properties: {
65+
field: { type: 'string', format: 'binary' }
6866
}
6967
}
7068
}
71-
},
72-
responses: {
73-
200: { description: 'Default Response' }
7469
}
70+
},
71+
responses: {
72+
200: { description: 'Default Response' }
7573
}
7674
}
7775
}
@@ -97,7 +95,7 @@ test('show modify the generated schema', async function (t) {
9795
const [res] = await once(req, 'response')
9896
res.resume()
9997
await once(res, 'end')
100-
t.equal(res.statusCode, 400) // body/field should be a file
98+
t.assert.strictEqual(res.statusCode, 400) // body/field should be a file
10199
}
102100

103101
// request with file
@@ -118,7 +116,7 @@ test('show modify the generated schema', async function (t) {
118116
const [res] = await once(req, 'response')
119117
res.resume()
120118
await once(res, 'end')
121-
t.equal(res.statusCode, 200)
119+
t.assert.strictEqual(res.statusCode, 200)
122120
}
123-
t.pass('res ended successfully')
121+
t.assert.ok('res ended successfully')
124122
})

0 commit comments

Comments
 (0)