Skip to content

Commit

Permalink
use tap instead of ava, 100% test coverage, disable package-lock gege…
Browse files Browse the repository at this point in the history
…neration, etc.. (#82)
  • Loading branch information
Uzlopak authored Aug 17, 2022
1 parent 8113220 commit 51b23d4
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 134 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ jobs:
uses: fastify/workflows/.github/workflows/plugins-ci.yml@v3
with:
license-check: true
lint: true
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
2 changes: 2 additions & 0 deletions .taprc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
files:
- test/**/*.test.js
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
"version": "3.0.0",
"description": "A small utility, used by Fastify itself, for generating consistent error objects across your codebase and plugins.",
"main": "index.js",
"types": "index.d.ts",
"types": "types/index.d.ts",
"scripts": {
"test": "standard && ava -v && tsd"
"lint": "standard",
"test": "npm run test:unit && npm run test:typescript",
"test:unit": "tap",
"test:typescript": "tsd"
},
"repository": {
"type": "git",
Expand All @@ -24,11 +27,9 @@
},
"homepage": "https://github.com/fastify/fastify-error#readme",
"devDependencies": {
"@types/node": "^18.0.0",
"ava": "^4.0.1",
"standard": "^17.0.0",
"tsd": "^0.22.0",
"typescript": "^4.1.3"
"tap": "^16.0.0",
"tsd": "^0.22.0"
},
"tsd": {
"compilerOptions": {
Expand Down
127 changes: 0 additions & 127 deletions test.js

This file was deleted.

156 changes: 156 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
'use strict'

/* eslint no-prototype-builtins: 0 */

const test = require('tap').test
const createError = require('..')

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)
})

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)
})

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')
})

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)
})

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')
})

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)
})

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)
})

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)
})

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)
})

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

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

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

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

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)
})

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')
})

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)
})
File renamed without changes.
2 changes: 1 addition & 1 deletion index.test-d.ts → types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import createError, { FastifyError, FastifyErrorConstructor } from './'
import createError, { FastifyError, FastifyErrorConstructor } from '..'
import { expectType } from 'tsd'

const CustomError = createError('ERROR_CODE', 'message')
Expand Down

0 comments on commit 51b23d4

Please sign in to comment.