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

fix: migrate from tap to node test and c8 #248

Merged
merged 4 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 0 additions & 4 deletions .taprc

This file was deleted.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"scripts": {
"lint": "standard",
"test": "npm run test:unit && npm run test:typescript",
"test:unit": "tap",
"test:unit": "node --test",
nimesh0505 marked this conversation as resolved.
Show resolved Hide resolved
"test:coverage": "c8 node --test && c8 report --reporter=html",
"test:typescript": "tsd"
},
"repository": {
Expand All @@ -30,10 +31,10 @@
"@fastify/pre-commit": "^2.1.0",
"@fastify/type-provider-typebox": "^5.0.0-pre.fv5.1",
"@types/node": "^22.0.0",
"c8": "^10.1.2",
nimesh0505 marked this conversation as resolved.
Show resolved Hide resolved
"fastify": "^5.0.0-alpha.2",
"proxyquire": "^2.1.3",
"standard": "^17.1.0",
"tap": "^18.7.0",
"tsd": "^0.31.0"
}
}
48 changes: 19 additions & 29 deletions test/bundlers.test.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,35 @@
'use strict'

const { test } = require('tap')
const { test } = require('node:test')
const fp = require('../plugin')

test('webpack removes require.main.filename', (t) => {
test('webpack removes require.main.filename', async (t) => {
Copy link
Member

Choose a reason for hiding this comment

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

Why have all of the test function been refactored to async functions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jsumners Thank you for your question. You are right to notice that all test functions have been converted to async functions. While it's not strictly necessary for the migration from tap to node:test, I chose to do so due to these reasons:

  1. Consistency - It provides a uniform pattern across all tests.
  2. Future-proofing - It allows for easier integration of async operations in the future if needed.
  3. Minimal impact: The change doesn't negatively affect the tests' functionality or performance.

However, if the team prefers to keep the tests as close to the original implementation as possible, we can certainly revert this change and only use async functions where necessary.

Copy link
Member

Choose a reason for hiding this comment

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

Unless there is a need for the functions to be async, please do not make them so.

const filename = require.main.filename
const info = console.info
t.teardown(() => {
t.after(() => {
require.main.filename = filename
console.info = info
})

require.main.filename = null

console.info = function (msg) {
t.fail('logged: ' + msg)
t.assert.fail('logged: ' + msg)
}

fp((fastify, opts, next) => {
next()
}, {
fastify: '^5.0.0'
})

t.end()
})

test('support faux modules', (t) => {
test('support faux modules', async (t) => {
const plugin = fp((fastify, opts, next) => {
next()
})

t.equal(plugin.default, plugin)
t.end()
t.assert.strictEqual(plugin.default, plugin)
})

test('support faux modules does not override existing default field in babel module', (t) => {
Expand All @@ -44,66 +41,60 @@ test('support faux modules does not override existing default field in babel mod

const plugin = fp(module)

t.equal(plugin.default, 'Existing default field')
t.end()
t.assert.strictEqual(plugin.default, 'Existing default field')
})

test('support ts named imports', (t) => {
test('support ts named imports', async (t) => {
const plugin = fp((fastify, opts, next) => {
next()
}, {
name: 'hello'
})

t.equal(plugin.hello, plugin)
t.end()
t.assert.strictEqual(plugin.hello, plugin)
})

test('from kebab-case to camelCase', (t) => {
test('from kebab-case to camelCase', async (t) => {
const plugin = fp((fastify, opts, next) => {
next()
}, {
name: 'hello-world'
})

t.equal(plugin.helloWorld, plugin)
t.end()
t.assert.strictEqual(plugin.helloWorld, plugin)
})

test('from @-prefixed named imports', (t) => {
test('from @-prefixed named imports', async (t) => {
const plugin = fp((fastify, opts, next) => {
next()
}, {
name: '@hello/world'
})

t.equal(plugin.helloWorld, plugin)
t.end()
t.assert.strictEqual(plugin.helloWorld, plugin)
})

test('from @-prefixed named kebab-case to camelCase', (t) => {
test('from @-prefixed named kebab-case to camelCase', async (t) => {
const plugin = fp((fastify, opts, next) => {
next()
}, {
name: '@hello/my-world'
})

t.equal(plugin.helloMyWorld, plugin)
t.end()
t.assert.strictEqual(plugin.helloMyWorld, plugin)
})

test('from kebab-case to camelCase multiple words', (t) => {
test('from kebab-case to camelCase multiple words', async (t) => {
const plugin = fp((fastify, opts, next) => {
next()
}, {
name: 'hello-long-world'
})

t.equal(plugin.helloLongWorld, plugin)
t.end()
t.assert.strictEqual(plugin.helloLongWorld, plugin)
})

test('from kebab-case to camelCase multiple words does not override', (t) => {
test('from kebab-case to camelCase multiple words does not override', async (t) => {
const fn = (fastify, opts, next) => {
next()
}
Expand All @@ -115,6 +106,5 @@ test('from kebab-case to camelCase multiple words does not override', (t) => {
name: 'hello-long-world'
})

t.equal(plugin.helloLongWorld, foobar)
t.end()
t.assert.strictEqual(plugin.helloLongWorld, foobar)
})
22 changes: 8 additions & 14 deletions test/checkVersion.test.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,52 @@
'use strict'

const { test } = require('tap')
const { test } = require('node:test')
const fp = require('../plugin')

test('checkVersion having require.main.filename', (t) => {
const info = console.info
t.ok(require.main.filename)
t.teardown(() => {
t.assert.ok(require.main.filename)
t.after(() => {
console.info = info
})

console.info = function (msg) {
t.fail('logged: ' + msg)
t.assert.fail('logged: ' + msg)
}

fp((fastify, opts, next) => {
next()
}, {
fastify: '^5.0.0'
})

t.end()
})

test('checkVersion having no require.main.filename but process.argv[1]', (t) => {
const filename = require.main.filename
const info = console.info
t.teardown(() => {
t.after(() => {
require.main.filename = filename
console.info = info
})

require.main.filename = null

console.info = function (msg) {
t.fail('logged: ' + msg)
t.assert.fail('logged: ' + msg)
}

fp((fastify, opts, next) => {
next()
}, {
fastify: '^5.0.0'
})

t.end()
})

test('checkVersion having no require.main.filename and no process.argv[1]', (t) => {
const filename = require.main.filename
const argv = process.argv
const info = console.info
t.teardown(() => {
t.after(() => {
require.main.filename = filename
process.argv = argv
console.info = info
Expand All @@ -60,14 +56,12 @@ test('checkVersion having no require.main.filename and no process.argv[1]', (t)
process.argv[1] = null

console.info = function (msg) {
t.fail('logged: ' + msg)
t.assert.fail('logged: ' + msg)
}

fp((fastify, opts, next) => {
next()
}, {
fastify: '^5.0.0'
})

t.end()
})
10 changes: 4 additions & 6 deletions test/composite.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
'use strict'

const t = require('tap')
const test = t.test
const { test } = require('node:test')
const fp = require('../plugin')

test('anonymous function should be named composite.test0', t => {
test('anonymous function should be named composite.test0', async (t) => {
t.plan(2)

const fn = fp((fastify, opts, next) => {
next()
})

t.equal(fn[Symbol.for('plugin-meta')].name, 'composite.test-auto-0')
t.equal(fn[Symbol.for('fastify.display-name')], 'composite.test-auto-0')
t.assert.strictEqual(fn[Symbol.for('plugin-meta')].name, 'composite.test-auto-0')
t.assert.strictEqual(fn[Symbol.for('fastify.display-name')], 'composite.test-auto-0')
})
13 changes: 7 additions & 6 deletions test/extractPluginName.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const t = require('tap')
const { test } = require('node:test')
const extractPluginName = require('../lib/getPluginName').extractPluginName

const winStack = `Error: anonymous function
Expand Down Expand Up @@ -41,8 +41,9 @@ at TAP.test (/home/leonardo/desktop/fastify-plugin/node_modules/tap/lib/test.js:

const anonymousStack = 'Unable to parse this'

t.plan(3)

t.equal(extractPluginName(winStack), 'hello.test')
t.equal(extractPluginName(nixStack), 'this.is.a.test')
t.equal(extractPluginName(anonymousStack), 'anonymous')
test('extractPluginName tests', async (t) => {
t.plan(3)
t.assert.strictEqual(extractPluginName(winStack), 'hello.test')
t.assert.strictEqual(extractPluginName(nixStack), 'this.is.a.test')
t.assert.strictEqual(extractPluginName(anonymousStack), 'anonymous')
})
9 changes: 4 additions & 5 deletions test/mu1tip1e.composite.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
'use strict'

const t = require('tap')
const test = t.test
const { test } = require('node:test')
const fp = require('../plugin')

test('anonymous function should be named mu1tip1e.composite.test', t => {
test('anonymous function should be named mu1tip1e.composite.test', async (t) => {
t.plan(2)

const fn = fp((fastify, opts, next) => {
next()
})

t.equal(fn[Symbol.for('plugin-meta')].name, 'mu1tip1e.composite.test-auto-0')
t.equal(fn[Symbol.for('fastify.display-name')], 'mu1tip1e.composite.test-auto-0')
t.assert.strictEqual(fn[Symbol.for('plugin-meta')].name, 'mu1tip1e.composite.test-auto-0')
t.assert.strictEqual(fn[Symbol.for('fastify.display-name')], 'mu1tip1e.composite.test-auto-0')
})
Loading
Loading