Skip to content

Commit

Permalink
migrate test suite from tap to node:test
Browse files Browse the repository at this point in the history
  • Loading branch information
nimesh0505 committed Sep 11, 2024
1 parent 97ec7bc commit dc5d22e
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 157 deletions.
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",
"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",
"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) => {
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

0 comments on commit dc5d22e

Please sign in to comment.