Skip to content

Commit a5ac793

Browse files
committed
test(mockBrowser): test themer import handling
1 parent b710095 commit a5ac793

File tree

3 files changed

+95
-5
lines changed

3 files changed

+95
-5
lines changed

packages/sanity/test/cli/util/importErrorHandler.test.ts renamed to packages/sanity/src/_internal/cli/util/__tests__/importErrorHandler.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import Module from 'node:module'
22

3-
import {describe, expect, it} from 'vitest'
3+
import {describe, expect, test} from 'vitest'
44

5-
import {setupImportErrorHandler} from '../../../src/_internal/cli/util/importErrorHandler'
5+
import {setupImportErrorHandler} from '../importErrorHandler'
66

77
interface ModuleConstructor {
88
_load(request: string, parent: Module | undefined, isMain: boolean): any
99
}
1010

1111
describe('setupImportErrorHandler', () => {
12-
it('should handle themer.sanity.build URL imports', () => {
12+
test('should handle themer.sanity.build URL imports', () => {
1313
const handler = setupImportErrorHandler()
1414

1515
const ModuleConstructor = Module as unknown as ModuleConstructor
@@ -29,7 +29,7 @@ describe('setupImportErrorHandler', () => {
2929
handler.cleanup()
3030
})
3131

32-
it('should re-throw errors for non-themer URLs', () => {
32+
test('should re-throw errors for non-themer URLs', () => {
3333
const handler = setupImportErrorHandler()
3434

3535
const ModuleConstructor = Module as unknown as ModuleConstructor
@@ -45,7 +45,7 @@ describe('setupImportErrorHandler', () => {
4545
handler.cleanup()
4646
})
4747

48-
it('should restore original Module._load after cleanup', () => {
48+
test('should restore original Module._load after cleanup', () => {
4949
const ModuleConstructor = Module as unknown as ModuleConstructor
5050
const originalLoad = ModuleConstructor._load
5151

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import {Worker} from 'node:worker_threads'
2+
3+
import {describe, expect, test} from 'vitest'
4+
5+
import {type ThemerImportWorkerData} from './themerImportWorker'
6+
7+
function getImportWorker(importName: string) {
8+
const workerData: ThemerImportWorkerData = {
9+
workDir: process.cwd(),
10+
importName,
11+
}
12+
13+
const filepath = new URL('./themerImportWorker.ts', import.meta.url).pathname
14+
15+
const worker = new Worker(
16+
`
17+
const { register } = require('esbuild-register/dist/node')
18+
19+
const { unregister } = register({
20+
target: 'node18',
21+
format: 'cjs',
22+
extensions: ['.ts'],
23+
})
24+
25+
require(${JSON.stringify(filepath)})
26+
`,
27+
{eval: true, workerData},
28+
)
29+
30+
return new Promise<{success: boolean; error?: string}>((resolve, reject) => {
31+
worker.on('message', resolve)
32+
worker.on('error', reject)
33+
worker.on('exit', (code) => {
34+
if (code !== 0) {
35+
reject(new Error(`Worker stopped with exit code ${code}`))
36+
}
37+
})
38+
})
39+
}
40+
41+
describe('mockBrowserEnvironment', () => {
42+
test('should handle themer.sanity.build imports in worker thread', async () => {
43+
await expect(getImportWorker('https://themer.sanity.build/api/hues')).resolves.toEqual({
44+
success: true,
45+
})
46+
})
47+
48+
test('should still error on non-themer.sanity.build imports in worker thread', async () => {
49+
await expect(getImportWorker('https://foobar.official/package')).resolves.toEqual({
50+
success: false,
51+
error: expect.stringContaining("Cannot find module 'https://foobar.official/package'"),
52+
})
53+
})
54+
})
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {isMainThread, parentPort, workerData as _workerData} from 'node:worker_threads'
2+
3+
import {mockBrowserEnvironment} from '../mockBrowserEnvironment'
4+
5+
export type ThemerImportWorkerData = {
6+
workDir: string
7+
importName: string
8+
}
9+
10+
const {workDir, importName} = _workerData satisfies ThemerImportWorkerData
11+
12+
async function main() {
13+
if (isMainThread || !parentPort) {
14+
throw new Error('This module must be run as a worker thread')
15+
}
16+
17+
const cleanup = mockBrowserEnvironment(workDir)
18+
19+
try {
20+
// eslint-disable-next-line import/no-dynamic-require
21+
require(importName)
22+
23+
// If we get here, the import was handled successfully
24+
parentPort?.postMessage({success: true})
25+
} catch (error) {
26+
// If we catch an error, the import error handler didn't work
27+
parentPort?.postMessage({
28+
success: false,
29+
error: error instanceof Error ? error.message : String(error),
30+
})
31+
} finally {
32+
cleanup()
33+
}
34+
}
35+
36+
void main().then(() => process.exit())

0 commit comments

Comments
 (0)