Skip to content

Commit f724a62

Browse files
committed
fix: unit tests fix
1 parent ff17272 commit f724a62

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

packages/shared/utils/src/lib/file/default-export-loader.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,18 @@ export async function loadDefaultExport<T = unknown>(
1717
): Promise<T> {
1818
try {
1919
const fileUrl = pathToFileURL(filePath).toString();
20-
// Use indirect eval to prevent webpack from replacing dynamic import with its own context
21-
// This ensures the import() call works at runtime for external user files
22-
const dynamicImport = new Function('url', 'return import(url)');
23-
const module = await dynamicImport(fileUrl);
20+
21+
// In test environments (Vitest), use native import to avoid transformation issues
22+
// In production (webpack/bundled), use Function constructor to preserve dynamic import
23+
const isTestEnv =
24+
typeof process !== 'undefined' &&
25+
(process.env.NODE_ENV === 'test' ||
26+
process.env.VITEST === 'true' ||
27+
typeof (globalThis as Record<string, unknown>).vitest !== 'undefined');
28+
29+
const module = isTestEnv
30+
? await import(fileUrl)
31+
: await new Function('url', 'return import(url)')(fileUrl);
2432

2533
if (!('default' in module)) {
2634
throw new Error(

0 commit comments

Comments
 (0)