Skip to content

Commit 4a1cf26

Browse files
committed
rework test
1 parent 4e6a67e commit 4a1cf26

File tree

1 file changed

+68
-49
lines changed

1 file changed

+68
-49
lines changed

test/instanceof.test.js

Lines changed: 68 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -55,62 +55,42 @@ test('check if createError creates an Error with the right BaseConstructor, whic
5555
t.assert.ok(err instanceof SyntaxError === false)
5656
})
5757

58-
test('instanceof', async (t) => {
58+
// for more information see https://github.com/fastify/fastify-error/pull/86#issuecomment-1301466407
59+
test('ensure that instanceof works accross different installations of the fastify-error module', async (t) => {
5960
const assertsPlanned = 5
6061
t.plan(assertsPlanned)
6162

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'), `
63+
// We need to create a test environment where fastify-error is installed in two different locations
64+
// and then we will check if the error created in one location is instanceof the error created in the other location
65+
// This is done by creating a test directory with the following structure:
66+
67+
// /
68+
// ├── index.js
69+
// └── node_modules/
70+
// ├── fastify-error/
71+
// │ └── index.js
72+
// └── dep/
73+
// ├── index.js
74+
// └── node_modules/
75+
// └── fastify-error/
76+
// └── index.js
77+
78+
const testCwd = path.resolve(os.tmpdir(), `fastify-error-instanceof-test-${Math.random().toString(36).substring(2, 15)}`)
79+
fs.mkdirSync(testCwd, { recursive: true })
80+
81+
// Create the index.js. It will be executed as a forked process, so we need to
82+
// use process.send to send messages back to the parent process.
83+
fs.writeFileSync(path.resolve(testCwd, 'index.js'), `
8684
'use strict'
8785
88-
const { createError } = require('fastify-error')
89-
90-
const Boom = createError('Boom', 'Boom', 500)
91-
const ChildBoom = createError('ChildBoom', 'Boom', 500, Boom)
92-
93-
module.exports.foo = function foo () {
94-
throw new ChildBoom('foo go Boom')
95-
}
96-
`)
86+
const path = require('node:path')
87+
const { createError, FastifyError } = require('fastify-error')
88+
const { foo } = require('dep')
9789
98-
fs.writeFileSync(path.resolve(testCwd, 'package.json'), `
99-
{
100-
"name": "test",
101-
"version": "1.0.0",
102-
"description": "main",
103-
"main": "index.js",
104-
"dependencies": {
105-
"fastify-error": "1.0.0",
106-
"main": "1.0.0"
107-
}
90+
// Ensure that fastify-error is required from the node_modules directory of the test-project
91+
if (require.resolve('fastify-error') !== path.resolve('${testCwd}', 'node_modules', 'fastify-error', 'index.js')) {
92+
throw new Error('fastify-error should be required from the node_modules directory of the test-project')
10893
}
109-
`)
110-
fs.writeFileSync(path.resolve(testCwd, 'index.js'), `
111-
'use strict'
112-
const { createError, FastifyError } = require('fastify-error')
113-
const { foo } = require('main')
11494
11595
const Boom = createError('Boom', 'Boom', 500)
11696
const ChildBoom = createError('ChildBoom', 'Boom', 500, Boom)
@@ -127,6 +107,38 @@ test('instanceof', async (t) => {
127107
}
128108
`)
129109

110+
// Create /node_modules/fastify-error directory
111+
// Copy the index.js file to the fastify-error directory
112+
fs.mkdirSync(path.resolve(testCwd, 'node_modules', 'fastify-error'), { recursive: true })
113+
fs.copyFileSync(path.resolve(process.cwd(), 'index.js'), path.resolve(testCwd, 'node_modules', 'fastify-error', 'index.js'))
114+
115+
// Create /node_modules/dep/node_modules/fastify-error directory
116+
// Copy the index.js to the fastify-error directory
117+
fs.mkdirSync(path.resolve(testCwd, 'node_modules', 'dep', 'node_modules', 'fastify-error'), { recursive: true })
118+
fs.copyFileSync(path.resolve(process.cwd(), 'index.js'), path.resolve(testCwd, 'node_modules', 'dep', 'node_modules', 'fastify-error', 'index.js'))
119+
120+
// Create /node_modules/dep/index.js. It will export a function foo which will
121+
// throw an error when called. The error will be an instance of ChildBoom, created
122+
// by the fastify-error module in the node_modules directory of dep.
123+
fs.writeFileSync(path.resolve(testCwd, 'node_modules', 'dep', 'index.js'), `
124+
'use strict'
125+
126+
const path = require('node:path')
127+
const { createError } = require('fastify-error')
128+
129+
// Ensure that fastify-error is required from the node_modules directory of dep
130+
if (require.resolve('fastify-error') !== path.resolve('${testCwd}','node_modules', 'dep', 'node_modules', 'fastify-error', 'index.js')) {
131+
throw new Error('fastify-error should be required from the node_modules directory of dep')
132+
}
133+
134+
const Boom = createError('Boom', 'Boom', 500)
135+
const ChildBoom = createError('ChildBoom', 'Boom', 500, Boom)
136+
137+
module.exports.foo = function foo () {
138+
throw new ChildBoom('foo go Boom')
139+
}
140+
`)
141+
130142
const finishedPromise = {
131143
promise: undefined,
132144
reject: undefined,
@@ -180,4 +192,11 @@ test('instanceof', async (t) => {
180192
})
181193

182194
await finishedPromise.promise
195+
196+
// Cleanup
197+
// As we are creating the test-setup on the fly in the /tmp directory, we can remove it
198+
// safely when we are done. It is not relevant for the test if the deletion fails.
199+
try {
200+
fs.rmSync(testCwd, { recursive: true, force: true })
201+
} catch {}
183202
})

0 commit comments

Comments
 (0)