Skip to content

Commit

Permalink
fix: migrate from tap to node test and c8 (#130)
Browse files Browse the repository at this point in the history
* fix: migrate from tap to node test and c8

* pr feedback

* fix: test plan to 7

* fix: remove assert and just t.assert
  • Loading branch information
dancastillo authored Aug 6, 2024
1 parent 5cde815 commit f639471
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 86 deletions.
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 @@ -9,7 +9,7 @@
"lint": "standard",
"lint:fix": "standard --fix",
"test": "npm run test:unit && npm run test:typescript",
"test:unit": "tap",
"test:unit": "c8 node --test",
"test:typescript": "tsd"
},
"repository": {
Expand All @@ -30,8 +30,8 @@
"homepage": "https://github.com/fastify/fastify-error#readme",
"devDependencies": {
"benchmark": "^2.1.4",
"c8": "^10.1.2",
"standard": "^17.1.0",
"tap": "^18.7.1",
"tsd": "^0.31.0"
},
"tsd": {
Expand Down
173 changes: 91 additions & 82 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,184 +1,193 @@
'use strict'

const { test } = require('tap')
const test = require('node:test')
const createError = require('..')

test('Create error with zero parameter', t => {
test('Create error with zero parameter', (t) => {
t.plan(6)

const NewError = createError('CODE', 'Not available')
const err = new NewError()
t.ok(err instanceof Error)
t.equal(err.name, 'FastifyError')
t.equal(err.message, 'Not available')
t.equal(err.code, 'CODE')
t.equal(err.statusCode, 500)
t.ok(err.stack)
t.assert.ok(err instanceof Error)
t.assert.equal(err.name, 'FastifyError')
t.assert.equal(err.message, 'Not available')
t.assert.equal(err.code, 'CODE')
t.assert.equal(err.statusCode, 500)
t.assert.ok(err.stack)
})

test('Create error with 1 parameter', t => {
test('Create error with 1 parameter', (t) => {
t.plan(6)

const NewError = createError('CODE', 'hey %s')
const err = new NewError('alice')
t.ok(err instanceof Error)
t.equal(err.name, 'FastifyError')
t.equal(err.message, 'hey alice')
t.equal(err.code, 'CODE')
t.equal(err.statusCode, 500)
t.ok(err.stack)
t.assert.equal(err.name, 'FastifyError')
t.assert.ok(err instanceof Error)
t.assert.equal(err.message, 'hey alice')
t.assert.equal(err.code, 'CODE')
t.assert.equal(err.statusCode, 500)
t.assert.ok(err.stack)
})

test('Create error with 1 parameter set to undefined', t => {
test('Create error with 1 parameter set to undefined', (t) => {
t.plan(1)

const NewError = createError('CODE', 'hey %s')
const err = new NewError(undefined)
t.equal(err.message, 'hey undefined')
t.assert.equal(err.message, 'hey undefined')
})

test('Create error with 2 parameters', (t) => {
t.plan(6)

const NewError = createError('CODE', 'hey %s, I like your %s')
const err = new NewError('alice', 'attitude')
t.ok(err instanceof Error)
t.equal(err.name, 'FastifyError')
t.equal(err.message, 'hey alice, I like your attitude')
t.equal(err.code, 'CODE')
t.equal(err.statusCode, 500)
t.ok(err.stack)
t.assert.ok(err instanceof Error)
t.assert.equal(err.name, 'FastifyError')
t.assert.equal(err.message, 'hey alice, I like your attitude')
t.assert.equal(err.code, 'CODE')
t.assert.equal(err.statusCode, 500)
t.assert.ok(err.stack)
})

test('Create error with 2 parameters set to undefined', t => {
test('Create error with 2 parameters set to undefined', (t) => {
t.plan(1)

const NewError = createError('CODE', 'hey %s, I like your %s')
const err = new NewError(undefined, undefined)
t.equal(err.message, 'hey undefined, I like your undefined')
t.assert.equal(err.message, 'hey undefined, I like your undefined')
})

test('Create error with 3 parameters', t => {
test('Create error with 3 parameters', (t) => {
t.plan(6)

const NewError = createError('CODE', 'hey %s, I like your %s %s')
const err = new NewError('alice', 'attitude', 'see you')
t.ok(err instanceof Error)
t.equal(err.name, 'FastifyError')
t.equal(err.message, 'hey alice, I like your attitude see you')
t.equal(err.code, 'CODE')
t.equal(err.statusCode, 500)
t.ok(err.stack)
t.assert.ok(err instanceof Error)
t.assert.equal(err.name, 'FastifyError')
t.assert.equal(err.message, 'hey alice, I like your attitude see you')
t.assert.equal(err.code, 'CODE')
t.assert.equal(err.statusCode, 500)
t.assert.ok(err.stack)
})

test('Create error with 3 parameters set to undefined', t => {
test('Create error with 3 parameters set to undefined', (t) => {
t.plan(4)

const NewError = createError('CODE', 'hey %s, I like your %s %s')
const err = new NewError(undefined, undefined, undefined)
t.equal(err.message, 'hey undefined, I like your undefined undefined')
t.equal(err.code, 'CODE')
t.equal(err.statusCode, 500)
t.ok(err.stack)
t.assert.equal(err.message, 'hey undefined, I like your undefined undefined')
t.assert.equal(err.code, 'CODE')
t.assert.equal(err.statusCode, 500)
t.assert.ok(err.stack)
})

test('Create error with 4 parameters set to undefined', t => {
test('Create error with 4 parameters set to undefined', (t) => {
t.plan(4)

const NewError = createError('CODE', 'hey %s, I like your %s %s and %s')
const err = new NewError(undefined, undefined, undefined, undefined)
t.equal(err.message, 'hey undefined, I like your undefined undefined and undefined')
t.equal(err.code, 'CODE')
t.equal(err.statusCode, 500)
t.ok(err.stack)
t.assert.equal(
err.message,
'hey undefined, I like your undefined undefined and undefined'
)
t.assert.equal(err.code, 'CODE')
t.assert.equal(err.statusCode, 500)
t.assert.ok(err.stack)
})

test('Create error with no statusCode property', t => {
test('Create error with no statusCode property', (t) => {
t.plan(6)

const NewError = createError('CODE', 'hey %s', 0)
const err = new NewError('dude')
t.ok(err instanceof Error)
t.equal(err.name, 'FastifyError')
t.equal(err.message, 'hey dude')
t.equal(err.code, 'CODE')
t.equal(err.statusCode, undefined)
t.ok(err.stack)
t.assert.ok(err instanceof Error)
t.assert.equal(err.name, 'FastifyError')
t.assert.equal(err.message, 'hey dude')
t.assert.equal(err.code, 'CODE')
t.assert.equal(err.statusCode, undefined)
t.assert.ok(err.stack)
})

test('Should throw when error code has no fastify code', t => {
test('Should throw when error code has no fastify code', (t) => {
t.plan(1)

t.throws(() => createError(), new Error('Fastify error code must not be empty'))
t.assert.throws(
() => createError(),
new Error('Fastify error code must not be empty')
)
})

test('Should throw when error code has no message', t => {
t.plan(1)

t.throws(() => createError('code'), new Error('Fastify error message must not be empty'))
test('Should throw when error code has no message', (t) => {
t.assert.throws(
() => createError('code'),
new Error('Fastify error message must not be empty')
)
})

test('Create error with different base', t => {
test('Create error with different base', (t) => {
t.plan(7)

const NewError = createError('CODE', 'hey %s', 500, TypeError)
const err = new NewError('dude')
t.ok(err instanceof Error)
t.ok(err instanceof TypeError)
t.equal(err.name, 'FastifyError')
t.equal(err.message, 'hey dude')
t.equal(err.code, 'CODE')
t.equal(err.statusCode, 500)
t.ok(err.stack)
t.assert.ok(err instanceof Error)
t.assert.ok(err instanceof TypeError)
t.assert.equal(err.name, 'FastifyError')
t.assert.equal(err.message, 'hey dude')
t.assert.equal(err.code, 'CODE')
t.assert.equal(err.statusCode, 500)
t.assert.ok(err.stack)
})

test('FastifyError.toString returns code', t => {
test('FastifyError.toString returns code', (t) => {
t.plan(1)

const NewError = createError('CODE', 'foo')
const err = new NewError()
t.equal(err.toString(), 'FastifyError [CODE]: foo')
t.assert.equal(err.toString(), 'FastifyError [CODE]: foo')
})

test('Create the error without the new keyword', t => {
test('Create the error without the new keyword', (t) => {
t.plan(6)

const NewError = createError('CODE', 'Not available')
const err = NewError()
t.ok(err instanceof Error)
t.equal(err.name, 'FastifyError')
t.equal(err.message, 'Not available')
t.equal(err.code, 'CODE')
t.equal(err.statusCode, 500)
t.ok(err.stack)
t.assert.ok(err instanceof Error)
t.assert.equal(err.name, 'FastifyError')
t.assert.equal(err.message, 'Not available')
t.assert.equal(err.code, 'CODE')
t.assert.equal(err.statusCode, 500)
t.assert.ok(err.stack)
})

test('Create an error with cause', t => {
test('Create an error with cause', (t) => {
t.plan(2)

const cause = new Error('HEY')
const NewError = createError('CODE', 'Not available')
const err = NewError({ cause })

t.ok(err instanceof Error)
t.equal(err.cause, cause)
t.assert.ok(err instanceof Error)
t.assert.equal(err.cause, cause)
})

test('Create an error with cause and message', t => {
test('Create an error with cause and message', (t) => {
t.plan(2)

const cause = new Error('HEY')
const NewError = createError('CODE', 'Not available: %s')
const err = NewError('foo', { cause })

t.ok(err instanceof Error)
t.equal(err.cause, cause)
t.assert.ok(err instanceof Error)
t.assert.equal(err.cause, cause)
})

test('Create an error with last argument null', t => {
test('Create an error with last argument null', (t) => {
t.plan(2)

const cause = new Error('HEY')
const NewError = createError('CODE', 'Not available')
const err = NewError({ cause }, null)

t.ok(err instanceof Error)
t.notOk(err.cause)
t.assert.ok(err instanceof Error)
t.assert.ifError(err.cause)
})

0 comments on commit f639471

Please sign in to comment.