Skip to content

Commit

Permalink
chore: Replaced tap with Node test runner.
Browse files Browse the repository at this point in the history
  • Loading branch information
ShogunPanda committed Feb 6, 2024
1 parent 8c14b34 commit 6663606
Show file tree
Hide file tree
Showing 6 changed files with 754 additions and 851 deletions.
35 changes: 18 additions & 17 deletions generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,26 +139,26 @@ function main(): void {
errorClasses.push(`${klass}`)
classesTests.push(
`
t.test('${klass}', t => {
test('${klass}', () => {
t.plan(14)
const error = new ${klass}('WHATEVER', {key1: 'prop1'})
t.equal(${klass}.status, ${code})
t.equal(${klass}.error, '${identifier}')
t.equal(${klass}.message, "${message}")
t.equal(${klass}.phrase, "${phrase}")
t.equal(error.status, ${code})
t.equal(error.message, 'WHATEVER')
t.equal(error.error, "${message}")
t.equal(error.errorPhrase, "${phrase}")
t.equal(error.code, "HTTP_ERROR_${errorConstant}")
t.equal(error.name, '${klass}')
t.${isClientError ? 'true' : 'false'}(error.isClientError)
t.${isClientError ? 'false' : 'true'}(error.isServerError)
t.${isClientError ? 'true' : 'false'}(error.expose)
t.equal(error.key1, 'prop1')
deepStrictEqual(${klass}.status, ${code})
deepStrictEqual(${klass}.error, '${identifier}')
deepStrictEqual(${klass}.message, "${message}")
deepStrictEqual(${klass}.phrase, "${phrase}")
deepStrictEqual(error.status, ${code})
deepStrictEqual(error.message, 'WHATEVER')
deepStrictEqual(error.error, "${message}")
deepStrictEqual(error.errorPhrase, "${phrase}")
deepStrictEqual(error.code, "HTTP_ERROR_${errorConstant}")
deepStrictEqual(error.name, '${klass}')
ok(${isClientError ? '' : '!'}(error.isClientError)
ok(${isClientError ? '!' : ''}(error.isServerError)
ok(${isClientError ? '' : '!'}(error.expose)
deepStrictEqual(error.key1, 'prop1')
})
`
)
Expand Down Expand Up @@ -227,7 +227,8 @@ function main(): void {
`
/* eslint-disable @typescript-eslint/no-floating-promises */
import t from 'tap'
import { deepStrictEqual, ok } from 'node:assert'
import { test } from 'node:test'
import { ${errorClasses
.sort((a: string, b: string) => a.toLowerCase().localeCompare(b.toLowerCase()))
.join(',')} } from '../src/index.js'
Expand Down
10 changes: 3 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
"format": "prettier -w src test",
"lint": "eslint --cache --ext .js,.jsx,.ts,.tsx src test",
"typecheck": "tsc -p . --emitDeclarationOnly",
"test": "c8 -c test/config/c8-local.json tap test/*.test.ts",
"test:ci": "c8 -c test/config/c8-ci.json tap --no-color test/*.test.ts",
"test": "c8 -c test/config/c8-local.json node --import tsx --test test/*.test.ts",
"test:ci": "c8 -c test/config/c8-ci.json node --import tsx --test-reporter=tap --test test/*.test.ts",
"ci": "npm run build && npm run test:ci",
"prepublishOnly": "npm run ci",
"postpublish": "git push origin && git push origin -f --tags",
Expand All @@ -54,14 +54,10 @@
"concurrently": "^8.2.2",
"http-errors": "^2.0.0",
"prettier": "^3.2.4",
"tap": "^18.7.0",
"ts-node": "^10.9.2",
"tsx": "^4.7.0",
"typescript": "^5.3.3"
},
"engines": {
"node": ">= 18.18.0"
},
"tap": {
"extends": "./test/config/tap.yml"
}
}
165 changes: 73 additions & 92 deletions test/base.test.ts
Original file line number Diff line number Diff line change
@@ -1,101 +1,90 @@
/* eslint-disable @typescript-eslint/no-floating-promises */

import createHttpError from 'http-errors'
import t from 'tap'
import { createError, HttpError, isHttpError } from '../src/index.js'

t.test('HttpError', t => {
t.plan(5)

t.test('it should create a basic error', t => {
t.plan(8)
import { deepStrictEqual, ok } from 'node:assert'
import { test } from 'node:test'
import { HttpError, createError, isHttpError } from '../src/index.js'

test('HttpError', async () => {
await test('it should create a basic error', () => {
const error = new HttpError(404, 'WHATEVER', { key1: 'value1' })
const otherError = new HttpError(570, 'WHATEVER', { key1: 'value1' })

t.type(error, HttpError)
t.type(error, Error)
t.strictSame(error.message, 'WHATEVER')
t.strictSame(error.key1, 'value1')
t.strictSame(error.name, 'HttpError')
t.strictSame(error.code, 'HTTP_ERROR_NOT_FOUND')
ok(error instanceof HttpError)
ok(error instanceof Error)
deepStrictEqual(error.message, 'WHATEVER')
deepStrictEqual(error.key1, 'value1')
deepStrictEqual(error.name, 'HttpError')
deepStrictEqual(error.code, 'HTTP_ERROR_NOT_FOUND')

t.type(otherError, HttpError)
t.strictSame(otherError.code, 'HTTP_ERROR_570')
ok(otherError instanceof HttpError)
deepStrictEqual(otherError.code, 'HTTP_ERROR_570')
})

t.test('it should assign derived properties', t => {
t.plan(20)

await test('it should assign derived properties', () => {
const clientError = new HttpError(404, 'WHATEVER', { key1: 'value1', headers: { a: 'b' } })
const serverError = new HttpError(502, 'WHATEVER', { key1: 'value1', headers: { c: 'd' } })
const otherError = new HttpError(502, 'WHATEVER', { expose: true })

t.strictSame(clientError.status, 404)
t.strictSame(clientError.statusCode, 404)
t.strictSame(clientError.error, 'Not Found')
t.strictSame(clientError.errorPhrase, 'Not found.')
t.ok(clientError.isClientError)
t.notOk(clientError.isServerError)
t.strictSame(clientError.statusClass, 400)
t.ok(clientError.expose)
t.same(clientError.headers, { a: 'b' })

t.strictSame(serverError.status, 502)
t.strictSame(serverError.statusCode, 502)
t.strictSame(serverError.error, 'Bad Gateway')
t.strictSame(serverError.errorPhrase, 'Bad gateway.')
t.notOk(serverError.isClientError)
t.ok(serverError.isServerError)
t.strictSame(serverError.statusClass, 500)
t.notOk(serverError.expose)
t.same(serverError.headers, { c: 'd' })

t.ok(otherError.expose)
t.same(otherError.headers, {})
deepStrictEqual(clientError.status, 404)
deepStrictEqual(clientError.statusCode, 404)
deepStrictEqual(clientError.error, 'Not Found')
deepStrictEqual(clientError.errorPhrase, 'Not found.')
ok(clientError.isClientError)
ok(!clientError.isServerError)
deepStrictEqual(clientError.statusClass, 400)
ok(clientError.expose)
deepStrictEqual(clientError.headers, { a: 'b' })

deepStrictEqual(serverError.status, 502)
deepStrictEqual(serverError.statusCode, 502)
deepStrictEqual(serverError.error, 'Bad Gateway')
deepStrictEqual(serverError.errorPhrase, 'Bad gateway.')
ok(!serverError.isClientError)
ok(serverError.isServerError)
deepStrictEqual(serverError.statusClass, 500)
ok(!serverError.expose)
deepStrictEqual(serverError.headers, { c: 'd' })

ok(otherError.expose)
deepStrictEqual(otherError.headers, {})
})

t.test('it should accept multiple invocations styles', t => {
t.plan(4)

await test('it should accept multiple invocations styles', () => {
const error = new HttpError('NotFound', { code: 'CODE', message: 'WHATEVER', key1: 'value1' })

t.equal(error.status, 404)
t.equal(error.code, 'CODE')
t.equal(error.message, 'WHATEVER')
t.equal(error.key1, 'value1')
deepStrictEqual(error.status, 404)
deepStrictEqual(error.code, 'CODE')
deepStrictEqual(error.message, 'WHATEVER')
deepStrictEqual(error.key1, 'value1')
})

t.test('it constrain number and expose to the expected format', t => {
t.plan(4)

t.equal(new HttpError(200).status, 500)
t.equal(new HttpError(800).status, 500)
t.equal(new HttpError('OTHER').status, 500)
t.notOk(new HttpError(200, { expose: 'NO' }).expose)
await test('it constrain number and expose to the expected format', () => {
deepStrictEqual(new HttpError(200).status, 500)
deepStrictEqual(new HttpError(800).status, 500)
deepStrictEqual(new HttpError('OTHER').status, 500)
ok(!new HttpError(200, { expose: 'NO' }).expose)
})

t.test('.serialize should correctly serialize the error', t => {
t.plan(3)

await test('.serialize should correctly serialize the error', () => {
const error = new HttpError(404, 'WHATEVER', { key1: 'value1' })
error.stack = '1\n2'

t.same(error.serialize(), {
deepStrictEqual(error.serialize(), {
statusCode: 404,
error: 'Not Found',
message: 'WHATEVER'
})

t.same(error.serialize(true), {
deepStrictEqual(error.serialize(true), {
statusCode: 404,
error: 'Not Found',
message: 'WHATEVER',
key1: 'value1',
stack: ['2']
})

t.same(error.serialize(true, true), {
deepStrictEqual(error.serialize(true, true), {
statusCode: 404,
error: 'Not Found',
message: 'WHATEVER',
Expand All @@ -104,43 +93,35 @@ t.test('HttpError', t => {
})
})

t.test('createError', t => {
t.plan(1)

t.test('it should create an error', t => {
t.plan(7)

test('createError', async () => {
await test('it should create an error', () => {
const error = createError(404, 'WHATEVER', { key1: 'value1' })
const otherError = createError(404, 'WHATEVER', { key: 'ignored' })

t.type(error, HttpError)
t.type(error, Error)
t.strictSame(error.message, 'WHATEVER')
t.strictSame(error.key1, 'value1')
ok(error instanceof HttpError)
ok(error instanceof Error)
deepStrictEqual(error.message, 'WHATEVER')
deepStrictEqual(error.key1, 'value1')

t.type(otherError, HttpError)
t.type(otherError, Error)
t.strictSame(otherError.message, 'WHATEVER')
ok(otherError instanceof HttpError)
ok(otherError instanceof Error)
deepStrictEqual(otherError.message, 'WHATEVER')
})
})

t.test('isHttpError', t => {
t.plan(1)

t.test('it should correctly detect HTTP error duck typing', t => {
t.plan(11)

t.ok(isHttpError(createError(404, 'WHATEVER', { key1: 'value1' })))
t.ok(isHttpError(createHttpError(404, 'WHATEVER', { key1: 'value1' })))
t.ok(isHttpError(new createHttpError.NotFound('WHATEVER')))
t.ok(isHttpError({ status: 404, statusCode: 404, expose: true }))

t.notOk(isHttpError(null))
t.notOk(isHttpError(undefined))
t.notOk(isHttpError('MESSAGE'))
t.notOk(isHttpError(123))
t.notOk(isHttpError({}))
t.notOk(isHttpError({ status: 404, statusCode: 405, expose: true }))
t.notOk(isHttpError({ status: 404, statusCode: 404, expose: 'WHATEVER' }))
test('isHttpError', async () => {
await test('it should correctly detect HTTP error duck typing', () => {
ok(isHttpError(createError(404, 'WHATEVER', { key1: 'value1' })))
ok(isHttpError(createHttpError(404, 'WHATEVER', { key1: 'value1' })))
ok(isHttpError(new createHttpError.NotFound('WHATEVER')))
ok(isHttpError({ status: 404, statusCode: 404, expose: true }))

ok(!isHttpError(null))
ok(!isHttpError(undefined))
ok(!isHttpError('MESSAGE'))
ok(!isHttpError(123))
ok(!isHttpError({}))
ok(!isHttpError({ status: 404, statusCode: 405, expose: true }))
ok(!isHttpError({ status: 404, statusCode: 404, expose: 'WHATEVER' }))
})
})
7 changes: 0 additions & 7 deletions test/config/tap.yml

This file was deleted.

0 comments on commit 6663606

Please sign in to comment.