forked from fastify/fastify-error
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
106 lines (94 loc) · 2.94 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
'use strict'
/* eslint no-prototype-builtins: 0 */
const test = require('ava')
const createError = require('./')
test('Create error with zero parameter', t => {
const NewError = createError('CODE', 'Not available')
const err = new NewError()
t.true(err instanceof Error)
t.is(err.name, 'FastifyError')
t.is(err.message, 'Not available')
t.is(err.code, 'CODE')
t.is(err.statusCode, 500)
t.truthy(err.stack)
})
test('Create error with 1 parameter', t => {
const NewError = createError('CODE', 'hey %s')
const err = new NewError('alice')
t.true(err instanceof Error)
t.is(err.name, 'FastifyError')
t.is(err.message, 'hey alice')
t.is(err.code, 'CODE')
t.is(err.statusCode, 500)
t.truthy(err.stack)
})
test('Create error with 2 parameters', t => {
const NewError = createError('CODE', 'hey %s, I like your %s')
const err = new NewError('alice', 'attitude')
t.true(err instanceof Error)
t.is(err.name, 'FastifyError')
t.is(err.message, 'hey alice, I like your attitude')
t.is(err.code, 'CODE')
t.is(err.statusCode, 500)
t.truthy(err.stack)
})
test('Create error with 3 parameters', t => {
const NewError = createError('CODE', 'hey %s, I like your %s %s')
const err = new NewError('alice', 'attitude', 'see you')
t.true(err instanceof Error)
t.is(err.name, 'FastifyError')
t.is(err.message, 'hey alice, I like your attitude see you')
t.is(err.code, 'CODE')
t.is(err.statusCode, 500)
t.truthy(err.stack)
})
test('Create error with no statusCode property', t => {
const NewError = createError('CODE', 'hey %s', 0)
const err = new NewError('dude')
t.true(err instanceof Error)
t.is(err.name, 'FastifyError')
t.is(err.message, 'hey dude')
t.is(err.code, 'CODE')
t.is(err.statusCode, undefined)
t.truthy(err.stack)
})
test('Should throw when error code has no fastify code', t => {
try {
createError()
} catch (err) {
t.is(err.message, 'Fastify error code must not be empty')
}
})
test('Should throw when error code has no message', t => {
try {
createError('code')
} catch (err) {
t.is(err.message, 'Fastify error message must not be empty')
}
})
test('Create error with different base', t => {
const NewError = createError('CODE', 'hey %s', 500, TypeError)
const err = new NewError('dude')
t.true(err instanceof Error)
t.true(err instanceof TypeError)
t.is(err.name, 'FastifyError')
t.is(err.message, 'hey dude')
t.is(err.code, 'CODE')
t.is(err.statusCode, 500)
t.truthy(err.stack)
})
test('FastifyError.toString returns code', t => {
const NewError = createError('CODE', 'foo')
const err = new NewError()
t.is(err.toString(), 'FastifyError [CODE]: foo')
})
test('Create the error without the new keyword', t => {
const NewError = createError('CODE', 'Not available')
const err = NewError()
t.true(err instanceof Error)
t.is(err.name, 'FastifyError')
t.is(err.message, 'Not available')
t.is(err.code, 'CODE')
t.is(err.statusCode, 500)
t.truthy(err.stack)
})