Skip to content
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

feat: use built in node:test runner in all template test implementations #682

Merged
merged 38 commits into from
Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
cabce0f
feat: move template tests to node:test
big-kahuna-burger Nov 27, 2023
509e1d8
feat: remove tap dependency and fix tests
big-kahuna-burger Nov 27, 2023
1f46627
feat: remove tap dependency for typescript
big-kahuna-burger Nov 27, 2023
c158367
feat: remove tap in js/esm
big-kahuna-burger Nov 27, 2023
5e72f02
feat: remove tap in ts/esm
big-kahuna-burger Nov 27, 2023
322271c
feat: remove tap in plugin generator
big-kahuna-burger Nov 27, 2023
ed1ba7f
fix: node:test test script is generated
big-kahuna-burger Nov 27, 2023
861ef00
feat: tsx runner for native ts tests
big-kahuna-burger Nov 27, 2023
dbdeeb8
fix: teardown types
big-kahuna-burger Nov 27, 2023
67ca2f4
fix: removes tap in esm
big-kahuna-burger Nov 27, 2023
d86d755
fix: missing import
big-kahuna-burger Nov 27, 2023
9c1f7bb
fix: teardown
big-kahuna-burger Nov 27, 2023
e49ba62
fix: teardown esm
big-kahuna-burger Nov 27, 2023
b5e9ce6
fix: ts test script
big-kahuna-burger Nov 27, 2023
0521442
fix: all tests
big-kahuna-burger Nov 27, 2023
29d80b6
build: reorganized runners
big-kahuna-burger Nov 27, 2023
cd107a8
fix: cleanup
big-kahuna-burger Nov 27, 2023
3f431ff
chore: avoid posttest and assign template tests to the end but withou…
big-kahuna-burger Nov 27, 2023
1b263f3
fix: linted code
big-kahuna-burger Nov 27, 2023
65d4f2e
ci: bump workflow version to 4 to drop 14 and 16 runtimes from testing
big-kahuna-burger Nov 27, 2023
e63cf71
ci: empty commit to trigger workflow on a fork
big-kahuna-burger Nov 27, 2023
22bd314
ci: revert to v3 of reusable workflow
big-kahuna-burger Nov 27, 2023
85647fa
chore: fixes unit:ts-cjs script
big-kahuna-burger Nov 28, 2023
ee832d7
feat: implements skipping test suites for incompatible runtime versions
big-kahuna-burger Nov 28, 2023
5420275
ci: run templates on 20 only
big-kahuna-burger Nov 28, 2023
d8b03ca
fix: lint errs
big-kahuna-burger Nov 28, 2023
90512cf
add suites runner and correct skip logic (#1)
big-kahuna-burger Nov 28, 2023
3406ae2
ci: fix args expansion (#5)
big-kahuna-burger Nov 28, 2023
5914b35
fix: remove problematic c8 on ts-esm tpl
big-kahuna-burger Nov 28, 2023
d305d3f
drops c8 (#6)
big-kahuna-burger Nov 28, 2023
9fe5443
Merge branch 'master' into test-node-builtin
big-kahuna-burger Dec 2, 2023
891c677
test: swap positions
big-kahuna-burger Dec 2, 2023
56100dd
test: add timeout
big-kahuna-burger Dec 2, 2023
4522c26
fix: remove tsx dependency and use existing ts-node
big-kahuna-burger Dec 8, 2023
74a17bf
fix: remove extra conf
big-kahuna-burger Dec 8, 2023
085f5ac
feat(script): drop dependency to del-cli and use --noEmit flag with tsc
big-kahuna-burger Dec 8, 2023
25076f5
test: add compilation test for ts-esm template
big-kahuna-burger Dec 8, 2023
c440373
test: adds fault exit code to suite runner on test:fail event being e…
big-kahuna-burger Dec 9, 2023
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
1 change: 0 additions & 1 deletion generate-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const pluginTemplate = {
fastify: cliPkg.devDependencies.fastify,
'fastify-tsconfig': cliPkg.devDependencies['fastify-tsconfig'],
standard: cliPkg.devDependencies.standard,
tap: cliPkg.devDependencies.tap,
'ts-standard': cliPkg.devDependencies['ts-standard'],
tsd: cliPkg.devDependencies.tsd,
typescript: cliPkg.devDependencies.typescript
Expand Down
27 changes: 5 additions & 22 deletions generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const javascriptTemplate = {
dir: 'app',
main: 'app.js',
scripts: {
test: 'tap "test/**/*.test.js"',
test: 'node --test test/**/*.test.js',
start: 'fastify start -l info app.js',
dev: 'fastify start -w -l info -P app.js'
},
Expand All @@ -28,9 +28,7 @@ const javascriptTemplate = {
'@fastify/sensible': cliPkg.devDependencies['@fastify/sensible'],
'fastify-cli': '^' + cliPkg.version
},
devDependencies: {
tap: cliPkg.devDependencies.tap
},
devDependencies: {},
logInstructions: function (pkg) {
log('debug', 'saved package.json')
log('info', `project ${pkg.name} generated successfully`)
Expand All @@ -49,7 +47,7 @@ const typescriptTemplate = {
dir: 'app-ts',
main: 'app.ts',
scripts: {
test: 'npm run build:ts && tsc -p test/tsconfig.json && tap --ts "test/**/*.test.ts"',
test: 'npm run build:ts && tsc -p test/tsconfig.json && c8 node --test -r ts-node/register test/**/*.ts',
start: 'npm run build:ts && fastify start -l info dist/app.js',
'build:ts': 'tsc',
'watch:ts': 'tsc -w',
Expand All @@ -69,7 +67,6 @@ const typescriptTemplate = {
'ts-node': cliPkg.devDependencies['ts-node'],
concurrently: cliPkg.devDependencies.concurrently,
'fastify-tsconfig': cliPkg.devDependencies['fastify-tsconfig'],
tap: cliPkg.devDependencies.tap,
typescript: cliPkg.devDependencies.typescript
},
nodemonConfig: {
Expand Down Expand Up @@ -122,8 +119,6 @@ function generate (dir, template) {

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

pkg.tap = template.tap

log('debug', 'edited package.json, saving')
writeFile('package.json', JSON.stringify(pkg, null, 2), (err) => {
if (err) {
Expand Down Expand Up @@ -164,31 +159,19 @@ function cli (args) {
if (opts.esm) {
template.dir = 'app-ts-esm'
template.type = 'module'
template.tap = {
'node-arg': [
'--no-warnings',
'--experimental-loader',
'ts-node/esm'
],
coverage: false
}

// For coverage, NYC with Typescript ESM doesn't work https://github.com/tapjs/node-tap/issues/735
template.devDependencies.c8 = cliPkg.devDependencies.c8
template.scripts.test = 'npm run build:ts && tsc -p test/tsconfig.json && c8 tap --ts "test/**/*.test.ts"'
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'
}
} else {
template = { ...javascriptTemplate }

if (opts.esm) {
template.dir = 'app-esm'
template.type = 'module'
template.tap = {
coverage: false
}

template.devDependencies.c8 = cliPkg.devDependencies.c8
template.scripts.test = 'c8 tap "test/**/*.test.js"'
template.scripts.test = 'node --test test/**/*.test.js'
}

if (opts.standardlint) {
Expand Down
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@
"scripts": {
"lint": "standard",
"lint:fix": "standard --fix",
"unit:templates": "npm run unit:ts-esm && npm run unit:ts-cjs && npm run unit:esm && npm run unit:cjs",
"unit:cjs": "tap \"templates/app/**/*.test.js\" --no-coverage --timeout 400 --jobs 1 --color -R specy",
"unit:esm": "tap \"templates/app-esm/**/*.test.js\" --no-coverage --timeout 400 --jobs 1 --color -R specy",
"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",
"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",
"pretest": "xcopy /e /k /i . \"..\\node_modules\\fastify-cli\" || rsync -r --exclude=node_modules ./ node_modules/fastify-cli || echo 'this is fine'",
"test": "npm run unit:suites && c8 --clean npm run test:cli-and-typescript",
"unit:cjs": "node suite-runner.js \"templates/app/test/**/*.test.js\"",
"unit:esm": "node suite-runner.js \"templates/app-esm/test/**/*.test.js\"",
"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\"",
"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\"",
"unit:suites": "node should-skip-test-suites.js || npm run all-suites",
"all-suites": "npm run unit:cjs && npm run unit:esm && npm run unit:ts-cjs && npm run unit:ts-esm",
"unit:cli-js": "tap \"test/**/*.test.js\" --no-coverage --timeout 400 --jobs 1 --color -R specy",
"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",
"unit:cli": "npm run unit:cli-js && npm run unit:cli-ts",
"pretest": "xcopy /e /k /i . \"..\\node_modules\\fastify-cli\" || rsync -r --exclude=node_modules ./ node_modules/fastify-cli || echo 'this is fine'",
"test-no-coverage": "npm run unit:cli && npm run unit:templates && npm run test:typescript",
"test": "c8 --clean npm run test-no-coverage",
"test:typescript": "tsd templates/plugin -t ./../../index.d.ts && tsc --project templates/app-ts/tsconfig.json && del-cli templates/app-ts/dist"
"test:cli-and-typescript": "npm run unit:cli && npm run test:typescript",
"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"
},
"keywords": [
"fastify",
Expand Down Expand Up @@ -78,7 +79,6 @@
"c8": "^7.13.0",
"concurrently": "^8.2.2",
"cross-env": "^7.0.3",
"del-cli": "^3.0.1",
"fastify-tsconfig": "^2.0.0",
"minimatch": "^5.1.0",
"proxyquire": "^2.1.3",
Expand Down
7 changes: 7 additions & 0 deletions should-skip-test-suites.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const nodeMajorVersion = process.versions.node.split('.').map(x => parseInt(x, 10))[0]
const shouldRunSuites = nodeMajorVersion >= 20
if (!shouldRunSuites) {
console.info(`Skipped templates test suites on node ${nodeMajorVersion}`)
process.exit(0)
}
process.exit(1)
22 changes: 22 additions & 0 deletions suite-runner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { run } = require('node:test')
const { spec } = require('node:test/reporters')
const path = require('path')
const glob = require('glob')

const pattern = process.argv[process.argv.length - 1]

console.info(`Running tests matching ${pattern}`)
const timeout = 5 * 60 * 1000 // 5 minutes
glob(pattern, (err, matches) => {
if (err) {
console.error(err)
process.exit(1)
}
const resolved = matches.map(file => path.resolve(file))
const testRs = run({ files: resolved, timeout })
.on('test:fail', () => {
process.exitCode = 1
})
.compose(spec)
testRs.pipe(process.stdout)
})
2 changes: 1 addition & 1 deletion templates/app-esm/test/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async function build (t) {
const app = await helper.build(argv, config())

// tear down our app after we are done
t.teardown(app.close.bind(app))
t.after(() => app.close())

return app
}
Expand Down
7 changes: 4 additions & 3 deletions templates/app-esm/test/plugins/support.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { test } from 'tap'
import { test } from 'node:test'
import * as assert from 'node:assert'
import Fastify from 'fastify'
import Support from '../../plugins/support.js'

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

await fastify.ready()
t.equal(fastify.someSupport(), 'hugs')
assert.equal(fastify.someSupport(), 'hugs')
})

// You can also use plugin with opts in fastify v2
Expand All @@ -19,6 +20,6 @@ test('support works standalone', async (t) => {
//
// fastify.ready((err) => {
// t.error(err)
// t.equal(fastify.someSupport(), 'hugs')
// assert.equal(fastify.someSupport(), 'hugs')
// })
// })
5 changes: 3 additions & 2 deletions templates/app-esm/test/routes/example.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { test } from 'tap'
import { test } from 'node:test'
import * as assert from 'node:assert'
import { build } from '../helper.js'

test('example is loaded', async (t) => {
Expand All @@ -7,5 +8,5 @@ test('example is loaded', async (t) => {
const res = await app.inject({
url: '/example'
})
t.equal(res.payload, 'this is an example')
assert.equal(res.payload, 'this is an example')
})
5 changes: 3 additions & 2 deletions templates/app-esm/test/routes/root.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { test } from 'tap'
import { test } from 'node:test'
import * as assert from 'node:assert'
import { build } from '../helper.js'

test('default root route', async (t) => {
Expand All @@ -7,5 +8,5 @@ test('default root route', async (t) => {
const res = await app.inject({
url: '/'
})
t.same(JSON.parse(res.payload), { root: true })
assert.deepStrictEqual(JSON.parse(res.payload), { root: true })
})
13 changes: 7 additions & 6 deletions templates/app-ts-esm/test/helper.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// This file contains code that we reuse between our tests.
import helper from 'fastify-cli/helper.js'
import path from 'path'
import tap from 'tap';
import * as test from 'node:test'
import * as path from 'path'
import { fileURLToPath } from 'url'


export type Test = typeof tap['Test']['prototype'];
export type TestContext = {
after: typeof test.after
};

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

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

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

// Tear down our app after we are done
t.teardown(() => void app.close())
t.after(() => void app.close())

return app
}
Expand Down
5 changes: 3 additions & 2 deletions templates/app-ts-esm/test/plugins/support.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { test } from 'tap'
import { test } from 'node:test'
import * as assert from 'node:assert'
import Fastify from 'fastify'
import Support from '../../src/plugins/support.js'

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

t.equal(fastify.someSupport(), 'hugs')
assert.equal(fastify.someSupport(), 'hugs')
})
5 changes: 3 additions & 2 deletions templates/app-ts-esm/test/routes/example.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { test } from 'tap'
import { test } from 'node:test'
import * as assert from 'node:assert'
import { build } from '../helper.js'

test('example is loaded', async (t) => {
Expand All @@ -8,5 +9,5 @@ test('example is loaded', async (t) => {
url: '/example'
})

t.equal(res.payload, 'this is an example')
assert.equal(res.payload, 'this is an example')
})
5 changes: 3 additions & 2 deletions templates/app-ts-esm/test/routes/root.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { test } from 'tap'
import { test } from 'node:test'
import * as assert from 'node:assert'
import { build } from '../helper.js'

test('default root route', async (t) => {
Expand All @@ -7,5 +8,5 @@ test('default root route', async (t) => {
const res = await app.inject({
url: '/'
})
t.same(JSON.parse(res.payload), { root: true })
assert.deepStrictEqual(JSON.parse(res.payload), { root: true })
})
10 changes: 6 additions & 4 deletions templates/app-ts/test/helper.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// This file contains code that we reuse between our tests.
const helper = require('fastify-cli/helper.js')
import * as path from 'path'
import * as tap from 'tap';
import * as test from 'node:test'

export type Test = typeof tap['Test']['prototype'];
export type TestContext = {
after: typeof test.after
};

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

Expand All @@ -14,7 +16,7 @@ async function config () {
}

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

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

// Tear down our app after we are done
t.teardown(() => void app.close())
t.after(() => void app.close())

return app
}
Expand Down
6 changes: 4 additions & 2 deletions templates/app-ts/test/plugins/support.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { test } from 'tap'
import { test } from 'node:test'
import * as assert from 'node:assert'

import Fastify from 'fastify'
import Support from '../../src/plugins/support'

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

t.equal(fastify.someSupport(), 'hugs')
assert.equal(fastify.someSupport(), 'hugs')
})
5 changes: 3 additions & 2 deletions templates/app-ts/test/routes/example.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { test } from 'tap'
import { test } from 'node:test'
import * as assert from 'node:assert'
import { build } from '../helper'

test('example is loaded', async (t) => {
Expand All @@ -8,5 +9,5 @@ test('example is loaded', async (t) => {
url: '/example'
})

t.equal(res.payload, 'this is an example')
assert.equal(res.payload, 'this is an example')
})
5 changes: 3 additions & 2 deletions templates/app-ts/test/routes/root.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { test } from 'tap'
import { test } from 'node:test'
import * as assert from 'node:assert'
import { build } from '../helper'

test('default root route', async (t) => {
Expand All @@ -7,5 +8,5 @@ test('default root route', async (t) => {
const res = await app.inject({
url: '/'
})
t.same(JSON.parse(res.payload), { root: true })
assert.deepStrictEqual(JSON.parse(res.payload), { root: true })
})
4 changes: 2 additions & 2 deletions templates/app/test/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ async function build (t) {
// different from the production setup
const app = await buildApplication(argv, config())

// tear down our app after we are done
t.teardown(app.close.bind(app))
// close the app after we are done
t.after(() => app.close())

return app
}
Expand Down
Loading