Skip to content

Commit

Permalink
feat: Port tests from tap to node:assert and node:test (#141)
Browse files Browse the repository at this point in the history
* Bump actions/setup-node from 3 to 4 (#138)

Bumps [actions/setup-node](https://github.com/actions/setup-node) from 3 to 4.
- [Release notes](https://github.com/actions/setup-node/releases)
- [Commits](actions/setup-node@v3...v4)

---
updated-dependencies:
- dependency-name: actions/setup-node
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump tsd from 0.29.0 to 0.30.0 (#139)

Bumps [tsd](https://github.com/tsdjs/tsd) from 0.29.0 to 0.30.0.
- [Release notes](https://github.com/tsdjs/tsd/releases)
- [Commits](tsdjs/tsd@v0.29.0...v0.30.0)

---
updated-dependencies:
- dependency-name: tsd
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump actions/dependency-review-action from 3 to 4 (#142)

Bumps [actions/dependency-review-action](https://github.com/actions/dependency-review-action) from 3 to 4.
- [Release notes](https://github.com/actions/dependency-review-action/releases)
- [Commits](actions/dependency-review-action@v3...v4)

---
updated-dependencies:
- dependency-name: actions/dependency-review-action
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* feat: Port tests from `tap` to `node:test`

* chore(deps): Update `@types/node` to v`20.11.17`

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
  • Loading branch information
codershiba and dependabot[bot] committed Mar 5, 2024
1 parent 335deaf commit 1c52a89
Show file tree
Hide file tree
Showing 7 changed files with 343 additions and 320 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Expand Up @@ -29,7 +29,7 @@ jobs:
persist-credentials: false

- name: Dependency review
uses: actions/dependency-review-action@v3
uses: actions/dependency-review-action@v4

test:
name: Test
Expand All @@ -47,7 +47,7 @@ jobs:
persist-credentials: false

- name: Setup Node ${{ matrix.node-version }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

Expand Down
2 changes: 0 additions & 2 deletions .taprc

This file was deleted.

17 changes: 9 additions & 8 deletions package.json
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": {
"@fastify/pre-commit": "^2.0.2",
"@types/node": "^20.1.0",
"@matteo.collina/tspl": "^0.1.1",
"@types/node": "^20.11.17",
"borp": "^0.9.1",
"pre-commit": "^1.2.2",
"snazzy": "^9.0.0",
"standard": "^17.0.0",
"tap": "^16.3.9",
"tsd": "^0.29.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
@@ -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:/)
}
})

0 comments on commit 1c52a89

Please sign in to comment.