Skip to content

Commit

Permalink
test: migrate from tap to node:test and c8 (#89)
Browse files Browse the repository at this point in the history
* test: migrate from tap to node:test and c8

* fix: lint errors
  • Loading branch information
dancastillo authored Jan 9, 2025
1 parent 19a7f1a commit 63eac3c
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 129 deletions.
2 changes: 0 additions & 2 deletions .taprc

This file was deleted.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"lint:fix": "eslint --fix",
"test": "npm run test:unit && npm run test:typescript",
"test:typescript": "tsd",
"test:unit": "tap"
"test:unit": "c8 --100 node --test"
},
"types": "types/index.d.ts",
"repository": {
Expand Down Expand Up @@ -60,10 +60,10 @@
"devDependencies": {
"@fastify/pre-commit": "^2.1.0",
"@types/node": "^22.0.0",
"c8": "^10.1.3",
"eslint": "^9.17.0",
"neostandard": "^0.12.0",
"split2": "^4.2.0",
"tap": "^18.7.2",
"tsd": "^0.31.0",
"undici": "^7.0.0"
},
Expand Down
62 changes: 30 additions & 32 deletions test/hooks.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
'use strict'

const t = require('tap')
const { afterEach, test } = require('node:test')
const { restartable } = require('..')

const test = t.test
t.jobs = 1
t.afterEach(async () => {
afterEach(async () => {
await new Promise((resolve) => setTimeout(resolve, 10))
})

Expand All @@ -20,20 +18,20 @@ test('should trigger preRestartHook', async (t) => {
keepAliveTimeout: 1
})

t.teardown(async () => {
t.after(async () => {
await app.close()
})

const expectedRestartOptions = { foo: 'bar' }

app.addPreRestartHook(async (app, restartOptions) => {
t.equal(app.restarted, false)
t.same(restartOptions, expectedRestartOptions)
t.assert.strictEqual(app.restarted, false)
t.assert.deepStrictEqual(restartOptions, expectedRestartOptions)
})

app.addPreRestartHook(async (app, restartOptions) => {
t.equal(app.restarted, false)
t.same(restartOptions, expectedRestartOptions)
t.assert.strictEqual(app.restarted, false)
t.assert.deepStrictEqual(restartOptions, expectedRestartOptions)
})

await app.restart(expectedRestartOptions)
Expand All @@ -50,7 +48,7 @@ test('should not fail preRestartHook throw an error', async (t) => {
keepAliveTimeout: 1
})

t.teardown(async () => {
t.after(async () => {
await app.close()
})

Expand All @@ -61,13 +59,13 @@ test('should not fail preRestartHook throw an error', async (t) => {
})

app.addPreRestartHook(async (app, restartOptions) => {
t.equal(app.restarted, false)
t.same(restartOptions, expectedRestartOptions)
t.assert.strictEqual(app.restarted, false)
t.assert.deepStrictEqual(restartOptions, expectedRestartOptions)
})

await app.restart(expectedRestartOptions)

t.equal(app.restarted, true)
t.assert.strictEqual(app.restarted, true)
})

test('should throw if preRestartHook is not a function', async (t) => {
Expand All @@ -81,13 +79,13 @@ test('should throw if preRestartHook is not a function', async (t) => {
keepAliveTimeout: 1
})

t.teardown(async () => {
t.after(async () => {
await app.close()
})

t.throws(() => {
t.assert.throws(() => {
app.addPreRestartHook('not a function')
}, 'preRestartHook must be a function')
}, { message: 'The hook must be a function' })
})

test('should trigger onRestartHook', async (t) => {
Expand All @@ -101,20 +99,20 @@ test('should trigger onRestartHook', async (t) => {
keepAliveTimeout: 1
})

t.teardown(async () => {
t.after(async () => {
await app.close()
})

const expectedRestartOptions = { foo: 'bar' }

app.addOnRestartHook(async (app, restartOptions) => {
t.equal(app.restarted, true)
t.same(restartOptions, expectedRestartOptions)
t.assert.strictEqual(app.restarted, true)
t.assert.deepStrictEqual(restartOptions, expectedRestartOptions)
})

app.addOnRestartHook(async (app, restartOptions) => {
t.equal(app.restarted, true)
t.same(restartOptions, expectedRestartOptions)
t.assert.strictEqual(app.restarted, true)
t.assert.deepStrictEqual(restartOptions, expectedRestartOptions)
})

await app.restart(expectedRestartOptions)
Expand All @@ -131,7 +129,7 @@ test('should not fail onRestartHook throw an error', async (t) => {
keepAliveTimeout: 1
})

t.teardown(async () => {
t.after(async () => {
await app.close()
})

Expand All @@ -142,13 +140,13 @@ test('should not fail onRestartHook throw an error', async (t) => {
})

app.addOnRestartHook(async (app, restartOptions) => {
t.equal(app.restarted, true)
t.same(restartOptions, expectedRestartOptions)
t.assert.strictEqual(app.restarted, true)
t.assert.deepStrictEqual(restartOptions, expectedRestartOptions)
})

await app.restart(expectedRestartOptions)

t.equal(app.restarted, true)
t.assert.strictEqual(app.restarted, true)
})

test('should throw if onRestartHook is not a function', async (t) => {
Expand All @@ -162,13 +160,13 @@ test('should throw if onRestartHook is not a function', async (t) => {
keepAliveTimeout: 1
})

t.teardown(async () => {
t.after(async () => {
await app.close()
})

t.throws(() => {
t.assert.throws(() => {
app.addOnRestartHook('not a function')
}, 'onRestartHook must be a function')
}, { message: 'The hook must be a function' })
})

test('should not throw if onRestartHook is a sync function', async (t) => {
Expand All @@ -182,18 +180,18 @@ test('should not throw if onRestartHook is a sync function', async (t) => {
keepAliveTimeout: 1
})

t.teardown(async () => {
t.after(async () => {
await app.close()
})

const expectedRestartOptions = { foo: 'bar' }

app.addOnRestartHook((app, restartOptions) => {
t.equal(app.restarted, true)
t.same(restartOptions, expectedRestartOptions)
t.assert.strictEqual(app.restarted, true)
t.assert.deepStrictEqual(restartOptions, expectedRestartOptions)
})

await app.restart(expectedRestartOptions)

t.equal(app.restarted, true)
t.assert.strictEqual(app.restarted, true)
})
Loading

0 comments on commit 63eac3c

Please sign in to comment.