diff --git a/test/graceful-shutdown.test.js b/test/graceful-shutdown.test.js index 4b9196e0..2eb48351 100644 --- a/test/graceful-shutdown.test.js +++ b/test/graceful-shutdown.test.js @@ -1,23 +1,25 @@ 'use strict' -const t = require('tap') -// Tests skip on win32 platforms due SIGINT signal is not supported across all windows platforms -const test = (process.platform === 'win32') ? t.skip : t.test +const { test, beforeEach, afterEach } = require('node:test') const sinon = require('sinon') const start = require('../start') let _port = 3001 - function getPort () { return '' + _port++ } -let spy = null let fastify = null +let spy = null let signalCounter = null const sandbox = sinon.createSandbox() -t.beforeEach(async () => { +// Skip tests on Windows, Linux and MacOS +const isWindows = process.platform === 'win32' +const isMacOS = process.platform === 'darwin' +const isLinux = process.platform === 'linux' + +beforeEach(async () => { signalCounter = process.listenerCount('SIGINT') const argv = ['-p', getPort(), './examples/plugin.js'] @@ -25,30 +27,30 @@ t.beforeEach(async () => { spy = sinon.spy(fastify, 'close') }) -t.afterEach(async () => { +afterEach(async () => { sandbox.restore() }) -test('should add and remove SIGINT listener as expected ', async t => { +// Tests skip on win32 platforms due SIGINT signal is not supported across all windows platforms +test('should add and remove SIGINT listener as expected', { skip: isWindows }, async (t) => { t.plan(2) - - t.equal(process.listenerCount('SIGINT'), signalCounter + 1) - + const initialCount = process.listenerCount('SIGINT') + t.assert.strictEqual(initialCount, signalCounter + 1) await fastify.close() - - t.equal(process.listenerCount('SIGINT'), signalCounter) - - t.end() + t.assert.strictEqual(process.listenerCount('SIGINT'), signalCounter) }) -test('should have called fastify.close() when receives a SIGINT signal', async t => { - process.once('SIGINT', () => { - sinon.assert.called(spy) - - t.end() - - process.exit() - }) - +test('should call fastify.close() on SIGINT', { skip: isWindows || isMacOS || isLinux }, (t) => { + const sigintHandler = async () => { + try { + sinon.assert.called(spy) + t.assert.ok('fastify.close() was called on SIGINT') + } finally { + process.removeListener('SIGINT', sigintHandler) + await fastify.close() + } + } + + process.once('SIGINT', sigintHandler) process.kill(process.pid, 'SIGINT') })