Skip to content

Commit 196a9fb

Browse files
feat: use built in node:test runner in all template test implementations (#682)
1 parent 957bd8d commit 196a9fb

27 files changed

+124
-102
lines changed

generate-plugin.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ const pluginTemplate = {
3333
fastify: cliPkg.devDependencies.fastify,
3434
'fastify-tsconfig': cliPkg.devDependencies['fastify-tsconfig'],
3535
standard: cliPkg.devDependencies.standard,
36-
tap: cliPkg.devDependencies.tap,
3736
'ts-standard': cliPkg.devDependencies['ts-standard'],
3837
tsd: cliPkg.devDependencies.tsd,
3938
typescript: cliPkg.devDependencies.typescript

generate.js

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const javascriptTemplate = {
1717
dir: 'app',
1818
main: 'app.js',
1919
scripts: {
20-
test: 'tap "test/**/*.test.js"',
20+
test: 'node --test test/**/*.test.js',
2121
start: 'fastify start -l info app.js',
2222
dev: 'fastify start -w -l info -P app.js'
2323
},
@@ -28,9 +28,7 @@ const javascriptTemplate = {
2828
'@fastify/sensible': cliPkg.devDependencies['@fastify/sensible'],
2929
'fastify-cli': '^' + cliPkg.version
3030
},
31-
devDependencies: {
32-
tap: cliPkg.devDependencies.tap
33-
},
31+
devDependencies: {},
3432
logInstructions: function (pkg) {
3533
log('debug', 'saved package.json')
3634
log('info', `project ${pkg.name} generated successfully`)
@@ -49,7 +47,7 @@ const typescriptTemplate = {
4947
dir: 'app-ts',
5048
main: 'app.ts',
5149
scripts: {
52-
test: 'npm run build:ts && tsc -p test/tsconfig.json && tap --ts "test/**/*.test.ts"',
50+
test: 'npm run build:ts && tsc -p test/tsconfig.json && c8 node --test -r ts-node/register test/**/*.ts',
5351
start: 'npm run build:ts && fastify start -l info dist/app.js',
5452
'build:ts': 'tsc',
5553
'watch:ts': 'tsc -w',
@@ -69,7 +67,6 @@ const typescriptTemplate = {
6967
'ts-node': cliPkg.devDependencies['ts-node'],
7068
concurrently: cliPkg.devDependencies.concurrently,
7169
'fastify-tsconfig': cliPkg.devDependencies['fastify-tsconfig'],
72-
tap: cliPkg.devDependencies.tap,
7370
typescript: cliPkg.devDependencies.typescript
7471
},
7572
nodemonConfig: {
@@ -122,8 +119,6 @@ function generate (dir, template) {
122119

123120
pkg.devDependencies = Object.assign(pkg.devDependencies || {}, template.devDependencies)
124121

125-
pkg.tap = template.tap
126-
127122
log('debug', 'edited package.json, saving')
128123
writeFile('package.json', JSON.stringify(pkg, null, 2), (err) => {
129124
if (err) {
@@ -164,31 +159,19 @@ function cli (args) {
164159
if (opts.esm) {
165160
template.dir = 'app-ts-esm'
166161
template.type = 'module'
167-
template.tap = {
168-
'node-arg': [
169-
'--no-warnings',
170-
'--experimental-loader',
171-
'ts-node/esm'
172-
],
173-
coverage: false
174-
}
175162

176-
// For coverage, NYC with Typescript ESM doesn't work https://github.com/tapjs/node-tap/issues/735
177163
template.devDependencies.c8 = cliPkg.devDependencies.c8
178-
template.scripts.test = 'npm run build:ts && tsc -p test/tsconfig.json && c8 tap --ts "test/**/*.test.ts"'
164+
template.scripts.test = 'npm run build:ts && tsc -p test/tsconfig.json && FASTIFY_AUTOLOAD_TYPESCRIPT=1 node --test --experimental-test-coverage --loader ts-node/esm test/**/*.ts'
179165
}
180166
} else {
181167
template = { ...javascriptTemplate }
182168

183169
if (opts.esm) {
184170
template.dir = 'app-esm'
185171
template.type = 'module'
186-
template.tap = {
187-
coverage: false
188-
}
189172

190173
template.devDependencies.c8 = cliPkg.devDependencies.c8
191-
template.scripts.test = 'c8 tap "test/**/*.test.js"'
174+
template.scripts.test = 'node --test test/**/*.test.js'
192175
}
193176

194177
if (opts.standardlint) {

package.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,19 @@
1010
"scripts": {
1111
"lint": "standard",
1212
"lint:fix": "standard --fix",
13-
"unit:templates": "npm run unit:ts-esm && npm run unit:ts-cjs && npm run unit:esm && npm run unit:cjs",
14-
"unit:cjs": "tap \"templates/app/**/*.test.js\" --no-coverage --timeout 400 --jobs 1 --color -R specy",
15-
"unit:esm": "tap \"templates/app-esm/**/*.test.js\" --no-coverage --timeout 400 --jobs 1 --color -R specy",
16-
"unit:ts-cjs": "cross-env TS_NODE_PROJECT=./test/configs/ts-cjs.tsconfig.json tap \"templates/app-ts/**/*.test.ts\" --no-coverage --timeout 400 --jobs 1 --color -R specy",
17-
"unit:ts-esm": "cross-env TS_NODE_PROJECT=./test/configs/ts-esm.tsconfig.json tap \"templates/app-ts-esm/test/**/*.test.ts\" --no-coverage --node-arg=--loader=ts-node/esm --timeout 400 --jobs 1 --color -R specy",
13+
"pretest": "xcopy /e /k /i . \"..\\node_modules\\fastify-cli\" || rsync -r --exclude=node_modules ./ node_modules/fastify-cli || echo 'this is fine'",
14+
"test": "npm run unit:suites && c8 --clean npm run test:cli-and-typescript",
15+
"unit:cjs": "node suite-runner.js \"templates/app/test/**/*.test.js\"",
16+
"unit:esm": "node suite-runner.js \"templates/app-esm/test/**/*.test.js\"",
17+
"unit:ts-cjs": "cross-env TS_NODE_PROJECT=./test/configs/ts-cjs.tsconfig.json node -r ts-node/register suite-runner.js \"templates/app-ts/test/**/*.test.ts\"",
18+
"unit:ts-esm": "cross-env TS_NODE_PROJECT=./test/configs/ts-esm.tsconfig.json FASTIFY_AUTOLOAD_TYPESCRIPT=1 node -r ts-node/register --loader ts-node/esm suite-runner.js \"templates/app-ts-esm/test/**/*.test.ts\"",
19+
"unit:suites": "node should-skip-test-suites.js || npm run all-suites",
20+
"all-suites": "npm run unit:cjs && npm run unit:esm && npm run unit:ts-cjs && npm run unit:ts-esm",
1821
"unit:cli-js": "tap \"test/**/*.test.js\" --no-coverage --timeout 400 --jobs 1 --color -R specy",
1922
"unit:cli-ts": "cross-env TS_NODE_PROJECT=./test/configs/ts-cjs.tsconfig.json tap \"test/**/*.test.ts\" --no-coverage --timeout 400 --jobs 1 --color -R specy",
2023
"unit:cli": "npm run unit:cli-js && npm run unit:cli-ts",
21-
"pretest": "xcopy /e /k /i . \"..\\node_modules\\fastify-cli\" || rsync -r --exclude=node_modules ./ node_modules/fastify-cli || echo 'this is fine'",
22-
"test-no-coverage": "npm run unit:cli && npm run unit:templates && npm run test:typescript",
23-
"test": "c8 --clean npm run test-no-coverage",
24-
"test:typescript": "tsd templates/plugin -t ./../../index.d.ts && tsc --project templates/app-ts/tsconfig.json && del-cli templates/app-ts/dist"
24+
"test:cli-and-typescript": "npm run unit:cli && npm run test:typescript",
25+
"test:typescript": "tsd templates/plugin -t ./../../index.d.ts && tsc --project templates/app-ts/tsconfig.json --noEmit && tsc --project templates/app-ts-esm/tsconfig.json --noEmit"
2526
},
2627
"keywords": [
2728
"fastify",
@@ -78,7 +79,6 @@
7879
"c8": "^7.13.0",
7980
"concurrently": "^8.2.2",
8081
"cross-env": "^7.0.3",
81-
"del-cli": "^3.0.1",
8282
"fastify-tsconfig": "^2.0.0",
8383
"minimatch": "^5.1.0",
8484
"proxyquire": "^2.1.3",

should-skip-test-suites.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const nodeMajorVersion = process.versions.node.split('.').map(x => parseInt(x, 10))[0]
2+
const shouldRunSuites = nodeMajorVersion >= 20
3+
if (!shouldRunSuites) {
4+
console.info(`Skipped templates test suites on node ${nodeMajorVersion}`)
5+
process.exit(0)
6+
}
7+
process.exit(1)

suite-runner.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const { run } = require('node:test')
2+
const { spec } = require('node:test/reporters')
3+
const path = require('path')
4+
const glob = require('glob')
5+
6+
const pattern = process.argv[process.argv.length - 1]
7+
8+
console.info(`Running tests matching ${pattern}`)
9+
const timeout = 5 * 60 * 1000 // 5 minutes
10+
glob(pattern, (err, matches) => {
11+
if (err) {
12+
console.error(err)
13+
process.exit(1)
14+
}
15+
const resolved = matches.map(file => path.resolve(file))
16+
const testRs = run({ files: resolved, timeout })
17+
.on('test:fail', () => {
18+
process.exitCode = 1
19+
})
20+
.compose(spec)
21+
testRs.pipe(process.stdout)
22+
})

templates/app-esm/test/helper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ async function build (t) {
2626
const app = await helper.build(argv, config())
2727

2828
// tear down our app after we are done
29-
t.teardown(app.close.bind(app))
29+
t.after(() => app.close())
3030

3131
return app
3232
}

templates/app-esm/test/plugins/support.test.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { test } from 'tap'
1+
import { test } from 'node:test'
2+
import * as assert from 'node:assert'
23
import Fastify from 'fastify'
34
import Support from '../../plugins/support.js'
45

@@ -7,7 +8,7 @@ test('support works standalone', async (t) => {
78
fastify.register(Support)
89

910
await fastify.ready()
10-
t.equal(fastify.someSupport(), 'hugs')
11+
assert.equal(fastify.someSupport(), 'hugs')
1112
})
1213

1314
// You can also use plugin with opts in fastify v2
@@ -19,6 +20,6 @@ test('support works standalone', async (t) => {
1920
//
2021
// fastify.ready((err) => {
2122
// t.error(err)
22-
// t.equal(fastify.someSupport(), 'hugs')
23+
// assert.equal(fastify.someSupport(), 'hugs')
2324
// })
2425
// })
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { test } from 'tap'
1+
import { test } from 'node:test'
2+
import * as assert from 'node:assert'
23
import { build } from '../helper.js'
34

45
test('example is loaded', async (t) => {
@@ -7,5 +8,5 @@ test('example is loaded', async (t) => {
78
const res = await app.inject({
89
url: '/example'
910
})
10-
t.equal(res.payload, 'this is an example')
11+
assert.equal(res.payload, 'this is an example')
1112
})
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { test } from 'tap'
1+
import { test } from 'node:test'
2+
import * as assert from 'node:assert'
23
import { build } from '../helper.js'
34

45
test('default root route', async (t) => {
@@ -7,5 +8,5 @@ test('default root route', async (t) => {
78
const res = await app.inject({
89
url: '/'
910
})
10-
t.same(JSON.parse(res.payload), { root: true })
11+
assert.deepStrictEqual(JSON.parse(res.payload), { root: true })
1112
})

templates/app-ts-esm/test/helper.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
// This file contains code that we reuse between our tests.
22
import helper from 'fastify-cli/helper.js'
3-
import path from 'path'
4-
import tap from 'tap';
3+
import * as test from 'node:test'
4+
import * as path from 'path'
55
import { fileURLToPath } from 'url'
66

7-
8-
export type Test = typeof tap['Test']['prototype'];
7+
export type TestContext = {
8+
after: typeof test.after
9+
};
910

1011
const __filename = fileURLToPath(import.meta.url)
1112
const __dirname = path.dirname(__filename)
@@ -18,7 +19,7 @@ async function config () {
1819
}
1920

2021
// Automatically build and tear down our instance
21-
async function build (t: Test) {
22+
async function build (t: TestContext) {
2223
// you can set all the options supported by the fastify CLI command
2324
const argv = [AppPath]
2425

@@ -28,7 +29,7 @@ async function build (t: Test) {
2829
const app = await helper.build(argv, await config())
2930

3031
// Tear down our app after we are done
31-
t.teardown(() => void app.close())
32+
t.after(() => void app.close())
3233

3334
return app
3435
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { test } from 'tap'
1+
import { test } from 'node:test'
2+
import * as assert from 'node:assert'
23
import Fastify from 'fastify'
34
import Support from '../../src/plugins/support.js'
45

@@ -7,5 +8,5 @@ test('support works standalone', async (t) => {
78
void fastify.register(Support)
89
await fastify.ready()
910

10-
t.equal(fastify.someSupport(), 'hugs')
11+
assert.equal(fastify.someSupport(), 'hugs')
1112
})
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { test } from 'tap'
1+
import { test } from 'node:test'
2+
import * as assert from 'node:assert'
23
import { build } from '../helper.js'
34

45
test('example is loaded', async (t) => {
@@ -8,5 +9,5 @@ test('example is loaded', async (t) => {
89
url: '/example'
910
})
1011

11-
t.equal(res.payload, 'this is an example')
12+
assert.equal(res.payload, 'this is an example')
1213
})
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { test } from 'tap'
1+
import { test } from 'node:test'
2+
import * as assert from 'node:assert'
23
import { build } from '../helper.js'
34

45
test('default root route', async (t) => {
@@ -7,5 +8,5 @@ test('default root route', async (t) => {
78
const res = await app.inject({
89
url: '/'
910
})
10-
t.same(JSON.parse(res.payload), { root: true })
11+
assert.deepStrictEqual(JSON.parse(res.payload), { root: true })
1112
})

templates/app-ts/test/helper.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// This file contains code that we reuse between our tests.
22
const helper = require('fastify-cli/helper.js')
33
import * as path from 'path'
4-
import * as tap from 'tap';
4+
import * as test from 'node:test'
55

6-
export type Test = typeof tap['Test']['prototype'];
6+
export type TestContext = {
7+
after: typeof test.after
8+
};
79

810
const AppPath = path.join(__dirname, '..', 'src', 'app.ts')
911

@@ -14,7 +16,7 @@ async function config () {
1416
}
1517

1618
// Automatically build and tear down our instance
17-
async function build (t: Test) {
19+
async function build (t: TestContext) {
1820
// you can set all the options supported by the fastify CLI command
1921
const argv = [AppPath]
2022

@@ -24,7 +26,7 @@ async function build (t: Test) {
2426
const app = await helper.build(argv, await config())
2527

2628
// Tear down our app after we are done
27-
t.teardown(() => void app.close())
29+
t.after(() => void app.close())
2830

2931
return app
3032
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { test } from 'tap'
1+
import { test } from 'node:test'
2+
import * as assert from 'node:assert'
3+
24
import Fastify from 'fastify'
35
import Support from '../../src/plugins/support'
46

@@ -7,5 +9,5 @@ test('support works standalone', async (t) => {
79
void fastify.register(Support)
810
await fastify.ready()
911

10-
t.equal(fastify.someSupport(), 'hugs')
12+
assert.equal(fastify.someSupport(), 'hugs')
1113
})
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { test } from 'tap'
1+
import { test } from 'node:test'
2+
import * as assert from 'node:assert'
23
import { build } from '../helper'
34

45
test('example is loaded', async (t) => {
@@ -8,5 +9,5 @@ test('example is loaded', async (t) => {
89
url: '/example'
910
})
1011

11-
t.equal(res.payload, 'this is an example')
12+
assert.equal(res.payload, 'this is an example')
1213
})
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { test } from 'tap'
1+
import { test } from 'node:test'
2+
import * as assert from 'node:assert'
23
import { build } from '../helper'
34

45
test('default root route', async (t) => {
@@ -7,5 +8,5 @@ test('default root route', async (t) => {
78
const res = await app.inject({
89
url: '/'
910
})
10-
t.same(JSON.parse(res.payload), { root: true })
11+
assert.deepStrictEqual(JSON.parse(res.payload), { root: true })
1112
})

templates/app/test/helper.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ async function build (t) {
2323
// different from the production setup
2424
const app = await buildApplication(argv, config())
2525

26-
// tear down our app after we are done
27-
t.teardown(app.close.bind(app))
26+
// close the app after we are done
27+
t.after(() => app.close())
2828

2929
return app
3030
}

0 commit comments

Comments
 (0)