Skip to content

Commit

Permalink
feat: Port tests from tap to node:test
Browse files Browse the repository at this point in the history
  • Loading branch information
codershiba committed Feb 11, 2024
1 parent e996624 commit 0afc9c0
Show file tree
Hide file tree
Showing 7 changed files with 341 additions and 318 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: [14, 16, 18]
node-version: [18, 20]
steps:
- name: Check out repo
uses: actions/checkout@v4
Expand Down
2 changes: 0 additions & 2 deletions .taprc

This file was deleted.

15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"scripts": {
"lint": "standard | snazzy",
"lint-ci": "standard",
"test": "tap --no-cov",
"test-ci": "tap --cov --no-check-coverage --coverage-report=text",
"test": "borp -p 'test/**/*.js'",
"test-ci": "borp --coverage -p 'test/**/*.js'",
"test-types": "tsc && tsd"
},
"repository": {
Expand All @@ -32,13 +32,14 @@
"test-types"
],
"devDependencies": {
"@types/node": "^20.1.0",
"@matteo.collina/tspl": "^0.1.1",
"@types/node": "^20.11.16",
"borp": "^0.9.1",
"pre-commit": "^1.2.2",
"snazzy": "^9.0.0",
"standard": "^17.0.0",
"tap": "^15.0.10",
"tsd": "^0.30.0",
"typescript": "^5.0.2"
"standard": "^17.1.0",
"tsd": "^0.30.4",
"typescript": "^5.3.3"
},
"tsd": {
"directory": "test/types"
Expand Down
160 changes: 72 additions & 88 deletions test/err-with-cause.test.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,50 @@
'use strict'

const test = require('tap').test
const { test } = require('node:test')
const assert = require('node:assert')
const serializer = require('../lib/err-with-cause')
const wrapErrorSerializer = require('../').wrapErrorSerializer
const { wrapErrorSerializer } = require('../')

test('serializes Error objects', function (t) {
t.plan(3)
test('serializes Error objects', () => {
const serialized = serializer(Error('foo'))
t.equal(serialized.type, 'Error')
t.equal(serialized.message, 'foo')
t.match(serialized.stack, /err-with-cause\.test\.js:/)
assert.strictEqual(serialized.type, 'Error')
assert.strictEqual(serialized.message, 'foo')
assert.match(serialized.stack, /err-with-cause\.test\.js:/)
})

test('serializes Error objects with extra properties', function (t) {
t.plan(5)
test('serializes Error objects with extra properties', () => {
const err = Error('foo')
err.statusCode = 500
const serialized = serializer(err)
t.equal(serialized.type, 'Error')
t.equal(serialized.message, 'foo')
t.ok(serialized.statusCode)
t.equal(serialized.statusCode, 500)
t.match(serialized.stack, /err-with-cause\.test\.js:/)
assert.strictEqual(serialized.type, 'Error')
assert.strictEqual(serialized.message, 'foo')
assert.ok(serialized.statusCode)
assert.strictEqual(serialized.statusCode, 500)
assert.match(serialized.stack, /err-with-cause\.test\.js:/)
})

test('serializes Error objects with subclass "type"', function (t) {
t.plan(1)

test('serializes Error objects with subclass "type"', () => {
class MyError extends Error {}

const err = new MyError('foo')
const serialized = serializer(err)
t.equal(serialized.type, 'MyError')
assert.strictEqual(serialized.type, 'MyError')
})

test('serializes nested errors', function (t) {
t.plan(7)
test('serializes nested errors', () => {
const err = Error('foo')
err.inner = Error('bar')
const serialized = serializer(err)
t.equal(serialized.type, 'Error')
t.equal(serialized.message, 'foo')
t.match(serialized.stack, /err-with-cause\.test\.js:/)
t.equal(serialized.inner.type, 'Error')
t.equal(serialized.inner.message, 'bar')
t.match(serialized.inner.stack, /Error: bar/)
t.match(serialized.inner.stack, /err-with-cause\.test\.js:/)
assert.strictEqual(serialized.type, 'Error')
assert.strictEqual(serialized.message, 'foo')
assert.match(serialized.stack, /err-with-cause\.test\.js:/)
assert.strictEqual(serialized.inner.type, 'Error')
assert.strictEqual(serialized.inner.message, 'bar')
assert.match(serialized.inner.stack, /Error: bar/)
assert.match(serialized.inner.stack, /err-with-cause\.test\.js:/)
})

test('serializes error causes', function (t) {
test('serializes error causes', () => {
const innerErr = Error('inner')
const middleErr = Error('middle')
middleErr.cause = innerErr
Expand All @@ -57,44 +53,39 @@ test('serializes error causes', function (t) {

const serialized = serializer(outerErr)

t.equal(serialized.type, 'Error')
t.equal(serialized.message, 'outer')
t.match(serialized.stack, /err-with-cause\.test\.js:/)

t.equal(serialized.cause.type, 'Error')
t.equal(serialized.cause.message, 'middle')
t.match(serialized.cause.stack, /err-with-cause\.test\.js:/)
assert.strictEqual(serialized.type, 'Error')
assert.strictEqual(serialized.message, 'outer')
assert.match(serialized.stack, /err-with-cause\.test\.js:/)

t.equal(serialized.cause.cause.type, 'Error')
t.equal(serialized.cause.cause.message, 'inner')
t.match(serialized.cause.cause.stack, /err-with-cause\.test\.js:/)
assert.strictEqual(serialized.cause.type, 'Error')
assert.strictEqual(serialized.cause.message, 'middle')
assert.match(serialized.cause.stack, /err-with-cause\.test\.js:/)

t.end()
assert.strictEqual(serialized.cause.cause.type, 'Error')
assert.strictEqual(serialized.cause.cause.message, 'inner')
assert.match(serialized.cause.cause.stack, /err-with-cause\.test\.js:/)
})

test('keeps non-error cause', function (t) {
t.plan(3)
test('keeps non-error cause', () => {
const err = Error('foo')
err.cause = 'abc'
const serialized = serializer(err)
t.equal(serialized.type, 'Error')
t.equal(serialized.message, 'foo')
t.equal(serialized.cause, 'abc')
assert.strictEqual(serialized.type, 'Error')
assert.strictEqual(serialized.message, 'foo')
assert.strictEqual(serialized.cause, 'abc')
})

test('prevents infinite recursion', function (t) {
t.plan(4)
test('prevents infinite recursion', () => {
const err = Error('foo')
err.inner = err
const serialized = serializer(err)
t.equal(serialized.type, 'Error')
t.equal(serialized.message, 'foo')
t.match(serialized.stack, /err-with-cause\.test\.js:/)
t.notOk(serialized.inner)
assert.strictEqual(serialized.type, 'Error')
assert.strictEqual(serialized.message, 'foo')
assert.match(serialized.stack, /err-with-cause\.test\.js:/)
assert.ok(!serialized.inner)
})

test('cleans up infinite recursion tracking', function (t) {
t.plan(8)
test('cleans up infinite recursion tracking', () => {
const err = Error('foo')
const bar = Error('bar')
err.inner = bar
Expand All @@ -103,29 +94,26 @@ test('cleans up infinite recursion tracking', function (t) {
serializer(err)
const serialized = serializer(err)

t.equal(serialized.type, 'Error')
t.equal(serialized.message, 'foo')
t.match(serialized.stack, /err-with-cause\.test\.js:/)
t.ok(serialized.inner)
t.equal(serialized.inner.type, 'Error')
t.equal(serialized.inner.message, 'bar')
t.match(serialized.inner.stack, /Error: bar/)
t.notOk(serialized.inner.inner)
assert.strictEqual(serialized.type, 'Error')
assert.strictEqual(serialized.message, 'foo')
assert.match(serialized.stack, /err-with-cause\.test\.js:/)
assert.ok(serialized.inner)
assert.strictEqual(serialized.inner.type, 'Error')
assert.strictEqual(serialized.inner.message, 'bar')
assert.match(serialized.inner.stack, /Error: bar/)
assert.ok(!serialized.inner.inner)
})

test('err.raw is available', function (t) {
t.plan(1)
test('err.raw is available', () => {
const err = Error('foo')
const serialized = serializer(err)
t.equal(serialized.raw, err)
assert.strictEqual(serialized.raw, err)
})

test('redefined err.constructor doesnt crash serializer', function (t) {
t.plan(10)

test('redefined err.constructor doesnt crash serializer', () => {
function check (a, name) {
t.equal(a.type, name)
t.equal(a.message, 'foo')
assert.strictEqual(a.type, name)
assert.strictEqual(a.message, 'foo')
}

const err1 = TypeError('foo')
Expand Down Expand Up @@ -154,20 +142,17 @@ test('redefined err.constructor doesnt crash serializer', function (t) {
check(serializer(err5), 'Error')
})

test('pass through anything that does not look like an Error', function (t) {
t.plan(3)

test('pass through anything that does not look like an Error', () => {
function check (a) {
t.equal(serializer(a), a)
assert.strictEqual(serializer(a), a)
}

check('foo')
check({ hello: 'world' })
check([1, 2])
})

test('can wrap err serializers', function (t) {
t.plan(5)
test('can wrap err serializers', () => {
const err = Error('foo')
err.foo = 'foo'
const serializer = wrapErrorSerializer(function (err) {
Expand All @@ -176,28 +161,27 @@ test('can wrap err serializers', function (t) {
return err
})
const serialized = serializer(err)
t.equal(serialized.type, 'Error')
t.equal(serialized.message, 'foo')
t.match(serialized.stack, /err-with-cause\.test\.js:/)
t.notOk(serialized.foo)
t.equal(serialized.bar, 'bar')
assert.strictEqual(serialized.type, 'Error')
assert.strictEqual(serialized.message, 'foo')
assert.match(serialized.stack, /err-with-cause\.test\.js:/)
assert.ok(!serialized.foo)
assert.strictEqual(serialized.bar, 'bar')
})

test('serializes aggregate errors', { skip: !global.AggregateError }, function (t) {
t.plan(14)
test('serializes aggregate errors', { skip: !global.AggregateError }, () => {
const foo = new Error('foo')
const bar = new Error('bar')
for (const aggregate of [
new AggregateError([foo, bar], 'aggregated message'), // eslint-disable-line no-undef
{ errors: [foo, bar], message: 'aggregated message', stack: 'err-with-cause.test.js:' }
]) {
const serialized = serializer(aggregate)
t.equal(serialized.message, 'aggregated message')
t.equal(serialized.aggregateErrors.length, 2)
t.equal(serialized.aggregateErrors[0].message, 'foo')
t.equal(serialized.aggregateErrors[1].message, 'bar')
t.match(serialized.aggregateErrors[0].stack, /^Error: foo/)
t.match(serialized.aggregateErrors[1].stack, /^Error: bar/)
t.match(serialized.stack, /err-with-cause\.test\.js:/)
assert.strictEqual(serialized.message, 'aggregated message')
assert.strictEqual(serialized.aggregateErrors.length, 2)
assert.strictEqual(serialized.aggregateErrors[0].message, 'foo')
assert.strictEqual(serialized.aggregateErrors[1].message, 'bar')
assert.match(serialized.aggregateErrors[0].stack, /^Error: foo/)
assert.match(serialized.aggregateErrors[1].stack, /^Error: bar/)
assert.match(serialized.stack, /err-with-cause\.test\.js:/)
}
})
Loading

0 comments on commit 0afc9c0

Please sign in to comment.