diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0697de9..1310c86 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 @@ -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 }} diff --git a/.taprc b/.taprc deleted file mode 100644 index 723b65b..0000000 --- a/.taprc +++ /dev/null @@ -1,2 +0,0 @@ -files: -- test/**/*.test.js diff --git a/package.json b/package.json index f4c3b42..44234dc 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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" diff --git a/test/err-with-cause.test.js b/test/err-with-cause.test.js index bb87a56..15f356a 100644 --- a/test/err-with-cause.test.js +++ b/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 @@ -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 @@ -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') @@ -154,11 +142,9 @@ 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') @@ -166,8 +152,7 @@ test('pass through anything that does not look like an Error', function (t) { 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) { @@ -176,15 +161,14 @@ 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 [ @@ -192,12 +176,12 @@ test('serializes aggregate errors', { skip: !global.AggregateError }, function ( { 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:/) } }) diff --git a/test/err.test.js b/test/err.test.js index 96aa5e3..0aecb07 100644 --- a/test/err.test.js +++ b/test/err.test.js @@ -1,53 +1,49 @@ 'use strict' -const test = require('tap').test +const assert = require('node:assert') +const { test } = require('node:test') const serializer = require('../lib/err') -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\.test\.js:/) + assert.strictEqual(serialized.type, 'Error') + assert.strictEqual(serialized.message, 'foo') + assert.match(serialized.stack, /err\.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\.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\.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\.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\.test\.js:/) + assert.strictEqual(serialized.type, 'Error') + assert.strictEqual(serialized.message, 'foo') + assert.match(serialized.stack, /err\.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\.test\.js:/) }) -test('serializes error causes', function (t) { - t.plan(14) +test('serializes error causes', () => { for (const cause of [ Error('bar'), { message: 'bar', stack: 'Error: bar: err.test.js:' } @@ -56,18 +52,17 @@ test('serializes error causes', function (t) { err.cause = cause err.cause.cause = Error('abc') const serialized = serializer(err) - t.equal(serialized.type, 'Error') - t.equal(serialized.message, 'foo: bar: abc') - t.match(serialized.stack, /err\.test\.js:/) - t.match(serialized.stack, /Error: foo/) - t.match(serialized.stack, /Error: bar/) - t.match(serialized.stack, /Error: abc/) - t.notOk(serialized.cause) + assert.strictEqual(serialized.type, 'Error') + assert.strictEqual(serialized.message, 'foo: bar: abc') + assert.match(serialized.stack, /err\.test\.js:/) + assert.match(serialized.stack, /Error: foo/) + assert.match(serialized.stack, /Error: bar/) + assert.match(serialized.stack, /Error: abc/) + assert.ok(!serialized.cause) } }) test('serializes error causes with VError support', function (t) { - t.plan(6) // Fake VError-style setup const err = Error('foo: bar') err.foo = 'abc' @@ -77,37 +72,34 @@ test('serializes error causes with VError support', function (t) { return err } const serialized = serializer(err) - t.equal(serialized.type, 'Error') - t.equal(serialized.message, 'foo: bar: abc') - t.match(serialized.stack, /err\.test\.js:/) - t.match(serialized.stack, /Error: foo/) - t.match(serialized.stack, /Error: bar/) - t.match(serialized.stack, /Error: abc/) + assert.strictEqual(serialized.type, 'Error') + assert.strictEqual(serialized.message, 'foo: bar: abc') + assert.match(serialized.stack, /err\.test\.js:/) + assert.match(serialized.stack, /Error: foo/) + assert.match(serialized.stack, /Error: bar/) + assert.match(serialized.stack, /Error: abc/) }) -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\.test\.js:/) - t.notOk(serialized.inner) + assert.strictEqual(serialized.type, 'Error') + assert.strictEqual(serialized.message, 'foo') + assert.match(serialized.stack, /err\.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 @@ -116,29 +108,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\.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\.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') @@ -166,11 +155,9 @@ 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') @@ -178,8 +165,7 @@ test('pass through anything that does not look like an Error', function (t) { 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) { @@ -188,15 +174,14 @@ 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\.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\.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 [ @@ -204,12 +189,12 @@ test('serializes aggregate errors', { skip: !global.AggregateError }, function ( { errors: [foo, bar], message: 'aggregated message', stack: 'err.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\.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\.test\.js:/) } }) diff --git a/test/req.test.js b/test/req.test.js index f777b5c..d8a6486 100644 --- a/test/req.test.js +++ b/test/req.test.js @@ -1,12 +1,13 @@ 'use strict' -const http = require('http') -const test = require('tap').test +const { tspl } = require('@matteo.collina/tspl') +const http = require('node:http') +const { test } = require('node:test') const serializers = require('../lib/req') -const wrapRequestSerializer = require('../').wrapRequestSerializer +const { wrapRequestSerializer } = require('../') -test('maps request', function (t) { - t.plan(2) +test('maps request', async (t) => { + const p = tspl(t, { plan: 2 }) const server = http.createServer(handler) server.unref() @@ -14,19 +15,20 @@ test('maps request', function (t) { http.get(server.address(), () => {}) }) - t.teardown(() => server.close()) + t.after(() => server.close()) function handler (req, res) { const serialized = serializers.mapHttpRequest(req) - t.ok(serialized.req) - t.ok(serialized.req.method) - t.end() + p.ok(serialized.req) + p.ok(serialized.req.method) res.end() } + + await p.completed }) -test('does not return excessively long object', function (t) { - t.plan(1) +test('does not return excessively long object', async (t) => { + const p = tspl(t, { plan: 1 }) const server = http.createServer(handler) server.unref() @@ -34,17 +36,19 @@ test('does not return excessively long object', function (t) { http.get(server.address(), () => {}) }) - t.teardown(() => server.close()) + t.after(() => server.close()) function handler (req, res) { const serialized = serializers.reqSerializer(req) - t.equal(Object.keys(serialized).length, 6) + p.strictEqual(Object.keys(serialized).length, 6) res.end() } + + await p.completed }) -test('req.raw is available', function (t) { - t.plan(2) +test('req.raw is available', async (t) => { + const p = tspl(t, { plan: 2 }) const server = http.createServer(handler) server.unref() @@ -52,19 +56,21 @@ test('req.raw is available', function (t) { http.get(server.address(), () => {}) }) - t.teardown(() => server.close()) + t.after(() => server.close()) function handler (req, res) { req.foo = 'foo' const serialized = serializers.reqSerializer(req) - t.ok(serialized.raw) - t.equal(serialized.raw.foo, 'foo') + p.ok(serialized.raw) + p.strictEqual(serialized.raw.foo, 'foo') res.end() } + + await p.completed }) -test('req.raw will be obtained in from input request raw property if input request raw property is truthy', function (t) { - t.plan(2) +test('req.raw will be obtained in from input request raw property if input request raw property is truthy', async (t) => { + const p = tspl(t, { plan: 2 }) const server = http.createServer(handler) server.unref() @@ -72,19 +78,21 @@ test('req.raw will be obtained in from input request raw property if input reque http.get(server.address(), () => {}) }) - t.teardown(() => server.close()) + t.after(() => server.close()) function handler (req, res) { req.raw = { req: { foo: 'foo' }, res: {} } const serialized = serializers.reqSerializer(req) - t.ok(serialized.raw) - t.equal(serialized.raw.req.foo, 'foo') + p.ok(serialized.raw) + p.strictEqual(serialized.raw.req.foo, 'foo') res.end() } + + await p.completed }) -test('req.id defaults to undefined', function (t) { - t.plan(1) +test('req.id defaults to undefined', async (t) => { + const p = tspl(t, { plan: 1 }) const server = http.createServer(handler) server.unref() @@ -92,17 +100,19 @@ test('req.id defaults to undefined', function (t) { http.get(server.address(), () => {}) }) - t.teardown(() => server.close()) + t.after(() => server.close()) function handler (req, res) { const serialized = serializers.reqSerializer(req) - t.equal(serialized.id, undefined) + p.strictEqual(serialized.id, undefined) res.end() } + + await p.completed }) -test('req.id has a non-function value', function (t) { - t.plan(1) +test('req.id has a non-function value', async (t) => { + const p = tspl(t, { plan: 1 }) const server = http.createServer(handler) server.unref() @@ -110,17 +120,19 @@ test('req.id has a non-function value', function (t) { http.get(server.address(), () => {}) }) - t.teardown(() => server.close()) + t.after(() => server.close()) function handler (req, res) { const serialized = serializers.reqSerializer(req) - t.equal(typeof serialized.id === 'function', false) + p.strictEqual(typeof serialized.id === 'function', false) res.end() } + + await p.completed }) -test('req.id will be obtained from input request info.id when input request id does not exist', function (t) { - t.plan(1) +test('req.id will be obtained from input request info.id when input request id does not exist', async (t) => { + const p = tspl(t, { plan: 1 }) const server = http.createServer(handler) server.unref() @@ -128,18 +140,20 @@ test('req.id will be obtained from input request info.id when input request id d http.get(server.address(), () => {}) }) - t.teardown(() => server.close()) + t.after(() => server.close()) function handler (req, res) { req.info = { id: 'test' } const serialized = serializers.reqSerializer(req) - t.equal(serialized.id, 'test') + p.strictEqual(serialized.id, 'test') res.end() } + + await p.completed }) -test('req.id has a non-function value with custom id function', function (t) { - t.plan(2) +test('req.id has a non-function value with custom id function', async (t) => { + const p = tspl(t, { plan: 2 }) const server = http.createServer(handler) server.unref() @@ -147,19 +161,21 @@ test('req.id has a non-function value with custom id function', function (t) { http.get(server.address(), () => {}) }) - t.teardown(() => server.close()) + t.after(() => server.close()) function handler (req, res) { req.id = function () { return 42 } const serialized = serializers.reqSerializer(req) - t.equal(typeof serialized.id === 'function', false) - t.equal(serialized.id, 42) + p.strictEqual(typeof serialized.id === 'function', false) + p.strictEqual(serialized.id, 42) res.end() } + + await p.completed }) -test('req.url will be obtained from input request req.path when input request url is an object', function (t) { - t.plan(1) +test('req.url will be obtained from input request req.path when input request url is an object', async (t) => { + const p = tspl(t, { plan: 1 }) const server = http.createServer(handler) server.unref() @@ -167,18 +183,20 @@ test('req.url will be obtained from input request req.path when input request ur http.get(server.address(), () => {}) }) - t.teardown(() => server.close()) + t.after(() => server.close()) function handler (req, res) { req.path = '/test' const serialized = serializers.reqSerializer(req) - t.equal(serialized.url, '/test') + p.strictEqual(serialized.url, '/test') res.end() } + + await p.completed }) -test('req.url will be obtained from input request url.path when input request url is an object', function (t) { - t.plan(1) +test('req.url will be obtained from input request url.path when input request url is an object', async (t) => { + const p = tspl(t, { plan: 1 }) const server = http.createServer(handler) server.unref() @@ -186,18 +204,20 @@ test('req.url will be obtained from input request url.path when input request ur http.get(server.address(), () => {}) }) - t.teardown(() => server.close()) + t.after(() => server.close()) function handler (req, res) { req.url = { path: '/test' } const serialized = serializers.reqSerializer(req) - t.equal(serialized.url, '/test') + p.strictEqual(serialized.url, '/test') res.end() } + + await p.completed }) -test('req.url will be obtained from input request url when input request url is not an object', function (t) { - t.plan(1) +test('req.url will be obtained from input request url when input request url is not an object', async (t) => { + const p = tspl(t, { plan: 1 }) const server = http.createServer(handler) server.unref() @@ -205,18 +225,20 @@ test('req.url will be obtained from input request url when input request url is http.get(server.address(), () => {}) }) - t.teardown(() => server.close()) + t.after(() => server.close()) function handler (req, res) { req.url = '/test' const serialized = serializers.reqSerializer(req) - t.equal(serialized.url, '/test') + p.strictEqual(serialized.url, '/test') res.end() } + + await p.completed }) -test('req.url will be empty when input request path and url are not defined', function (t) { - t.plan(1) +test('req.url will be empty when input request path and url are not defined', async (t) => { + const p = tspl(t, { plan: 1 }) const server = http.createServer(handler) server.unref() @@ -224,17 +246,19 @@ test('req.url will be empty when input request path and url are not defined', fu http.get(server.address(), () => {}) }) - t.teardown(() => server.close()) + t.after(() => server.close()) function handler (req, res) { const serialized = serializers.reqSerializer(req) - t.equal(serialized.url, '/') + p.strictEqual(serialized.url, '/') res.end() } + + await p.completed }) -test('req.url will be obtained from input request originalUrl when available', function (t) { - t.plan(1) +test('req.url will be obtained from input request originalUrl when available', async (t) => { + const p = tspl(t, { plan: 1 }) const server = http.createServer(handler) server.unref() @@ -242,18 +266,20 @@ test('req.url will be obtained from input request originalUrl when available', f http.get(server.address(), () => {}) }) - t.teardown(() => server.close()) + t.after(() => server.close()) function handler (req, res) { req.originalUrl = '/test' const serialized = serializers.reqSerializer(req) - t.equal(serialized.url, '/test') + p.strictEqual(serialized.url, '/test') res.end() } + + await p.completed }) -test('req.url will be obtained from input request url when req path is a function', function (t) { - t.plan(1) +test('req.url will be obtained from input request url when req path is a function', async (t) => { + const p = tspl(t, { plan: 1 }) const server = http.createServer(handler) server.unref() @@ -261,7 +287,7 @@ test('req.url will be obtained from input request url when req path is a functio http.get(server.address(), () => {}) }) - t.teardown(() => server.close()) + t.after(() => server.close()) function handler (req, res) { req.path = function () { @@ -269,13 +295,15 @@ test('req.url will be obtained from input request url when req path is a functio } req.url = '/test' const serialized = serializers.reqSerializer(req) - t.equal(serialized.url, '/test') + p.strictEqual(serialized.url, '/test') res.end() } + + await p.completed }) -test('req.url being undefined does not throw an error', function (t) { - t.plan(1) +test('req.url being undefined does not throw an error', async (t) => { + const p = tspl(t, { plan: 1 }) const server = http.createServer(handler) server.unref() @@ -283,18 +311,20 @@ test('req.url being undefined does not throw an error', function (t) { http.get(server.address(), () => {}) }) - t.teardown(() => server.close()) + t.after(() => server.close()) function handler (req, res) { req.url = undefined const serialized = serializers.reqSerializer(req) - t.equal(serialized.url, undefined) + p.strictEqual(serialized.url, undefined) res.end() } + + await p.completed }) -test('can wrap request serializers', function (t) { - t.plan(3) +test('can wrap request serializers', async (t) => { + const p = tspl(t, { plan: 3 }) const server = http.createServer(handler) server.unref() @@ -302,24 +332,26 @@ test('can wrap request serializers', function (t) { http.get(server.address(), () => {}) }) - t.teardown(() => server.close()) + t.after(() => server.close()) const serailizer = wrapRequestSerializer(function (req) { - t.ok(req.method) - t.equal(req.method, 'GET') + p.ok(req.method) + p.strictEqual(req.method, 'GET') delete req.method return req }) function handler (req, res) { const serialized = serailizer(req) - t.notOk(serialized.method) + p.ok(!serialized.method) res.end() } + + await p.completed }) -test('req.remoteAddress will be obtained from request socket.remoteAddress as fallback', function (t) { - t.plan(1) +test('req.remoteAddress will be obtained from request socket.remoteAddress as fallback', async (t) => { + const p = tspl(t, { plan: 1 }) const server = http.createServer(handler) server.unref() @@ -327,18 +359,20 @@ test('req.remoteAddress will be obtained from request socket.remoteAddress as fa http.get(server.address(), () => {}) }) - t.teardown(() => server.close()) + t.after(() => server.close()) function handler (req, res) { req.socket = { remoteAddress: 'http://localhost' } const serialized = serializers.reqSerializer(req) - t.equal(serialized.remoteAddress, 'http://localhost') + p.strictEqual(serialized.remoteAddress, 'http://localhost') res.end() } + + await p.completed }) -test('req.remoteAddress will be obtained from request info.remoteAddress if available', function (t) { - t.plan(1) +test('req.remoteAddress will be obtained from request info.remoteAddress if available', async (t) => { + const p = tspl(t, { plan: 1 }) const server = http.createServer(handler) server.unref() @@ -346,18 +380,20 @@ test('req.remoteAddress will be obtained from request info.remoteAddress if avai http.get(server.address(), () => {}) }) - t.teardown(() => server.close()) + t.after(() => server.close()) function handler (req, res) { req.info = { remoteAddress: 'http://localhost' } const serialized = serializers.reqSerializer(req) - t.equal(serialized.remoteAddress, 'http://localhost') + p.strictEqual(serialized.remoteAddress, 'http://localhost') res.end() } + + await p.completed }) -test('req.remotePort will be obtained from request socket.remotePort as fallback', function (t) { - t.plan(1) +test('req.remotePort will be obtained from request socket.remotePort as fallback', async (t) => { + const p = tspl(t, { plan: 1 }) const server = http.createServer(handler) server.unref() @@ -365,18 +401,20 @@ test('req.remotePort will be obtained from request socket.remotePort as fallback http.get(server.address(), () => {}) }) - t.teardown(() => server.close()) + t.after(() => server.close()) function handler (req, res) { req.socket = { remotePort: 3000 } const serialized = serializers.reqSerializer(req) - t.equal(serialized.remotePort, 3000) + p.strictEqual(serialized.remotePort, 3000) res.end() } + + await p.completed }) -test('req.remotePort will be obtained from request info.remotePort if available', function (t) { - t.plan(1) +test('req.remotePort will be obtained from request info.remotePort if available', async (t) => { + const p = tspl(t, { plan: 1 }) const server = http.createServer(handler) server.unref() @@ -384,18 +422,20 @@ test('req.remotePort will be obtained from request info.remotePort if available' http.get(server.address(), () => {}) }) - t.teardown(() => server.close()) + t.after(() => server.close()) function handler (req, res) { req.info = { remotePort: 3000 } const serialized = serializers.reqSerializer(req) - t.equal(serialized.remotePort, 3000) + p.strictEqual(serialized.remotePort, 3000) res.end() } + + await p.completed }) -test('req.query is available', function (t) { - t.plan(1) +test('req.query is available', async (t) => { + const p = tspl(t, { plan: 1 }) const server = http.createServer(handler) server.unref() @@ -403,18 +443,20 @@ test('req.query is available', function (t) { http.get(server.address(), () => {}) }) - t.teardown(() => server.close()) + t.after(() => server.close()) function handler (req, res) { req.query = '/foo?bar=foobar&bar=foo' const serialized = serializers.reqSerializer(req) - t.equal(serialized.query, '/foo?bar=foobar&bar=foo') + p.strictEqual(serialized.query, '/foo?bar=foobar&bar=foo') res.end() } + + await p.completed }) -test('req.params is available', function (t) { - t.plan(1) +test('req.params is available', async (t) => { + const p = tspl(t, { plan: 1 }) const server = http.createServer(handler) server.unref() @@ -422,12 +464,14 @@ test('req.params is available', function (t) { http.get(server.address(), () => {}) }) - t.teardown(() => server.close()) + t.after(() => server.close()) function handler (req, res) { req.params = '/foo/bar' const serialized = serializers.reqSerializer(req) - t.equal(serialized.params, '/foo/bar') + p.strictEqual(serialized.params, '/foo/bar') res.end() } + + await p.completed }) diff --git a/test/res.test.js b/test/res.test.js index 7a302bd..638afaf 100644 --- a/test/res.test.js +++ b/test/res.test.js @@ -2,13 +2,14 @@ /* eslint-disable no-prototype-builtins */ -const http = require('http') -const test = require('tap').test +const { tspl } = require('@matteo.collina/tspl') +const http = require('node:http') +const { test } = require('node:test') const serializers = require('../lib/res') -const wrapResponseSerializer = require('../').wrapResponseSerializer +const { wrapResponseSerializer } = require('../') -test('res.raw is not enumerable', function (t) { - t.plan(1) +test('res.raw is not enumerable', async (t) => { + const p = tspl(t, { plan: 1 }) const server = http.createServer(handler) server.unref() @@ -16,17 +17,19 @@ test('res.raw is not enumerable', function (t) { http.get(server.address(), () => {}) }) - t.teardown(() => server.close()) + t.after(() => server.close()) - function handler (req, res) { + function handler (_req, res) { const serialized = serializers.resSerializer(res) - t.equal(serialized.propertyIsEnumerable('raw'), false) + p.strictEqual(serialized.propertyIsEnumerable('raw'), false) res.end() } + + await p.completed }) -test('res.raw is available', function (t) { - t.plan(2) +test('res.raw is available', async (t) => { + const p = tspl(t, { plan: 2 }) const server = http.createServer(handler) server.unref() @@ -34,19 +37,21 @@ test('res.raw is available', function (t) { http.get(server.address(), () => {}) }) - t.teardown(() => server.close()) + t.after(() => server.close()) - function handler (req, res) { + function handler (_req, res) { res.statusCode = 200 const serialized = serializers.resSerializer(res) - t.ok(serialized.raw) - t.equal(serialized.raw.statusCode, 200) + p.ok(serialized.raw) + p.strictEqual(serialized.raw.statusCode, 200) res.end() } + + await p.completed }) -test('can wrap response serializers', function (t) { - t.plan(3) +test('can wrap response serializers', async (t) => { + const p = tspl(t, { plan: 3 }) const server = http.createServer(handler) server.unref() @@ -54,25 +59,27 @@ test('can wrap response serializers', function (t) { http.get(server.address(), () => {}) }) - t.teardown(() => server.close()) + t.after(() => server.close()) const serializer = wrapResponseSerializer(function (res) { - t.ok(res.statusCode) - t.equal(res.statusCode, 200) + p.ok(res.statusCode) + p.strictEqual(res.statusCode, 200) delete res.statusCode return res }) - function handler (req, res) { + function handler (_req, res) { res.end() res.statusCode = 200 const serialized = serializer(res) - t.notOk(serialized.statusCode) + p.ok(!serialized.statusCode) } + + await p.completed }) -test('res.headers is serialized', function (t) { - t.plan(1) +test('res.headers is serialized', async (t) => { + const p = tspl(t, { plan: 1 }) const server = http.createServer(handler) server.unref() @@ -80,18 +87,20 @@ test('res.headers is serialized', function (t) { http.get(server.address(), () => {}) }) - t.teardown(() => server.close()) + t.after(() => server.close()) - function handler (req, res) { + function handler (_req, res) { res.setHeader('x-custom', 'y') const serialized = serializers.resSerializer(res) - t.equal(serialized.headers['x-custom'], 'y') + p.strictEqual(serialized.headers['x-custom'], 'y') res.end() } + + await p.completed }) -test('res.statusCode is null before headers are flushed', function (t) { - t.plan(1) +test('req.url will be obtained from input request url when input request url is not an object', async (t) => { + const p = tspl(t, { plan: 1 }) const server = http.createServer(handler) server.unref() @@ -99,11 +108,13 @@ test('res.statusCode is null before headers are flushed', function (t) { http.get(server.address(), () => {}) }) - t.teardown(() => server.close()) + t.after(() => server.close()) - function handler (req, res) { + function handler (_req, res) { const serialized = serializers.resSerializer(res) - t.equal(serialized.statusCode, null) + p.strictEqual(serialized.statusCode, null) res.end() } + + await p.completed })