Skip to content

Commit 51b23d4

Browse files
authored
use tap instead of ava, 100% test coverage, disable package-lock gegeneration, etc.. (#82)
1 parent 8113220 commit 51b23d4

File tree

8 files changed

+168
-134
lines changed

8 files changed

+168
-134
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ jobs:
1515
uses: fastify/workflows/.github/workflows/plugins-ci.yml@v3
1616
with:
1717
license-check: true
18+
lint: true

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

.taprc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
files:
2+
- test/**/*.test.js

package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
"version": "3.0.0",
44
"description": "A small utility, used by Fastify itself, for generating consistent error objects across your codebase and plugins.",
55
"main": "index.js",
6-
"types": "index.d.ts",
6+
"types": "types/index.d.ts",
77
"scripts": {
8-
"test": "standard && ava -v && tsd"
8+
"lint": "standard",
9+
"test": "npm run test:unit && npm run test:typescript",
10+
"test:unit": "tap",
11+
"test:typescript": "tsd"
912
},
1013
"repository": {
1114
"type": "git",
@@ -24,11 +27,9 @@
2427
},
2528
"homepage": "https://github.com/fastify/fastify-error#readme",
2629
"devDependencies": {
27-
"@types/node": "^18.0.0",
28-
"ava": "^4.0.1",
2930
"standard": "^17.0.0",
30-
"tsd": "^0.22.0",
31-
"typescript": "^4.1.3"
31+
"tap": "^16.0.0",
32+
"tsd": "^0.22.0"
3233
},
3334
"tsd": {
3435
"compilerOptions": {

test.js

Lines changed: 0 additions & 127 deletions
This file was deleted.

test/index.test.js

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
'use strict'
2+
3+
/* eslint no-prototype-builtins: 0 */
4+
5+
const test = require('tap').test
6+
const createError = require('..')
7+
8+
test('Create error with zero parameter', t => {
9+
t.plan(6)
10+
11+
const NewError = createError('CODE', 'Not available')
12+
const err = new NewError()
13+
t.ok(err instanceof Error)
14+
t.equal(err.name, 'FastifyError')
15+
t.equal(err.message, 'Not available')
16+
t.equal(err.code, 'CODE')
17+
t.equal(err.statusCode, 500)
18+
t.ok(err.stack)
19+
})
20+
21+
test('Create error with 1 parameter', t => {
22+
t.plan(6)
23+
24+
const NewError = createError('CODE', 'hey %s')
25+
const err = new NewError('alice')
26+
t.ok(err instanceof Error)
27+
t.equal(err.name, 'FastifyError')
28+
t.equal(err.message, 'hey alice')
29+
t.equal(err.code, 'CODE')
30+
t.equal(err.statusCode, 500)
31+
t.ok(err.stack)
32+
})
33+
34+
test('Create error with 1 parameter set to undefined', t => {
35+
t.plan(1)
36+
37+
const NewError = createError('CODE', 'hey %s')
38+
const err = new NewError(undefined)
39+
t.equal(err.message, 'hey undefined')
40+
})
41+
42+
test('Create error with 2 parameters', (t) => {
43+
t.plan(6)
44+
45+
const NewError = createError('CODE', 'hey %s, I like your %s')
46+
const err = new NewError('alice', 'attitude')
47+
t.ok(err instanceof Error)
48+
t.equal(err.name, 'FastifyError')
49+
t.equal(err.message, 'hey alice, I like your attitude')
50+
t.equal(err.code, 'CODE')
51+
t.equal(err.statusCode, 500)
52+
t.ok(err.stack)
53+
})
54+
55+
test('Create error with 2 parameters set to undefined', t => {
56+
t.plan(1)
57+
58+
const NewError = createError('CODE', 'hey %s, I like your %s')
59+
const err = new NewError(undefined, undefined)
60+
t.equal(err.message, 'hey undefined, I like your undefined')
61+
})
62+
63+
test('Create error with 3 parameters', t => {
64+
t.plan(6)
65+
66+
const NewError = createError('CODE', 'hey %s, I like your %s %s')
67+
const err = new NewError('alice', 'attitude', 'see you')
68+
t.ok(err instanceof Error)
69+
t.equal(err.name, 'FastifyError')
70+
t.equal(err.message, 'hey alice, I like your attitude see you')
71+
t.equal(err.code, 'CODE')
72+
t.equal(err.statusCode, 500)
73+
t.ok(err.stack)
74+
})
75+
76+
test('Create error with 3 parameters set to undefined', t => {
77+
t.plan(4)
78+
79+
const NewError = createError('CODE', 'hey %s, I like your %s %s')
80+
const err = new NewError(undefined, undefined, undefined)
81+
t.equal(err.message, 'hey undefined, I like your undefined undefined')
82+
t.equal(err.code, 'CODE')
83+
t.equal(err.statusCode, 500)
84+
t.ok(err.stack)
85+
})
86+
87+
test('Create error with 4 parameters set to undefined', t => {
88+
t.plan(4)
89+
90+
const NewError = createError('CODE', 'hey %s, I like your %s %s and %s')
91+
const err = new NewError(undefined, undefined, undefined, undefined)
92+
t.equal(err.message, 'hey undefined, I like your undefined undefined and undefined')
93+
t.equal(err.code, 'CODE')
94+
t.equal(err.statusCode, 500)
95+
t.ok(err.stack)
96+
})
97+
98+
test('Create error with no statusCode property', t => {
99+
t.plan(6)
100+
101+
const NewError = createError('CODE', 'hey %s', 0)
102+
const err = new NewError('dude')
103+
t.ok(err instanceof Error)
104+
t.equal(err.name, 'FastifyError')
105+
t.equal(err.message, 'hey dude')
106+
t.equal(err.code, 'CODE')
107+
t.equal(err.statusCode, undefined)
108+
t.ok(err.stack)
109+
})
110+
111+
test('Should throw when error code has no fastify code', t => {
112+
t.plan(1)
113+
114+
t.throws(() => createError(), 'Fastify error code must not be empty')
115+
})
116+
117+
test('Should throw when error code has no message', t => {
118+
t.plan(1)
119+
120+
t.throws(() => createError('code'), 'Fastify error message must not be empty')
121+
})
122+
123+
test('Create error with different base', t => {
124+
t.plan(7)
125+
126+
const NewError = createError('CODE', 'hey %s', 500, TypeError)
127+
const err = new NewError('dude')
128+
t.ok(err instanceof Error)
129+
t.ok(err instanceof TypeError)
130+
t.equal(err.name, 'FastifyError')
131+
t.equal(err.message, 'hey dude')
132+
t.equal(err.code, 'CODE')
133+
t.equal(err.statusCode, 500)
134+
t.ok(err.stack)
135+
})
136+
137+
test('FastifyError.toString returns code', t => {
138+
t.plan(1)
139+
140+
const NewError = createError('CODE', 'foo')
141+
const err = new NewError()
142+
t.equal(err.toString(), 'FastifyError [CODE]: foo')
143+
})
144+
145+
test('Create the error without the new keyword', t => {
146+
t.plan(6)
147+
148+
const NewError = createError('CODE', 'Not available')
149+
const err = NewError()
150+
t.ok(err instanceof Error)
151+
t.equal(err.name, 'FastifyError')
152+
t.equal(err.message, 'Not available')
153+
t.equal(err.code, 'CODE')
154+
t.equal(err.statusCode, 500)
155+
t.ok(err.stack)
156+
})
File renamed without changes.

index.test-d.ts renamed to types/index.test-d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import createError, { FastifyError, FastifyErrorConstructor } from './'
1+
import createError, { FastifyError, FastifyErrorConstructor } from '..'
22
import { expectType } from 'tsd'
33

44
const CustomError = createError('ERROR_CODE', 'message')

0 commit comments

Comments
 (0)