-
-
Notifications
You must be signed in to change notification settings - Fork 173
test: migrated graceful-shutdown.test.js from tap to node:test #825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
61c443c
ea9ec29
e6a43cf
42b7cbf
5b7f942
8a2e473
0e5cef3
239ccdb
669819e
b8d7b84
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should check this with a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Plan hasn't been inserted. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} finally { | ||
process.exit() // Clean exit | ||
} | ||
} | ||
|
||
process.once('SIGINT', sigintHandler) | ||
process.kill(process.pid, 'SIGINT') | ||
}) |
Uh oh!
There was an error while loading. Please reload this page.