diff --git a/test/start.test.js b/test/start.test.js index 8e732550..9c1b518f 100644 --- a/test/start.test.js +++ b/test/start.test.js @@ -11,8 +11,10 @@ const baseFilename = path.join(__dirname, 'fixtures', `test_${crypto.randomBytes const { fork } = require('node:child_process') const moduleSupport = semver.satisfies(process.version, '>= 14 || >= 12.17.0 < 13.0.0') -const t = require('tap') -const test = t.test +const { test } = require('node:test') + +const os = require('os') +const { promisify } = require('node:util') const sinon = require('sinon') const proxyquire = require('proxyquire').noPreserveCache() const start = require('../start') @@ -20,6 +22,18 @@ const start = require('../start') const writeFile = util.promisify(fs.writeFile) const readFile = util.promisify(fs.readFile) +const mkdtemp = promisify(fs.mkdtemp) + +async function createTestDir (files) { + const tmpDir = await mkdtemp(path.join(os.tmpdir(), 'my-test-')) + await Promise.all( + Object.entries(files).map(([filename, content]) => + writeFile(path.join(tmpDir, filename), content) + ) + ) + return tmpDir +} + function requireUncached (module) { delete require.cache[require.resolve(module)] return require(module) @@ -42,14 +56,14 @@ test('should start the server', async t => { const fastify = await start.start(argv) const result = await fetch(`http://localhost:${fastify.server.address().port}`) - t.equal(result.status, 200) + t.assert.strictEqual(result.status, 200) const body = await result.text() - t.equal(result.headers.get('content-length'), '' + body.length) - t.same(JSON.parse(body), { hello: 'world' }) + t.assert.strictEqual(result.headers.get('content-length'), '' + body.length) + t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' }) await fastify.close() - t.pass('server closed') + t.assert.ok('server closed') }) test('should start the server with a typescript compiled module', async t => { @@ -59,14 +73,14 @@ test('should start the server with a typescript compiled module', async t => { const fastify = await start.start(argv) const result = await fetch(`http://localhost:${fastify.server.address().port}`) - t.equal(result.status, 200) + t.assert.strictEqual(result.status, 200) const body = await result.text() - t.equal(result.headers.get('content-length'), '' + body.length) - t.same(JSON.parse(body), { hello: 'world' }) + t.assert.strictEqual(result.headers.get('content-length'), '' + body.length) + t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' }) await fastify.close() - t.pass('server closed') + t.assert.ok('server closed') }) test('should start the server with pretty output', async t => { @@ -76,14 +90,14 @@ test('should start the server with pretty output', async t => { const fastify = await start.start(argv) const result = await fetch(`http://localhost:${fastify.server.address().port}`) - t.equal(result.status, 200) + t.assert.strictEqual(result.status, 200) const body = await result.text() - t.equal(result.headers.get('content-length'), '' + body.length) - t.same(JSON.parse(body), { hello: 'world' }) + t.assert.strictEqual(result.headers.get('content-length'), '' + body.length) + t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' }) await fastify.close() - t.pass('server closed') + t.assert.ok('server closed') }) test('should start fastify with custom options', async t => { @@ -94,9 +108,9 @@ test('should start fastify with custom options', async t => { const argv = ['-p', getPort(), '-o', 'true', './examples/plugin-with-options.js'] const fastify = await start.start(argv) await fastify.close() - t.pass('server closed') + t.assert.ok('server closed') } catch (e) { - t.pass('Custom options') + t.assert.ok('Custom options') } }) @@ -115,20 +129,19 @@ test('should start fastify with custom plugin options', async t => { const fastify = await start.start(argv) const result = await fetch(`http://localhost:${fastify.server.address().port}`) - t.equal(result.status, 200) + t.assert.strictEqual(result.status, 200) const body = await result.text() - t.equal(result.headers.get('content-length'), '' + body.length) - t.same(JSON.parse(body), { + t.assert.strictEqual(result.headers.get('content-length'), '' + body.length) + t.assert.deepStrictEqual(JSON.parse(body), { a: true, b: true, c: true, hello: 'world' }) - await fastify.close() - t.pass('server closed') - t.end() + t.after(async () => await fastify.close()) + t.assert.ok('server closed') }) test('should start fastify with default custom plugin options', async t => { @@ -143,17 +156,16 @@ test('should start fastify with default custom plugin options', async t => { const fastify = await start.start(argv) const result = await fetch(`http://localhost:${fastify.server.address().port}`) - t.equal(result.status, 200) + t.assert.strictEqual(result.status, 200) const body = await result.text() - t.equal(result.headers.get('content-length'), '' + body.length) - t.same(JSON.parse(body), { + t.assert.strictEqual(result.headers.get('content-length'), '' + body.length) + t.assert.deepStrictEqual(JSON.parse(body), { hello: 'test' }) - await fastify.close() - t.pass('server closed') - t.end() + t.after(async () => await fastify.close()) + t.assert.ok('server closed') }) test('should start fastify with custom options with a typescript compiled plugin', async t => { @@ -163,9 +175,9 @@ test('should start fastify with custom options with a typescript compiled plugin try { const argv = ['-p', getPort(), '-o', 'true', './examples/ts-plugin-with-options.js'] await start.start(argv) - t.fail('Custom options') + t.assert.rejects('Custom options') } catch (e) { - t.pass('Custom options') + t.assert.ok('Custom options') } }) @@ -184,19 +196,19 @@ test('should start fastify with custom plugin options with a typescript compiled const fastify = await start.start(argv) const result = await fetch(`http://localhost:${fastify.server.address().port}`) - t.equal(result.status, 200) + t.assert.strictEqual(result.status, 200) const body = await result.text() - t.equal(result.headers.get('content-length'), '' + body.length) - t.same(JSON.parse(body), { + t.assert.strictEqual(result.headers.get('content-length'), '' + body.length) + t.assert.deepStrictEqual(JSON.parse(body), { a: true, b: true, c: true, hello: 'world' }) - await fastify.close() - t.pass('server closed') + t.after(async () => await fastify.close()) + t.assert.ok('server closed') }) test('should start fastify with custom plugin default exported options with a typescript compiled plugin', async t => { @@ -211,17 +223,16 @@ test('should start fastify with custom plugin default exported options with a ty const fastify = await start.start(argv) const result = await fetch(`http://localhost:${fastify.server.address().port}`) - t.equal(result.status, 200) + t.assert.strictEqual(result.status, 200) const body = await result.text() - t.equal(result.headers.get('content-length'), '' + body.length) - t.same(JSON.parse(body), { + t.assert.strictEqual(result.headers.get('content-length'), '' + body.length) + t.assert.deepStrictEqual(JSON.parse(body), { hello: 'test' }) - await fastify.close() - t.pass('server closed') - t.end() + t.after(async () => await fastify.close()) + t.assert.ok('server closed') }) test('should start the server at the given prefix', async t => { @@ -231,21 +242,21 @@ test('should start the server at the given prefix', async t => { const fastify = await start.start(argv) const result = await fetch(`http://localhost:${fastify.server.address().port}/api/hello`) - t.equal(result.status, 200) + t.assert.strictEqual(result.status, 200) const body = await result.text() - t.equal(result.headers.get('content-length'), '' + body.length) - t.same(JSON.parse(body), { hello: 'world' }) + t.assert.strictEqual(result.headers.get('content-length'), '' + body.length) + t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' }) - await fastify.close() - t.pass('server closed') + t.after(async () => await fastify.close()) + t.assert.ok('server closed') }) test('should start fastify at given socket path', { skip: process.platform === 'win32' }, async t => { t.plan(1) const sockFile = path.resolve('test.sock') - t.teardown(() => { + t.after(() => { try { fs.unlinkSync(sockFile) } catch (e) { } @@ -264,13 +275,13 @@ test('should start fastify at given socket path', { skip: process.platform === ' path: '/', socketPath: sockFile }, function (response) { - t.same(response.statusCode, 200) + t.assert.strictEqual(response.statusCode, 200) return resolve() }) request.end() }) - t.teardown(fastify.close.bind(fastify)) + t.after(async () => await fastify.close()) }) test('should error with a good timeout value', async t => { @@ -279,7 +290,7 @@ test('should error with a good timeout value', async t => { const start = proxyquire('../start', { assert: { ifError (err) { - t.equal(err.code, 'AVV_ERR_PLUGIN_EXEC_TIMEOUT') + t.assert.strictEqual(err.code, 'AVV_ERR_PLUGIN_EXEC_TIMEOUT') } } }) @@ -290,7 +301,7 @@ test('should error with a good timeout value', async t => { const argv = ['-p', port, '-T', '100', './test/data/timeout-plugin.js'] await start.start(argv) } catch (err) { - t.equal(err.code, 'AVV_ERR_PLUGIN_EXEC_TIMEOUT') + t.assert.strictEqual(err.code, 'AVV_ERR_PLUGIN_EXEC_TIMEOUT') } }) @@ -298,35 +309,37 @@ test('should warn on file not found', t => { t.plan(1) const oldStop = start.stop - t.teardown(() => { start.stop = oldStop }) + t.after(() => { start.stop = oldStop }) start.stop = function (message) { - t.ok(/not-found.js doesn't exist within/.test(message), message) + t.assert.ok(/not-found.js doesn't exist within/.test(message), message) } const argv = ['-p', getPort(), './data/not-found.js'] start.start(argv) }) -test('should throw on package not found', t => { +test('should throw on package not found', (t, done) => { t.plan(1) const oldStop = start.stop - t.teardown(() => { start.stop = oldStop }) + t.after(() => { start.stop = oldStop }) start.stop = function (err) { - t.ok(/Cannot find module 'unknown-package'/.test(err.message), err.message) + t.assert.ok(/Cannot find module 'unknown-package'/.test(err.message), err.message) + done() } const argv = ['-p', getPort(), './test/data/package-not-found.js'] start.start(argv) }) -test('should throw on parsing error', t => { +test('should throw on parsing error', (t, done) => { t.plan(1) const oldStop = start.stop - t.teardown(() => { start.stop = oldStop }) + t.after(() => { start.stop = oldStop }) start.stop = function (err) { - t.equal(err.constructor, SyntaxError) + t.assert.strictEqual(err.constructor, SyntaxError) + done() } const argv = ['-p', getPort(), './test/data/parsing-error.js'] @@ -335,8 +348,8 @@ test('should throw on parsing error', t => { test('should start the server with an async/await plugin', async t => { if (Number(process.versions.node[0]) < 7) { - t.pass('Skip because Node version < 7') - return t.end() + t.assert.ok('Skip because Node version < 7') + return } t.plan(4) @@ -345,40 +358,39 @@ test('should start the server with an async/await plugin', async t => { const fastify = await start.start(argv) const result = await fetch(`http://localhost:${fastify.server.address().port}`) - t.equal(result.status, 200) + t.assert.strictEqual(result.status, 200) const body = await result.text() - t.equal(result.headers.get('content-length'), '' + body.length) - t.same(JSON.parse(body), { hello: 'world' }) + t.assert.strictEqual(result.headers.get('content-length'), '' + body.length) + t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' }) - await fastify.close() - t.pass('server closed') + t.after(async () => await fastify.close()) + t.assert.ok('server closed') }) test('should exit without error on help', t => { const exit = process.exit process.exit = sinon.spy() - t.teardown(() => { + t.after(() => { process.exit = exit }) const argv = ['-p', getPort(), '-h', 'true'] start.start(argv) - t.ok(process.exit.called) - t.equal(process.exit.lastCall.args[0], undefined) - - t.end() + t.assert.ok(process.exit.called) + t.assert.strictEqual(process.exit.lastCall.args[0], undefined) }) -test('should throw the right error on require file', t => { +test('should throw the right error on require file', (t, done) => { t.plan(1) const oldStop = start.stop - t.teardown(() => { start.stop = oldStop }) + t.after(() => { start.stop = oldStop }) start.stop = function (err) { - t.ok(/undefinedVariable is not defined/.test(err.message), err.message) + t.assert.ok(/undefinedVariable is not defined/.test(err.message), err.message) + done() } const argv = ['-p', getPort(), './test/data/undefinedVariable.js'] @@ -400,16 +412,16 @@ test('should respond 413 - Payload too large', async t => { body: bodyTooLarge, }) - t.equal(responseFail.status, 413) + t.assert.strictEqual(responseFail.status, 413) const responseOk = await fetch(`http://localhost:${fastify.server.address().port}`, { method: 'POST', body: bodySmaller, }) - t.equal(responseOk.status, 200) + t.assert.strictEqual(responseOk.status, 200) - await fastify.close() - t.pass('server closed') + t.after(async () => await fastify.close()) + t.assert.ok('server closed') }) test('should start the server (using env var)', async t => { @@ -420,16 +432,16 @@ test('should start the server (using env var)', async t => { const fastify = await start.start(argv) const result = await fetch(`http://localhost:${process.env.FASTIFY_PORT}`) - t.equal(result.status, 200) + t.assert.strictEqual(result.status, 200) const body = await result.text() - t.equal(result.headers.get('content-length'), '' + body.length) - t.same(JSON.parse(body), { hello: 'world' }) + t.assert.strictEqual(result.headers.get('content-length'), '' + body.length) + t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' }) delete process.env.FASTIFY_PORT - await fastify.close() - t.pass('server closed') + t.after(async () => await fastify.close()) + t.assert.ok('server closed') }) test('should start the server (using PORT-env var)', async t => { @@ -440,16 +452,16 @@ test('should start the server (using PORT-env var)', async t => { const fastify = await start.start(argv) const result = await fetch(`http://localhost:${process.env.PORT}`) - t.equal(result.status, 200) + t.assert.strictEqual(result.status, 200) const body = await result.text() - t.equal(result.headers.get('content-length'), '' + body.length) - t.same(JSON.parse(body), { hello: 'world' }) + t.assert.strictEqual(result.headers.get('content-length'), '' + body.length) + t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' }) delete process.env.PORT - await fastify.close() - t.pass('server closed') + t.after(async () => await fastify.close()) + t.assert.ok('server closed') }) test('should start the server (using FASTIFY_PORT-env preceding PORT-env var)', async t => { @@ -461,17 +473,17 @@ test('should start the server (using FASTIFY_PORT-env preceding PORT-env var)', const fastify = await start.start(argv) const result = await fetch(`http://localhost:${process.env.FASTIFY_PORT}`) - t.equal(result.status, 200) + t.assert.strictEqual(result.status, 200) const body = await result.text() - t.equal(result.headers.get('content-length'), '' + body.length) - t.same(JSON.parse(body), { hello: 'world' }) + t.assert.strictEqual(result.headers.get('content-length'), '' + body.length) + t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' }) delete process.env.FASTIFY_PORT delete process.env.PORT - await fastify.close() - t.pass('server closed') + t.after(async () => await fastify.close()) + t.assert.ok('server closed') }) test('should start the server (using -p preceding FASTIFY_PORT-env var)', async t => { @@ -483,16 +495,16 @@ test('should start the server (using -p preceding FASTIFY_PORT-env var)', async const fastify = await start.start(argv) const result = await fetch(`http://localhost:${port}`) - t.equal(result.status, 200) + t.assert.strictEqual(result.status, 200) const body = await result.text() - t.equal(result.headers.get('content-length'), '' + body.length) - t.same(JSON.parse(body), { hello: 'world' }) + t.assert.strictEqual(result.headers.get('content-length'), '' + body.length) + t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' }) delete process.env.FASTIFY_PORT - await fastify.close() - t.pass('server closed') + t.after(async () => await fastify.close()) + t.assert.ok('server closed') }) test('should start the server at the given prefix (using env var)', async t => { @@ -504,17 +516,17 @@ test('should start the server at the given prefix (using env var)', async t => { const fastify = await start.start(argv) const result = await fetch(`http://localhost:${process.env.FASTIFY_PORT}/api/hello`) - t.equal(result.status, 200) + t.assert.strictEqual(result.status, 200) const body = await result.text() - t.equal(result.headers.get('content-length'), '' + body.length) - t.same(JSON.parse(body), { hello: 'world' }) + t.assert.strictEqual(result.headers.get('content-length'), '' + body.length) + t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' }) delete process.env.FASTIFY_PORT delete process.env.FASTIFY_PREFIX - await fastify.close() - t.pass('server closed') + t.after(async () => await fastify.close()) + t.assert.ok('server closed') }) test('should start the server at the given prefix (using env var read from dotenv)', async t => { @@ -523,18 +535,18 @@ test('should start the server at the given prefix (using env var read from doten const start = proxyquire('../start', { dotenv: { config () { - t.pass('config called') + t.assert.ok('config called') process.env.FASTIFY_PORT = 8080 } } }) const argv = ['./examples/plugin.js'] const fastify = await start.start(argv) - t.equal(fastify.server.address().port, 8080) + t.assert.strictEqual(fastify.server.address().port, 8080) delete process.env.FASTIFY_PORT - await fastify.close() - t.pass('server closed') + t.after(async () => await fastify.close()) + t.assert.ok('server closed') }) test('should start the server listening on 0.0.0.0 when running in docker', async t => { @@ -549,10 +561,10 @@ test('should start the server listening on 0.0.0.0 when running in docker', asyn const argv = ['-p', getPort(), './examples/plugin.js'] const fastify = await start.start(argv) - t.equal(fastify.server.address().address, '0.0.0.0') + t.assert.strictEqual(fastify.server.address().address, '0.0.0.0') - await fastify.close() - t.pass('server closed') + t.after(async () => await fastify.close()) + t.assert.ok('server closed') }) test('should start the server listening on 0.0.0.0 when running in kubernetes', async t => { @@ -570,10 +582,10 @@ test('should start the server listening on 0.0.0.0 when running in kubernetes', const argv = ['-p', getPort(), './examples/plugin.js'] const fastify = await start.start(argv) - t.equal(fastify.server.address().address, '0.0.0.0') + t.assert.strictEqual(fastify.server.address().address, '0.0.0.0') - await fastify.close() - t.pass('server closed') + t.after(async () => await fastify.close()) + t.assert.ok('server closed') }) test('should start the server with watch options that the child process restart when directory changed', { skip: ['win32', 'darwin'].includes(process.platform) }, async (t) => { @@ -586,7 +598,7 @@ test('should start the server with watch options that the child process restart const argv = ['-p', port, '-w', './examples/plugin.js'] const fastifyEmitter = await start.start(argv) - t.teardown(async () => { + t.after(async () => { if (fs.existsSync(tmpjs)) { fs.unlinkSync(tmpjs) } @@ -594,15 +606,15 @@ test('should start the server with watch options that the child process restart }) await once(fastifyEmitter, 'ready') - t.pass('should receive ready event') + t.assert.ok('should receive ready event') const restartPromise = once(fastifyEmitter, 'restart') await writeFile(tmpjs, 'hello fastify', { flag: 'a+' }) // chokidar watch can't catch change event in CI, but local test is all ok. you can remove annotation in local environment. - t.pass('change tmpjs') + t.assert.ok('change tmpjs') // this might happen more than once but does not matter in this context await restartPromise - t.pass('should receive restart event') + t.assert.ok('should receive restart event') }) test('should start the server with watch and verbose-watch options that the child process restart when directory changed with console message about changes ', { skip: ['win32', 'darwin'].includes(process.platform) }, async (t) => { @@ -627,7 +639,7 @@ test('should start the server with watch and verbose-watch options that the chil const argv = ['-p', port, '-w', '--verbose-watch', './examples/plugin.js'] const fastifyEmitter = await start.start(argv) - t.teardown(async () => { + t.after(async () => { if (fs.existsSync(tmpjs)) { fs.unlinkSync(tmpjs) } @@ -635,53 +647,52 @@ test('should start the server with watch and verbose-watch options that the chil }) await once(fastifyEmitter, 'ready') - t.pass('should receive ready event') + t.assert.ok('should receive ready event') const restartPromise = once(fastifyEmitter, 'restart') await writeFile(tmpjs, 'hello fastify', { flag: 'a+' }) // chokidar watch can't catch change event in CI, but local test is all ok. you can remove annotation in local environment. - t.pass('change tmpjs') + t.assert.ok('change tmpjs') // this might happen more than once but does not matter in this context await restartPromise - t.pass('should receive restart event') - t.ok(spy.args.length > 0, 'should print a console message on file update') + t.assert.ok('should receive restart event') + t.assert.ok(spy.args.length > 0, 'should print a console message on file update') }) test('should reload the env on restart when watching', { skip: process.platform === 'win32' }, async (t) => { - const testdir = t.testdir({ + const testdir = await createTestDir({ '.env': 'GREETING=world', 'plugin.js': await readFile(path.join(__dirname, '../examples/plugin-with-env.js')) }) const cwd = process.cwd() - process.chdir(testdir) const port = getPort() const argv = ['-p', port, '-w', path.join(testdir, 'plugin.js')] const fastifyEmitter = await requireUncached('../start').start(argv) - t.teardown(() => { + t.after(() => { process.chdir(cwd) }) await once(fastifyEmitter, 'ready') const r1 = await fetch(`http://localhost:${port}`) - t.equal(r1.status, 200) + t.assert.strictEqual(r1.status, 200) const body1 = await r1.text() - t.same(JSON.parse(body1), { hello: 'world' }) + t.assert.deepStrictEqual(JSON.parse(body1), { hello: 'world' }) await writeFile(path.join(testdir, '.env'), 'GREETING=planet') await once(fastifyEmitter, 'restart') const r2 = await fetch(`http://localhost:${port}`) - t.equal(r2.status, 200) + t.assert.strictEqual(r2.status, 200) const body2 = await r2.text() - t.same(JSON.parse(body2), { hello: 'world' }) /* world because when making a restart the server still passes the arguments that change the environment variable */ + t.assert.deepStrictEqual(JSON.parse(body2), { hello: 'world' }) /* world because when making a restart the server still passes the arguments that change the environment variable */ await fastifyEmitter.stop() }) @@ -689,38 +700,38 @@ test('should reload the env on restart when watching', { skip: process.platform test('should read env variables from .env file', async (t) => { const port = getPort() - const testdir = t.testdir({ + const testdir = await createTestDir({ '.env': `FASTIFY_PORT=${port}`, 'plugin.js': await readFile(path.join(__dirname, '../examples/plugin.js')) }) const cwd = process.cwd() - process.chdir(testdir) - t.teardown(() => { + t.after(() => { process.chdir(cwd) }) const fastify = await requireUncached('../start').start([path.join(testdir, 'plugin.js')]) - t.equal(fastify.server.address().port, +port) + t.assert.strictEqual(fastify.server.address().port, +port) const res = await fetch(`http://localhost:${port}`) - t.equal(res.status, 200) + t.assert.strictEqual(res.status, 200) const body = await res.text() - t.same(JSON.parse(body), { hello: 'world' }) + t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' }) - await fastify.close() + t.after(() => fastify.close()) }) -test('crash on unhandled rejection', t => { +test('crash on unhandled rejection', (t, done) => { t.plan(1) const argv = ['-p', getPort(), './test/data/rejection.js'] const child = fork(path.join(__dirname, '..', 'start.js'), argv, { silent: true }) child.on('close', function (code) { - t.equal(code, 1) + t.assert.strictEqual(code, 1) + done() }) }) @@ -730,16 +741,16 @@ test('should start the server with inspect options and the defalut port is 9320' const start = proxyquire('../start', { 'node:inspector': { open (p) { - t.equal(p, 9320) - t.pass('inspect open called') + t.assert.strictEqual(p, 9320) + t.assert.ok('inspect open called') } } }) const argv = ['--d', './examples/plugin.js'] const fastify = await start.start(argv) - await fastify.close() - t.pass('server closed') + t.after(async () => await fastify.close()) + t.assert.ok('server closed') }) test('should start the server with inspect options and use the exactly port', async t => { @@ -749,16 +760,16 @@ test('should start the server with inspect options and use the exactly port', as const start = proxyquire('../start', { 'node:inspector': { open (p) { - t.equal(p, Number(port)) - t.pass('inspect open called') + t.assert.strictEqual(p, Number(port)) + t.assert.ok('inspect open called') } } }) const argv = ['--d', '--debug-port', port, './examples/plugin.js'] const fastify = await start.start(argv) - await fastify.close() - t.pass('server closed') + t.after(() => fastify.close()) + t.assert.ok('server closed') }) test('boolean env are not overridden if no arguments are passed', async t => { @@ -771,9 +782,9 @@ test('boolean env are not overridden if no arguments are passed', async t => { try { const argv = ['./examples/plugin-with-options.js'] await start.start(argv) - t.fail('Custom options') + t.assert.rejects('Custom options') } catch (e) { - t.pass('Custom options') + t.assert.ok('Custom options') } }) @@ -782,10 +793,10 @@ test('should support preloading custom module', async t => { const argv = ['-r', './test/data/custom-require.js', './examples/plugin.js'] const fastify = await start.start(argv) - t.ok(GLOBAL_MODULE_1) + t.assert.ok(GLOBAL_MODULE_1) - await fastify.close() - t.pass('server closed') + t.after(() => fastify.close()) + t.assert.ok('server closed') }) test('should support preloading custom ES module', async t => { @@ -793,10 +804,10 @@ test('should support preloading custom ES module', async t => { const argv = ['-i', './test/data/custom-import.mjs', './examples/plugin.js'] const fastify = await start.start(argv) - t.ok(globalThis.GLOBAL_MODULE_3) + t.assert.ok(globalThis.GLOBAL_MODULE_3) - await fastify.close() - t.pass('server closed') + t.after(() => fastify.close()) + t.assert.ok('server closed') }) test('should support preloading multiple custom modules', async t => { @@ -804,11 +815,11 @@ test('should support preloading multiple custom modules', async t => { const argv = ['-r', './test/data/custom-require.js', '-r', './test/data/custom-require2.js', './examples/plugin.js'] const fastify = await start.start(argv) - t.ok(GLOBAL_MODULE_1) - t.ok(GLOBAL_MODULE_2) + t.assert.ok(GLOBAL_MODULE_1) + t.assert.ok(GLOBAL_MODULE_2) - await fastify.close() - t.pass('server closed') + t.after(() => fastify.close()) + t.assert.ok('server closed') }) test('should support preloading multiple custom ES modules', async t => { @@ -816,10 +827,10 @@ test('should support preloading multiple custom ES modules', async t => { const argv = ['-i', './test/data/custom-import.mjs', '-i', './test/data/custom-import2.mjs', './examples/plugin.js'] const fastify = await start.start(argv) - t.ok(GLOBAL_MODULE_3) - t.ok(GLOBAL_MODULE_4) - await fastify.close() - t.pass('server closed') + t.assert.ok(GLOBAL_MODULE_3) + t.assert.ok(GLOBAL_MODULE_4) + t.after(async () => await fastify.close()) + t.assert.ok('server closed') }) test('preloading custom module with empty and trailing require flags should not throw', async t => { @@ -827,10 +838,10 @@ test('preloading custom module with empty and trailing require flags should not const argv = ['-r', './test/data/custom-require.js', '-r', '', './examples/plugin.js', '-r'] const fastify = await start.start(argv) - t.ok(GLOBAL_MODULE_1) + t.assert.ok(GLOBAL_MODULE_1) - await fastify.close() - t.pass('server closed') + t.after(() => fastify.close()) + t.assert.ok('server closed') }) test('preloading custom ES module with empty and trailing import flags should not throw', async t => { @@ -838,42 +849,42 @@ test('preloading custom ES module with empty and trailing import flags should no const argv = ['-i', './test/data/custom-import.mjs', '-i', '', './examples/plugin.js', '-i'] const fastify = await start.start(argv) - t.ok(GLOBAL_MODULE_3) + t.assert.ok(GLOBAL_MODULE_3) - await fastify.close() - t.pass('server closed') + t.after(() => fastify.close()) + t.assert.ok('server closed') }) test('preloading custom module that is not found should throw', async t => { t.plan(2) const oldStop = start.stop - t.teardown(() => { start.stop = oldStop }) + t.after(() => { start.stop = oldStop }) start.stop = function (err) { - t.ok(/Cannot find module/.test(err.message), err.message) + t.assert.ok(/Cannot find module/.test(err.message), err.message) } const argv = ['-r', './test/data/require-missing.js', './examples/plugin.js'] const fastify = await start.start(argv) - await fastify.close() - t.pass('server closed') + t.after(() => fastify.close()) + t.assert.ok('server closed') }) test('preloading custom ES module that is not found should throw', async t => { t.plan(2) const oldStop = start.stop - t.teardown(() => { start.stop = oldStop }) + t.after(() => { start.stop = oldStop }) start.stop = function (err) { - t.ok(/Cannot find module/.test(err.message), err.message) + t.assert.ok(/Cannot find module/.test(err.message), err.message) } const argv = ['-i', './test/data/import-missing.mjs', './examples/plugin.js'] const fastify = await start.start(argv) - await fastify.close() - t.pass('server closed') + t.after(() => fastify.close()) + t.assert.ok('server closed') }) test('preloading custom module should be done before starting server', async t => { @@ -883,14 +894,14 @@ test('preloading custom module should be done before starting server', async t = const fastify = await start.start(argv) const result = await fetch(`http://localhost:${fastify.server.address().port}`) - t.equal(result.status, 200) + t.assert.strictEqual(result.status, 200) const body = await result.text() - t.equal(result.headers.get('content-length'), '' + body.length) - t.same(JSON.parse(body), { hasPreloaded: true }) + t.assert.strictEqual(result.headers.get('content-length'), '' + body.length) + t.assert.deepStrictEqual(JSON.parse(body), { hasPreloaded: true }) - await fastify.close() - t.pass('server closed') + t.after(() => fastify.close()) + t.assert.ok('server closed') }) test('should support custom logger configuration', async t => { @@ -898,10 +909,10 @@ test('should support custom logger configuration', async t => { const argv = ['-L', './test/data/custom-logger.js', './examples/plugin.js'] const fastify = await start.start(argv) - t.ok(fastify.log.test) + t.assert.ok(fastify.log.test) - await fastify.close() - t.pass('server closed') + t.after(() => fastify.close()) + t.assert.ok('server closed') }) test('should support custom logger configuration in ESM', async t => { @@ -909,10 +920,10 @@ test('should support custom logger configuration in ESM', async t => { const argv = ['-L', './test/data/custom-logger.mjs', './examples/plugin.js'] const fastify = await start.start(argv) - t.ok(fastify.log.test) + t.assert.ok(fastify.log.test) - await fastify.close() - t.pass('server closed') + t.after(() => fastify.close()) + t.assert.ok('server closed') }) test('preloading a built-in module works', async t => { @@ -920,8 +931,8 @@ test('preloading a built-in module works', async t => { const argv = ['-r', 'path', './examples/plugin.js'] const fastify = await start.start(argv) - await fastify.close() - t.pass('server closed') + t.after(() => fastify.close()) + t.assert.ok('server closed') }) test('preloading a module in node_modules works', async t => { @@ -929,33 +940,33 @@ test('preloading a module in node_modules works', async t => { const argv = ['-r', 'tap', './examples/plugin.js'] const fastify = await start.start(argv) - await fastify.close() - t.pass('server closed') + t.after(() => fastify.close()) + t.assert.ok('server closed') }) test('should throw on logger configuration module not found', async t => { t.plan(2) const oldStop = start.stop - t.teardown(() => { start.stop = oldStop }) + t.after(() => { start.stop = oldStop }) start.stop = function (err) { - t.ok(/Cannot find module/.test(err.message), err.message) + t.assert.ok(/Cannot find module/.test(err.message), err.message) } const argv = ['-L', './test/data/missing.js', './examples/plugin.js'] const fastify = await start.start(argv) - await fastify.close() - t.pass('server closed') + t.after(() => fastify.close()) + t.assert.ok('server closed') }) test('should throw on async plugin with one argument', async t => { t.plan(1) const oldStop = start.stop - t.teardown(() => { start.stop = oldStop }) + t.after(() => { start.stop = oldStop }) start.stop = function (err) { - t.ok(/Async\/Await plugin function should contain 2 arguments./.test(err.message), err.message) + t.assert.ok(/Async\/Await plugin function should contain 2 arguments./.test(err.message), err.message) } const argv = ['./test/data/async-plugin-with-one-argument.js'] @@ -977,20 +988,19 @@ test('should start fastify with custom plugin options with a ESM typescript comp const fastify = await start.start(argv) const result = await fetch(`http://localhost:${fastify.server.address().port}`) - t.equal(result.status, 200) + t.assert.strictEqual(result.status, 200) const body = await result.text() - t.equal(result.headers.get('content-length'), '' + body.length) - t.same(JSON.parse(body), { + t.assert.strictEqual(result.headers.get('content-length'), '' + body.length) + t.assert.deepStrictEqual(JSON.parse(body), { a: true, b: true, c: true, hello: 'world' }) - await fastify.close() - t.pass('server closed') - t.end() + t.after(() => fastify.close()) + t.assert.ok('server closed') }) test('should start fastify with custom plugin default options with a ESM typescript compiled plugin', { skip: !moduleSupport }, async t => { @@ -1005,31 +1015,29 @@ test('should start fastify with custom plugin default options with a ESM typescr const fastify = await start.start(argv) const result = await fetch(`http://localhost:${fastify.server.address().port}`) - t.equal(result.status, 200) + t.assert.strictEqual(result.status, 200) const body = await result.text() - t.equal(result.headers.get('content-length'), '' + body.length) - t.same(JSON.parse(body), { + t.assert.strictEqual(result.headers.get('content-length'), '' + body.length) + t.assert.deepStrictEqual(JSON.parse(body), { hello: 'test' }) - await fastify.close() - t.pass('server closed') - t.end() + t.after(() => fastify.close()) + t.assert.ok('server closed') }) test('should throw an error when loading ESM typescript compiled plugin and ESM is not supported', { skip: moduleSupport }, async t => { t.plan(1) const oldStop = start.stop - t.teardown(() => { start.stop = oldStop }) + t.after(() => { start.stop = oldStop }) start.stop = function (err) { - t.ok(/Your version of node does not support ES modules./.test(err.message), err.message) + t.assert.ok(/Your version of node does not support ES modules./.test(err.message), err.message) } const argv = ['./examples/ts-plugin-with-custom-options.mjs'] await start.start(argv) - t.end() }) test('should start fastify with custom plugin options with a ESM plugin with package.json "type":"module"', { skip: !moduleSupport }, async t => { @@ -1047,20 +1055,19 @@ test('should start fastify with custom plugin options with a ESM plugin with pac const fastify = await start.start(argv) const result = await fetch(`http://localhost:${fastify.server.address().port}`) - t.equal(result.status, 200) + t.assert.strictEqual(result.status, 200) const body = await result.text() - t.equal(result.headers.get('content-length'), '' + body.length) - t.same(JSON.parse(body), { + t.assert.strictEqual(result.headers.get('content-length'), '' + body.length) + t.assert.deepStrictEqual(JSON.parse(body), { a: true, b: true, c: true, hello: 'world' }) - await fastify.close() - t.pass('server closed') - t.end() + t.after(() => fastify.close()) + t.assert.ok('server closed') }) test('should start fastify with custom server options (ignoreTrailingSlash) with a ESM plugin with package.json "type":"module"', { skip: !moduleSupport }, async t => { @@ -1075,20 +1082,19 @@ test('should start fastify with custom server options (ignoreTrailingSlash) with const fastify = await start.start(argv) const result1 = await fetch(`http://localhost:${fastify.server.address().port}/foo`) - t.equal(result1.status, 200) + t.assert.strictEqual(result1.status, 200) const body1 = await result1.text() - t.equal(result1.headers.get('content-length'), '' + body1.length) + t.assert.strictEqual(result1.headers.get('content-length'), '' + body1.length) const result2 = await fetch(`http://localhost:${fastify.server.address().port}/foo/`) - t.equal(result2.status, 200) + t.assert.strictEqual(result2.status, 200) const body2 = await result2.text() - t.equal(result2.headers.get('content-length'), '' + body2.length) + t.assert.strictEqual(result2.headers.get('content-length'), '' + body2.length) - await fastify.close() - t.pass('server closed') - t.end() + t.after(() => fastify.close()) + t.assert.ok('server closed') }) test('should start fastify with custom plugin options with a CJS plugin with package.json "type":"module"', { skip: !moduleSupport }, async t => { @@ -1106,20 +1112,19 @@ test('should start fastify with custom plugin options with a CJS plugin with pac const fastify = await start.start(argv) const result = await fetch(`http://localhost:${fastify.server.address().port}`) - t.equal(result.status, 200) + t.assert.strictEqual(result.status, 200) const body = await result.text() - t.equal(result.headers.get('content-length'), '' + body.length) - t.same(JSON.parse(body), { + t.assert.strictEqual(result.headers.get('content-length'), '' + body.length) + t.assert.deepStrictEqual(JSON.parse(body), { a: true, b: true, c: true, hello: 'world' }) - await fastify.close() - t.pass('server closed') - t.end() + t.after(async () => await fastify.close()) + t.assert.ok('server closed') }) test('should throw error for invalid fastify plugin (object)', async t => { @@ -1128,8 +1133,8 @@ test('should throw error for invalid fastify plugin (object)', async t => { const port = getPort() const argv = ['-p', port, '-T', '100', './test/data/object.js'] await start.start(argv) - t.fail('should not start') + t.assert.rejects('should not start') } catch (err) { - t.equal(err.code, 'AVV_ERR_PLUGIN_NOT_VALID') + t.assert.strictEqual(err.code, 'AVV_ERR_PLUGIN_NOT_VALID') } })