Skip to content

Commit 21c11fc

Browse files
committed
add test
1 parent 49ef118 commit 21c11fc

File tree

2 files changed

+168
-18
lines changed

2 files changed

+168
-18
lines changed

test/index.test.js

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -222,24 +222,6 @@ test('Create an error with last argument null', (t) => {
222222
t.assert.ifError(err.cause)
223223
})
224224

225-
test('check if instanceof works', (t) => {
226-
t.plan(7)
227-
228-
const NewError = createError('CODE', 'Not available')
229-
const err = NewError()
230-
231-
const FastifySyntaxError = createError('CODE', 'Not available', 500, SyntaxError)
232-
const syntaxErr = new FastifySyntaxError()
233-
234-
t.assert.ok(err instanceof Error)
235-
t.assert.ok(err instanceof FastifyError)
236-
t.assert.ok(err instanceof SyntaxError === false)
237-
t.assert.ok(new Error() instanceof FastifyError === false)
238-
t.assert.ok(syntaxErr instanceof SyntaxError)
239-
t.assert.ok(syntaxErr instanceof FastifyError)
240-
t.assert.ok(syntaxErr instanceof FastifySyntaxError)
241-
})
242-
243225
test('check if FastifyError is instantiable', (t) => {
244226
t.plan(2)
245227

test/instanceof.test.js

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
'use strict'
2+
3+
const cp = require('node:child_process')
4+
const fs = require('node:fs')
5+
const path = require('node:path')
6+
const os = require('node:os')
7+
const test = require('node:test')
8+
const { createError, FastifyError } = require('..')
9+
10+
test('check if createError creates an Error which is instanceof Error', (t) => {
11+
t.plan(3)
12+
13+
const CustomFastifyError = createError('CODE', 'Not available')
14+
const err = CustomFastifyError()
15+
16+
t.assert.ok(err instanceof Error)
17+
t.assert.ok(err instanceof SyntaxError === false)
18+
t.assert.ok(err instanceof TypeError === false)
19+
})
20+
21+
test('check if createError creates an Error which is instanceof FastifyError', (t) => {
22+
t.plan(4)
23+
24+
const CustomFastifyError = createError('CODE', 'Not available')
25+
const err = CustomFastifyError()
26+
27+
t.assert.ok(err instanceof Error)
28+
t.assert.ok(err instanceof FastifyError)
29+
t.assert.ok(err instanceof SyntaxError === false)
30+
t.assert.ok(err instanceof TypeError === false)
31+
})
32+
33+
test('check if createError creates an Error with the right BaseConstructor', (t) => {
34+
t.plan(2)
35+
36+
const CustomFastifyError = createError('CODE', 'Not available', 500, TypeError)
37+
const err = CustomFastifyError()
38+
39+
t.assert.ok(err instanceof Error)
40+
t.assert.ok(err instanceof TypeError)
41+
})
42+
43+
test('check if createError creates an Error with the right BaseConstructor, which is a FastifyError', (t) => {
44+
t.plan(6)
45+
46+
const BaseFastifyError = createError('CODE', 'Not available', 500, TypeError)
47+
const CustomFastifyError = createError('CODE', 'Not available', 500, BaseFastifyError)
48+
const err = CustomFastifyError()
49+
50+
t.assert.ok(err instanceof Error)
51+
t.assert.ok(err instanceof TypeError)
52+
t.assert.ok(err instanceof FastifyError)
53+
t.assert.ok(err instanceof BaseFastifyError)
54+
t.assert.ok(err instanceof CustomFastifyError)
55+
t.assert.ok(err instanceof SyntaxError === false)
56+
})
57+
58+
test('instanceof', async (t) => {
59+
const assertsPlanned = 2
60+
t.plan(assertsPlanned)
61+
62+
const testCwd = path.resolve(os.tmpdir())
63+
64+
fs.mkdirSync(path.resolve(testCwd, 'node_modules', 'fastify-error'), { recursive: true })
65+
66+
fs.copyFileSync(path.resolve(process.cwd(), 'index.js'), path.resolve(testCwd, 'node_modules', 'fastify-error', 'index.js'))
67+
fs.copyFileSync(path.resolve(process.cwd(), 'package.json'), path.resolve(testCwd, 'node_modules', 'fastify-error', 'package.json'))
68+
69+
fs.mkdirSync(path.resolve(testCwd, 'node_modules', 'main', 'node_modules', 'fastify-error'), { recursive: true })
70+
71+
fs.copyFileSync(path.resolve(process.cwd(), 'index.js'), path.resolve(testCwd, 'node_modules', 'main', 'node_modules', 'fastify-error', 'index.js'))
72+
fs.copyFileSync(path.resolve(process.cwd(), 'package.json'), path.resolve(testCwd, 'node_modules', 'main', 'node_modules', 'fastify-error', 'package.json'))
73+
74+
fs.writeFileSync(path.resolve(testCwd, 'node_modules', 'main', 'package.json'), `
75+
{
76+
"name": "main",
77+
"version": "1.0.0",
78+
"description": "main",
79+
"main": "index.js",
80+
"dependencies": {
81+
"fastify-error": "1.0.0"
82+
}
83+
}
84+
`)
85+
fs.writeFileSync(path.resolve(testCwd, 'node_modules', 'main', 'index.js'), `
86+
'use strict'
87+
88+
const { createError } = require('fastify-error')
89+
90+
const Boom = createError('Boom', 'Boom', 500)
91+
92+
module.exports.foo = function foo () {
93+
throw new Boom('foo go Boom')
94+
}
95+
`)
96+
97+
fs.writeFileSync(path.resolve(testCwd, 'package.json'), `
98+
{
99+
"name": "test",
100+
"version": "1.0.0",
101+
"description": "main",
102+
"main": "index.js",
103+
"dependencies": {
104+
"fastify-error": "1.0.0",
105+
"main": "1.0.0"
106+
}
107+
}
108+
`)
109+
fs.writeFileSync(path.resolve(testCwd, 'index.js'), `
110+
'use strict'
111+
const { createError, FastifyError } = require('fastify-error')
112+
const { foo } = require('main')
113+
114+
const Boom = createError('Boom', 'Boom', 500)
115+
116+
try {
117+
foo()
118+
} catch (err) {
119+
process.send(err instanceof FastifyError)
120+
process.send(err instanceof Boom)
121+
}
122+
`)
123+
124+
const finishedPromise = {
125+
promise: undefined,
126+
reject: undefined,
127+
resolve: undefined,
128+
}
129+
130+
finishedPromise.promise = new Promise((resolve, reject) => {
131+
finishedPromise.resolve = resolve
132+
finishedPromise.reject = reject
133+
})
134+
135+
const child = cp.fork(path.resolve(testCwd, 'index.js'), {
136+
cwd: testCwd,
137+
stdio: 'pipe',
138+
env: {
139+
...process.env,
140+
NODE_OPTIONS: '--no-warnings'
141+
},
142+
})
143+
144+
let messageCount = 0
145+
child.on('message', message => {
146+
try {
147+
switch (messageCount) {
148+
case 0:
149+
t.assert.strictEqual(message, true, 'instanceof FastifyError')
150+
break
151+
case 1:
152+
t.assert.strictEqual(message, false, 'instanceof Boom')
153+
break
154+
}
155+
if (++messageCount === assertsPlanned) {
156+
finishedPromise.resolve()
157+
}
158+
} catch (err) {
159+
finishedPromise.reject(err)
160+
}
161+
})
162+
163+
child.on('error', err => {
164+
finishedPromise.reject(err)
165+
})
166+
167+
await finishedPromise.promise
168+
})

0 commit comments

Comments
 (0)