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

test: migrate to node test runner #118

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
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 @@ -10,7 +10,7 @@
"lint:fix": "eslint --fix",
"test": "npm run test:unit && npm run test:jest && npm run test:typescript",
"test:jest": "jest jest.test.js",
"test:unit": "tap",
"test:unit": "c8 --100 node --test",
"test:typescript": "tsd"
},
"repository": {
Expand Down Expand Up @@ -64,10 +64,10 @@
"devDependencies": {
"@fastify/pre-commit": "^2.1.0",
"benchmark": "^2.1.4",
"c8": "^10.1.3",
"eslint": "^9.17.0",
"jest": "^29.7.0",
"neostandard": "^0.12.0",
"tap": "^18.7.2",
"tsd": "^0.31.0"
}
}
17 changes: 11 additions & 6 deletions test/emit-interpolated-string.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
'use strict'

const test = require('tap').test
const { test } = require('node:test')
const { createWarning } = require('..')
const { withResolvers } = require('./promise')

test('emit with interpolated string', t => {
t.plan(4)

const { promise, resolve } = withResolvers()

process.on('warning', onWarning)
function onWarning (warning) {
t.equal(warning.name, 'TestDeprecation')
t.equal(warning.code, 'CODE')
t.equal(warning.message, 'Hello world')
t.ok(codeWarning.emitted)
t.assert.deepStrictEqual(warning.name, 'TestDeprecation')
t.assert.deepStrictEqual(warning.code, 'CODE')
t.assert.deepStrictEqual(warning.message, 'Hello world')
t.assert.ok(codeWarning.emitted)
}

const codeWarning = createWarning({
Expand All @@ -24,6 +27,8 @@ test('emit with interpolated string', t => {

setImmediate(() => {
process.removeListener('warning', onWarning)
t.end()
resolve()
})

return promise
})
17 changes: 11 additions & 6 deletions test/emit-once-only.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
'use strict'

const test = require('tap').test
const { test } = require('node:test')
const { createWarning } = require('..')
const { withResolvers } = require('./promise')

test('emit should emit a given code only once', t => {
t.plan(4)

const { promise, resolve } = withResolvers()

process.on('warning', onWarning)
function onWarning (warning) {
t.equal(warning.name, 'TestDeprecation')
t.equal(warning.code, 'CODE')
t.equal(warning.message, 'Hello world')
t.ok(warn.emitted)
t.assert.deepStrictEqual(warning.name, 'TestDeprecation')
t.assert.deepStrictEqual(warning.code, 'CODE')
t.assert.deepStrictEqual(warning.message, 'Hello world')
t.assert.ok(warn.emitted)
}

const warn = createWarning({
Expand All @@ -23,6 +26,8 @@ test('emit should emit a given code only once', t => {
warn()
setImmediate(() => {
process.removeListener('warning', onWarning)
t.end()
resolve()
})

return promise
})
16 changes: 10 additions & 6 deletions test/emit-reset.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
'use strict'

const test = require('tap').test
const { test } = require('node:test')
const { createWarning } = require('../')
const { withResolvers } = require('./promise')

test('a limited warning can be re-set', t => {
t.plan(4)

const { promise, resolve } = withResolvers()
let count = 0
process.on('warning', onWarning)
function onWarning () {
Expand All @@ -19,18 +21,20 @@ test('a limited warning can be re-set', t => {
})

warn()
t.ok(warn.emitted)
t.assert.ok(warn.emitted)

warn()
t.ok(warn.emitted)
t.assert.ok(warn.emitted)

warn.emitted = false
warn()
t.ok(warn.emitted)
t.assert.ok(warn.emitted)

setImmediate(() => {
t.equal(count, 2)
t.assert.deepStrictEqual(count, 2)
process.removeListener('warning', onWarning)
t.end()
resolve()
})

return promise
})
15 changes: 10 additions & 5 deletions test/emit-set.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
'use strict'

const test = require('tap').test
const { test } = require('node:test')
const { createWarning } = require('../')
const { withResolvers } = require('./promise')

test('emit should set the emitted state', t => {
t.plan(3)

const { promise, resolve } = withResolvers()

process.on('warning', onWarning)
function onWarning () {
t.fail('should not be called')
Expand All @@ -16,15 +19,17 @@ test('emit should set the emitted state', t => {
code: 'CODE',
message: 'Hello world'
})
t.notOk(warn.emitted)
t.assert.ok(!warn.emitted)
warn.emitted = true
t.ok(warn.emitted)
t.assert.ok(warn.emitted)

warn()
t.ok(warn.emitted)
t.assert.ok(warn.emitted)

setImmediate(() => {
process.removeListener('warning', onWarning)
t.end()
resolve()
})

return promise
})
19 changes: 12 additions & 7 deletions test/emit-unlimited.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict'

const test = require('tap').test
const { test } = require('node:test')
const { createWarning } = require('..')
const { withResolvers } = require('./promise')

test('emit should emit a given code unlimited times', t => {
t.plan(50)
Expand All @@ -10,13 +11,15 @@ test('emit should emit a given code unlimited times', t => {
const expectedRun = []
const times = 10

const { promise, resolve } = withResolvers()

process.on('warning', onWarning)
function onWarning (warning) {
t.equal(warning.name, 'TestDeprecation')
t.equal(warning.code, 'CODE')
t.equal(warning.message, 'Hello world')
t.ok(warn.emitted)
t.equal(runs++, expectedRun.shift())
t.assert.deepStrictEqual(warning.name, 'TestDeprecation')
t.assert.deepStrictEqual(warning.code, 'CODE')
t.assert.deepStrictEqual(warning.message, 'Hello world')
t.assert.ok(warn.emitted)
t.assert.deepStrictEqual(runs++, expectedRun.shift())
}

const warn = createWarning({
Expand All @@ -32,6 +35,8 @@ test('emit should emit a given code unlimited times', t => {
}
setImmediate(() => {
process.removeListener('warning', onWarning)
t.end()
resolve()
})

return promise
})
40 changes: 20 additions & 20 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const test = require('tap').test
const { test } = require('node:test')
const { createWarning, createDeprecation } = require('..')

process.removeAllListeners('warning')
Expand All @@ -13,9 +13,9 @@ test('Create warning with zero parameter', t => {
code: 'CODE',
message: 'Not available'
})
t.equal(warnItem.name, 'TestWarning')
t.equal(warnItem.message, 'Not available')
t.equal(warnItem.code, 'CODE')
t.assert.deepStrictEqual(warnItem.name, 'TestWarning')
t.assert.deepStrictEqual(warnItem.message, 'Not available')
t.assert.deepStrictEqual(warnItem.code, 'CODE')
})

test('Create error with 1 parameter', t => {
Expand All @@ -26,9 +26,9 @@ test('Create error with 1 parameter', t => {
code: 'CODE',
message: 'hey %s'
})
t.equal(warnItem.name, 'TestWarning')
t.equal(warnItem.format('alice'), 'hey alice')
t.equal(warnItem.code, 'CODE')
t.assert.deepStrictEqual(warnItem.name, 'TestWarning')
t.assert.deepStrictEqual(warnItem.format('alice'), 'hey alice')
t.assert.deepStrictEqual(warnItem.code, 'CODE')
})

test('Create error with 2 parameters', t => {
Expand All @@ -39,9 +39,9 @@ test('Create error with 2 parameters', t => {
code: 'CODE',
message: 'hey %s, I like your %s'
})
t.equal(warnItem.name, 'TestWarning')
t.equal(warnItem.format('alice', 'attitude'), 'hey alice, I like your attitude')
t.equal(warnItem.code, 'CODE')
t.assert.deepStrictEqual(warnItem.name, 'TestWarning')
t.assert.deepStrictEqual(warnItem.format('alice', 'attitude'), 'hey alice, I like your attitude')
t.assert.deepStrictEqual(warnItem.code, 'CODE')
})

test('Create error with 3 parameters', t => {
Expand All @@ -52,9 +52,9 @@ test('Create error with 3 parameters', t => {
code: 'CODE',
message: 'hey %s, I like your %s %s'
})
t.equal(warnItem.name, 'TestWarning')
t.equal(warnItem.format('alice', 'attitude', 'see you'), 'hey alice, I like your attitude see you')
t.equal(warnItem.code, 'CODE')
t.assert.deepStrictEqual(warnItem.name, 'TestWarning')
t.assert.deepStrictEqual(warnItem.format('alice', 'attitude', 'see you'), 'hey alice, I like your attitude see you')
t.assert.deepStrictEqual(warnItem.code, 'CODE')
})

test('Creates a deprecation warning', t => {
Expand All @@ -65,32 +65,32 @@ test('Creates a deprecation warning', t => {
code: 'CODE',
message: 'hello %s'
})
t.equal(deprecationItem.name, 'DeprecationWarning')
t.equal(deprecationItem.format('world'), 'hello world')
t.equal(deprecationItem.code, 'CODE')
t.assert.deepStrictEqual(deprecationItem.name, 'DeprecationWarning')
t.assert.deepStrictEqual(deprecationItem.format('world'), 'hello world')
t.assert.deepStrictEqual(deprecationItem.code, 'CODE')
})

test('Should throw when error code has no name', t => {
t.plan(1)
t.throws(() => createWarning(), new Error('Warning name must not be empty'))
t.assert.throws(() => createWarning(), new Error('Warning name must not be empty'))
})

test('Should throw when error has no code', t => {
t.plan(1)
t.throws(() => createWarning({ name: 'name' }), new Error('Warning code must not be empty'))
t.assert.throws(() => createWarning({ name: 'name' }), new Error('Warning code must not be empty'))
})

test('Should throw when error has no message', t => {
t.plan(1)
t.throws(() => createWarning({
t.assert.throws(() => createWarning({
name: 'name',
code: 'code'
}), new Error('Warning message must not be empty'))
})

test('Cannot set unlimited other than boolean', t => {
t.plan(1)
t.throws(() => createWarning({
t.assert.throws(() => createWarning({
name: 'name',
code: 'code',
message: 'message',
Expand Down
11 changes: 8 additions & 3 deletions test/issue-88.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
'use strict'

const { test } = require('tap')
const { test } = require('node:test')
const { createWarning } = require('..')
const { withResolvers } = require('./promise')

test('Must not overwrite config', t => {
t.plan(1)

function onWarning (warning) {
t.equal(warning.code, 'CODE_1')
t.assert.deepStrictEqual(warning.code, 'CODE_1')
}

const a = createWarning({
Expand All @@ -22,12 +23,16 @@ test('Must not overwrite config', t => {
unlimited: true
})

const { promise, resolve } = withResolvers()

process.on('warning', onWarning)
a('CODE_1')
a('CODE_1')

setImmediate(() => {
process.removeListener('warning', onWarning)
t.end()
resolve()
})

return promise
})
32 changes: 17 additions & 15 deletions test/jest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@

const { createWarning } = require('..')

test('works with jest', done => {
const code = createWarning({
name: 'TestDeprecation',
code: 'CODE',
message: 'Hello world'
})
code('world')
if (globalThis.test) {
test('works with jest', done => {
const code = createWarning({
name: 'TestDeprecation',
code: 'CODE',
message: 'Hello world'
})
code('world')

// we cannot actually listen to process warning event
// because jest messes with it (that's the point of this test)
// we can only test it was emitted indirectly
// and test no exception is raised
setImmediate(() => {
expect(code.emitted).toBeTruthy()
done()
// we cannot actually listen to process warning event
// because jest messes with it (that's the point of this test)
// we can only test it was emitted indirectly
// and test no exception is raised
setImmediate(() => {
expect(code.emitted).toBeTruthy()
done()
})
})
})
}
Loading
Loading