Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions test/graceful-shutdown.test.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
'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 assert = require('assert')
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
const isWindows = process.platform === 'win32'
const conditionalTest = isWindows ? test.skip : test

beforeEach(async () => {
signalCounter = process.listenerCount('SIGINT')

const argv = ['-p', getPort(), './examples/plugin.js']
fastify = await start.start(argv)
spy = sinon.spy(fastify, 'close')
})

t.afterEach(async () => {
afterEach(async () => {
sandbox.restore()
})

test('should add and remove SIGINT listener as expected ', async t => {
t.plan(2)

t.equal(process.listenerCount('SIGINT'), signalCounter + 1)
conditionalTest('should add and remove SIGINT listener as expected', async (t) => {
assert.strictEqual(process.listenerCount('SIGINT'), signalCounter + 1)

await fastify.close()

t.equal(process.listenerCount('SIGINT'), signalCounter)

t.end()
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()
})

conditionalTest('should call fastify.close() on SIGINT', async (t) => {
const sigintHandler = () => {
try {
sinon.assert.called(spy)
t.pass('fastify.close() was called on SIGINT')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should check this with a t.plan.
Especially because t.pass is not part of the node test runner, so this test should fail

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Plan hasn't been inserted.
The assertion could be unchecked and the test would still pass.

Copy link
Contributor Author

@Tony133 Tony133 Aug 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test fails if I put the method "plan"
screenshot-error-plan

will probably fail again the CI

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of being async, you should end the test once the assertion has been checked
Contrary to tap, the test runner doesn't wait for the plan to complete

} finally {
process.exit() // Clean exit
}
}

process.once('SIGINT', sigintHandler)
process.kill(process.pid, 'SIGINT')
})
Loading