Skip to content

Commit 2f3de31

Browse files
committed
test(importErrorHandler): add tests for import error handler
1 parent f4431cf commit 2f3de31

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import Module from 'node:module'
2+
3+
import {describe, expect, it} from 'vitest'
4+
5+
import {setupImportErrorHandler} from '../../../src/_internal/cli/util/importErrorHandler'
6+
7+
interface ModuleConstructor {
8+
_load(request: string, parent: Module | undefined, isMain: boolean): any
9+
}
10+
11+
describe('setupImportErrorHandler', () => {
12+
it('should handle themer.sanity.build URL imports', () => {
13+
const handler = setupImportErrorHandler()
14+
15+
const ModuleConstructor = Module as unknown as ModuleConstructor
16+
17+
// Try to load a themer.sanity.build URL (which would normally fail)
18+
const result = ModuleConstructor._load(
19+
'https://themer.sanity.build/api/hues?default=0078ff',
20+
undefined,
21+
false,
22+
)
23+
24+
expect(result).toBeDefined()
25+
expect(result.__esModule).toBe(true)
26+
expect(result.default).toBeDefined()
27+
expect(result.someProperty.deepProperty).toBeDefined()
28+
29+
handler.cleanup()
30+
})
31+
32+
it('should re-throw errors for non-themer URLs', () => {
33+
const handler = setupImportErrorHandler()
34+
35+
const ModuleConstructor = Module as unknown as ModuleConstructor
36+
37+
expect(() => {
38+
ModuleConstructor._load(
39+
'https://example.com/this-module-definitely-does-not-exist-xyz',
40+
undefined,
41+
false,
42+
)
43+
}).toThrow()
44+
45+
handler.cleanup()
46+
})
47+
48+
it('should restore original Module._load after cleanup', () => {
49+
const ModuleConstructor = Module as unknown as ModuleConstructor
50+
const originalLoad = ModuleConstructor._load
51+
52+
const handler = setupImportErrorHandler()
53+
54+
expect(ModuleConstructor._load).not.toBe(originalLoad)
55+
handler.cleanup()
56+
expect(ModuleConstructor._load).toBe(originalLoad)
57+
})
58+
})

0 commit comments

Comments
 (0)