diff --git a/test/basic.test.js b/test/basic.test.js index 05a4810..6cf8e1b 100644 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -1,174 +1,183 @@ 'use strict' -const { test } = require('tap') +const { test } = require('node:test') const boot = require('..') -test('boot an empty app', (t) => { +test('boot an empty app', (t, done_) => { t.plan(1) const app = boot() app.on('start', () => { - t.pass('booted') + t.assert.ok(true, 'booted') + done_() }) }) -test('start returns app', (t) => { +test('start returns app', (t, done_) => { t.plan(1) const app = boot({}, { autostart: false }) app .start() .ready((err) => { - t.error(err) + t.assert.ifError(err) + done_() }) }) -test('boot an app with a plugin', (t) => { +test('boot an app with a plugin', (t, done_) => { t.plan(4) const app = boot() let after = false app.use(function (server, opts, done) { - t.equal(server, app, 'the first argument is the server') - t.same(opts, {}, 'no options') - t.ok(after, 'delayed execution') + t.assert.deepStrictEqual(server, app, 'the first argument is the server') + t.assert.deepStrictEqual(opts, {}, 'no options') + t.assert.ok(after, 'delayed execution') done() }) after = true app.on('start', () => { - t.pass('booted') + t.assert.ok(true, 'booted') + done_() }) }) -test('boot an app with a promisified plugin', (t) => { +test('boot an app with a promisified plugin', (t, done_) => { t.plan(4) const app = boot() let after = false app.use(function (server, opts) { - t.equal(server, app, 'the first argument is the server') - t.same(opts, {}, 'no options') - t.ok(after, 'delayed execution') + t.assert.deepStrictEqual(server, app, 'the first argument is the server') + t.assert.deepStrictEqual(opts, {}, 'no options') + t.assert.ok(after, 'delayed execution') return Promise.resolve() }) after = true app.on('start', () => { - t.pass('booted') + t.assert.ok(true, 'booted') + done_() }) }) -test('boot an app with a plugin and a callback /1', (t) => { +test('boot an app with a plugin and a callback /1', (t, done_) => { t.plan(2) const app = boot(() => { - t.pass('booted') + t.assert.ok(true, 'booted') }) app.use(function (server, opts, done) { - t.pass('plugin loaded') + t.assert.ok(true, 'plugin loaded') done() + done_() }) }) -test('boot an app with a plugin and a callback /2', (t) => { +test('boot an app with a plugin and a callback /2', (t, done_) => { t.plan(2) const app = boot({}, () => { - t.pass('booted') + t.assert.ok(true, 'booted') }) app.use(function (server, opts, done) { - t.pass('plugin loaded') + t.assert.ok(true, 'plugin loaded') done() + done_() }) }) -test('boot a plugin with a custom server', (t) => { +test('boot a plugin with a custom server', (t, done_) => { t.plan(4) const server = {} const app = boot(server) app.use(function (s, opts, done) { - t.equal(s, server, 'the first argument is the server') - t.same(opts, {}, 'no options') + t.assert.deepStrictEqual(s, server, 'the first argument is the server') + t.assert.deepStrictEqual(opts, {}, 'no options') done() }) app.onClose(() => { - t.ok('onClose called') + t.assert.ok(true, 'onClose called') + done_() }) app.on('start', () => { app.close(() => { - t.pass('booted') + t.assert.ok(true, 'booted') }) }) }) -test('custom instance should inherits avvio methods /1', (t) => { +test('custom instance should inherits avvio methods /1', (t, done_) => { t.plan(6) const server = {} const app = boot(server, {}) server.use(function (s, opts, done) { - t.equal(s, server, 'the first argument is the server') - t.same(opts, {}, 'no options') + t.assert.deepStrictEqual(s, server, 'the first argument is the server') + t.assert.deepStrictEqual(opts, {}, 'no options') done() }).after(() => { - t.ok('after called') + t.assert.ok(true, 'after called') }) server.onClose(() => { - t.ok('onClose called') + t.assert.ok(true, 'onClose called') + done_() }) server.ready(() => { - t.ok('ready called') + t.assert.ok(true, 'ready called') }) app.on('start', () => { server.close(() => { - t.pass('booted') + t.assert.ok(true, 'booted') }) }) }) -test('custom instance should inherits avvio methods /2', (t) => { +test('custom instance should inherits avvio methods /2', (t, done_) => { t.plan(6) const server = {} const app = new boot(server, {}) // eslint-disable-line new-cap server.use(function (s, opts, done) { - t.equal(s, server, 'the first argument is the server') - t.same(opts, {}, 'no options') + t.assert.deepStrictEqual(s, server, 'the first argument is the server') + t.assert.deepStrictEqual(opts, {}, 'no options') done() }).after(() => { - t.ok('after called') + t.assert.ok(true, 'after called') }) server.onClose(() => { - t.ok('onClose called') + t.assert.ok(true, 'onClose called') + done_() }) server.ready(() => { - t.ok('ready called') + t.assert.ok(true, 'ready called') }) app.on('start', () => { server.close(() => { - t.pass('booted') + t.assert.ok(true, 'booted') }) }) }) -test('boot a plugin with options', (t) => { +test('boot a plugin with options', (t, done_) => { t.plan(3) const server = {} @@ -178,17 +187,18 @@ test('boot a plugin with options', (t) => { } app.use(function (s, opts, done) { - t.equal(s, server, 'the first argument is the server') - t.same(opts, myOpts, 'passed options') + t.assert.deepStrictEqual(s, server, 'the first argument is the server') + t.assert.deepStrictEqual(opts, myOpts, 'passed options') done() }, myOpts) app.on('start', () => { - t.pass('booted') + t.assert.ok(true, 'booted') + done_() }) }) -test('boot a plugin with a function that returns the options', (t) => { +test('boot a plugin with a function that returns the options', (t, done_) => { t.plan(4) const server = {} @@ -197,7 +207,7 @@ test('boot a plugin with a function that returns the options', (t) => { hello: 'world' } const myOptsAsFunc = parent => { - t.equal(parent, server) + t.assert.deepStrictEqual(parent, server) return parent.myOpts } @@ -207,46 +217,47 @@ test('boot a plugin with a function that returns the options', (t) => { }, myOpts) app.use(function (s, opts, done) { - t.equal(s, server, 'the first argument is the server') - t.same(opts, myOpts, 'passed options via function accessing parent injected variable') + t.assert.deepStrictEqual(s, server, 'the first argument is the server') + t.assert.deepStrictEqual(opts, myOpts, 'passed options via function accessing parent injected variable') done() }, myOptsAsFunc) app.on('start', () => { - t.pass('booted') + t.assert.ok(true, 'booted') + done_() }) }) test('throw on non-function use', (t) => { t.plan(1) const app = boot() - t.throws(() => { + t.assert.throws(() => { app.use({}) }) }) // https://github.com/mcollina/avvio/issues/20 -test('ready and nextTick', (t) => { +test('ready and nextTick', (t, done_) => { const app = boot() process.nextTick(() => { app.ready(() => { - t.end() + done_() }) }) }) // https://github.com/mcollina/avvio/issues/20 -test('promises and microtask', (t) => { +test('promises and microtask', (t, done_) => { const app = boot() Promise.resolve() .then(() => { app.ready(function () { - t.end() + done_() }) }) }) -test('always loads nested plugins after the current one', (t) => { +test('always loads nested plugins after the current one', (t, done_) => { t.plan(2) const server = {} @@ -259,31 +270,33 @@ test('always loads nested plugins after the current one', (t) => { second = true done() }) - t.notOk(second) + t.assert.ok(!second) done() }) app.on('start', () => { - t.ok(second) + t.assert.ok(true, second) + done_() }) }) -test('promise long resolve', (t) => { +test('promise long resolve', (t, done_) => { t.plan(2) const app = boot() setTimeout(function () { - t.throws(() => { + t.assert.throws(() => { app.use((s, opts, done) => { done() }) }, 'root plugin has already booted') + done_() }) app.ready(function (err) { - t.notOk(err) + t.assert.ok(!err) }) }) @@ -292,12 +305,11 @@ test('do not autostart', (t) => { autostart: false }) app.on('start', () => { - t.fail() + t.assert.fail() }) - t.end() }) -test('start with ready', (t) => { +test('start with ready', (t, done_) => { t.plan(2) const app = boot(null, { @@ -305,15 +317,16 @@ test('start with ready', (t) => { }) app.on('start', () => { - t.pass() + t.assert.ok(true) + done_() }) app.ready(function (err) { - t.error(err) + t.assert.ifError(err) }) }) -test('load a plugin after start()', (t) => { +test('load a plugin after start()', (t, done_) => { t.plan(1) let startCalled = false @@ -322,8 +335,9 @@ test('load a plugin after start()', (t) => { }) app.use((s, opts, done) => { - t.ok(startCalled) + t.assert.ok(startCalled) done() + done_() }) // we use a timer because @@ -337,18 +351,19 @@ test('load a plugin after start()', (t) => { }, 2) }) -test('booted should be set before ready', (t) => { +test('booted should be set before ready', (t, done_) => { t.plan(2) const app = boot() app.ready(function (err) { - t.error(err) - t.equal(app.booted, true) + t.assert.ifError(err) + t.assert.ok(app.booted) + done_() }) }) -test('start should be emitted after ready resolves', (t) => { +test('start should be emitted after ready resolves', (t, done_) => { t.plan(1) const app = boot() @@ -359,23 +374,25 @@ test('start should be emitted after ready resolves', (t) => { }) app.on('start', function () { - t.equal(ready, true) + t.assert.ok(ready) + done_() }) }) -test('throws correctly if registering after ready', (t) => { +test('throws correctly if registering after ready', (t, done_) => { t.plan(1) const app = boot() app.ready(function () { - t.throws(() => { + t.assert.throws(() => { app.use((a, b, done) => done()) }, 'root plugin has already booted') + done_() }) }) -test('preReady errors must be managed', (t) => { +test('preReady errors must be managed', (t, done_) => { t.plan(2) const app = boot() @@ -389,12 +406,13 @@ test('preReady errors must be managed', (t) => { }) app.ready(err => { - t.pass('ready function is called') - t.equal(err.message, 'boom') + t.assert.ok(true, 'ready function is called') + t.assert.equal(err.message, 'boom') + done_() }) }) -test('preReady errors do not override plugin\'s errors', (t) => { +test('preReady errors do not override plugin\'s errors', (t, done_) => { t.plan(3) const app = boot() @@ -404,17 +422,18 @@ test('preReady errors do not override plugin\'s errors', (t) => { }) app.on('preReady', () => { - t.pass('preReady is executed') + t.assert.ok(true, 'preReady is executed') throw new Error('boom') }) app.ready(err => { - t.pass('ready function is called') - t.equal(err.message, 'baam') + t.assert.ok(true, 'ready function is called') + t.assert.equal(err.message, 'baam') + done_() }) }) -test('support faux modules', (t) => { +test('support faux modules', (t, done_) => { t.plan(4) const app = boot() @@ -424,9 +443,9 @@ test('support faux modules', (t) => { // or Babel that they export a .default property. app.use({ default: function (server, opts, done) { - t.equal(server, app, 'the first argument is the server') - t.same(opts, {}, 'no options') - t.ok(after, 'delayed execution') + t.assert.deepStrictEqual(server, app, 'the first argument is the server') + t.assert.deepStrictEqual(opts, {}, 'no options') + t.assert.ok(true, after, 'delayed execution') done() } }) @@ -434,6 +453,7 @@ test('support faux modules', (t) => { after = true app.on('start', () => { - t.pass('booted') + t.assert.ok(true, 'booted') + done_() }) })