Skip to content

Commit bdce68c

Browse files
authored
test: migrate esm util and helper tests to node:test (#859)
Signed-off-by: Luca Raveri <lucaraveri993@gmail.com>
1 parent 08c66cc commit bdce68c

2 files changed

Lines changed: 74 additions & 55 deletions

File tree

test/esm/util.test.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import { requireModule } from '../../util.js'
22
import { resolve, join } from 'node:path'
3-
import t from 'tap'
3+
import { test } from 'node:test'
44
import * as url from 'node:url'
55

6-
const test = t.test
7-
86
const __dirname = url.fileURLToPath(new URL('.', import.meta.url))
97

108
test('requiring a commonjs module works in an esm project', (t) => {
11-
t.plan(1)
129
const module = requireModule(
1310
resolve(join(__dirname, './data/custom-logger.cjs'))
1411
)
15-
t.strictSame(module, { name: 'Custom Logger', customLevels: { test: 99 } })
12+
t.assert.deepStrictEqual(module, { name: 'Custom Logger', customLevels: { test: 99 } })
1613
})

test/helper.test.js

Lines changed: 72 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
const util = require('node:util')
44
const fs = require('node:fs')
55
const path = require('node:path')
6-
const { test } = require('tap')
6+
const { test } = require('node:test')
77
const stream = require('node:stream')
8+
const os = require('node:os')
89

910
const helper = require('../helper')
1011

@@ -14,37 +15,49 @@ const readFile = util.promisify(fs.readFile)
1415
test('should return the fastify instance', async t => {
1516
const argv = ['./examples/plugin.js']
1617
const app = await helper.build(argv, {})
17-
t.teardown(() => app.close())
18-
t.notOk(app.server.listening)
18+
t.after(() => app.close())
19+
t.assert.ok(!app.server.listening)
1920
})
2021

2122
test('should reload the env at each build', async t => {
22-
const testdir = t.testdir({
23-
'.env': 'GREETING=world',
24-
'plugin.js': await readFile(path.join(__dirname, '../examples/plugin-with-env.js'))
23+
const testdir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'fastify-cli-'))
24+
t.after(async () => {
25+
await fs.promises.rm(testdir, { recursive: true, force: true })
2526
})
2627

28+
await writeFile(path.join(testdir, '.env'), 'GREETING=world')
29+
await writeFile(
30+
path.join(testdir, 'plugin.js'),
31+
await readFile(path.join(__dirname, '../examples/plugin-with-env.js'))
32+
)
33+
2734
const argv = [path.join(testdir, 'plugin.js')]
2835
const cwd = process.cwd()
2936

3037
process.chdir(testdir)
31-
t.teardown(() => { process.chdir(cwd) })
38+
t.after(() => { process.chdir(cwd) })
3239

3340
{
3441
await writeFile(path.join(testdir, '.env'), 'GREETING=one')
3542
const app = await helper.build(argv)
36-
t.teardown(() => app.close())
37-
const res = await app.inject('/')
38-
t.same(res.json(), { hello: 'one' })
43+
try {
44+
const res = await app.inject('/')
45+
t.assert.deepStrictEqual(res.json(), { hello: 'one' })
46+
} finally {
47+
await app.close()
48+
}
3949
}
4050

4151
{
4252
delete process.env.GREETING // dotenv will not overwrite the env if set
4353
await writeFile(path.join(testdir, '.env'), 'GREETING=two')
4454
const app = await helper.build(argv)
45-
t.teardown(() => app.close())
46-
const res = await app.inject('/')
47-
t.same(res.json(), { hello: 'two' })
55+
try {
56+
const res = await app.inject('/')
57+
t.assert.deepStrictEqual(res.json(), { hello: 'two' })
58+
} finally {
59+
await app.close()
60+
}
4861
}
4962
})
5063

@@ -57,15 +70,18 @@ test('setting plugin options', async t => {
5770
'world'
5871
]
5972
const app = await helper.build(argv, { from: 'build' })
60-
t.teardown(() => app.close())
61-
const res = await app.inject('/')
62-
t.same(res.json(), {
63-
a: true,
64-
b: true,
65-
c: true,
66-
hello: 'world',
67-
from: 'build'
68-
})
73+
try {
74+
const res = await app.inject('/')
75+
t.assert.deepStrictEqual(res.json(), {
76+
a: true,
77+
b: true,
78+
c: true,
79+
hello: 'world',
80+
from: 'build'
81+
})
82+
} finally {
83+
await app.close()
84+
}
6985
})
7086

7187
test('setting plugin options, extra has priority', async t => {
@@ -76,29 +92,35 @@ test('setting plugin options, extra has priority', async t => {
7692
'world'
7793
]
7894
const app = await helper.build(argv, { hello: 'planet' })
79-
t.teardown(() => app.close())
80-
const res = await app.inject('/')
81-
t.same(res.json(), {
82-
hello: 'planet'
83-
})
95+
try {
96+
const res = await app.inject('/')
97+
t.assert.deepStrictEqual(res.json(), {
98+
hello: 'planet'
99+
})
100+
} finally {
101+
await app.close()
102+
}
84103
})
85104

86105
test('setting plugin options, extra has priority', async t => {
87106
const args = './examples/plugin-with-custom-options.js -- --hello world --from args'
88107
const app = await helper.build(args, { hello: 'planet' })
89-
t.teardown(() => app.close())
90-
const res = await app.inject('/')
91-
t.same(res.json(), {
92-
hello: 'planet',
93-
from: 'args'
94-
})
108+
try {
109+
const res = await app.inject('/')
110+
t.assert.deepStrictEqual(res.json(), {
111+
hello: 'planet',
112+
from: 'args'
113+
})
114+
} finally {
115+
await app.close()
116+
}
95117
})
96118

97119
test('should start fastify', async t => {
98120
const argv = ['./examples/plugin.js']
99121
const app = await helper.listen(argv, {})
100-
t.teardown(() => app.close())
101-
t.ok(app.server.listening)
122+
t.after(() => app.close())
123+
t.assert.ok(app.server.listening)
102124
})
103125

104126
test('should start fastify with custom logger configuration', async t => {
@@ -117,12 +139,12 @@ test('should start fastify with custom logger configuration', async t => {
117139
stream: dest
118140
}
119141
})
120-
t.teardown(() => app.close())
142+
t.after(() => app.close())
121143
app.log.info('test')
122-
t.same(lines.length, 0)
144+
t.assert.strictEqual(lines.length, 0)
123145
app.log.warn('test')
124-
t.same(lines.length, 1)
125-
t.same(app.log.level, 'warn')
146+
t.assert.strictEqual(lines.length, 1)
147+
t.assert.strictEqual(app.log.level, 'warn')
126148
})
127149

128150
test('should merge the CLI and FILE configs', async t => {
@@ -142,24 +164,24 @@ test('should merge the CLI and FILE configs', async t => {
142164
stream: dest
143165
}
144166
})
145-
t.teardown(() => app.close())
167+
t.after(() => app.close())
146168
app.log.info('test')
147-
t.same(lines.length, 0)
169+
t.assert.strictEqual(lines.length, 0)
148170
app.log.warn({ foo: 'test' })
149-
t.same(app.log.level, 'warn')
150-
t.same(lines.length, 1)
151-
t.same(lines[0].foo, '***')
171+
t.assert.strictEqual(app.log.level, 'warn')
172+
t.assert.strictEqual(lines.length, 1)
173+
t.assert.strictEqual(lines[0].foo, '***')
152174
})
153175

154176
test('should ensure can access all decorators', async t => {
155177
const argv = ['./examples/plugin.js']
156178
const app = await helper.build(argv, { skipOverride: true })
157-
t.teardown(() => app.close())
158-
t.ok(app.test)
179+
t.after(() => app.close())
180+
t.assert.ok(app.test)
159181

160182
const app2 = await helper.listen(argv, { skipOverride: true })
161-
t.teardown(() => app2.close())
162-
t.ok(app2.test)
183+
t.after(() => app2.close())
184+
t.assert.ok(app2.test)
163185
})
164186

165187
test('should return the fastify instance when using serverModule', async t => {
@@ -168,6 +190,6 @@ test('should return the fastify instance when using serverModule', async t => {
168190
app.decorate('foo', 'bar')
169191
next()
170192
})
171-
t.teardown(() => app.close())
172-
t.equals(app.foo, 'bar')
193+
t.after(() => app.close())
194+
t.assert.strictEqual(app.foo, 'bar')
173195
})

0 commit comments

Comments
 (0)