import path from 'path'; import fs from 'graceful-fs'; import vm from 'vm'; import { URL, pathToFileURL, fileURLToPath } from 'url'; import { createRequire } from 'module'; import { rimrafSync } from 'rimraf'; import { describe, it, beforeAll, afterAll, beforeEach, afterEach, expect, rs, } from '@rstest/core'; // Create a require function using __filename (available in CommonJS output mode) const nativeRequire = createRequire(__filename); // FEDERATION_WEBPACK_PATH selects one webpack for the harness and the plugins // under test, so a legacy webpack run does not mix two copies. const { normalizeWebpackPath } = nativeRequire( '@module-federation/sdk/normalize-webpack-path', ); const webpackEntry = normalizeWebpackPath('webpack'); const { supportsWebpackFeature } = nativeRequire('./helpers/webpackFeatures'); // 预热 webpack(与原逻辑一致) nativeRequire('./helpers/warmup-webpack'); const checkArrayExpectation = nativeRequire('./checkArrayExpectation'); const FakeDocument = nativeRequire('./helpers/FakeDocument'); const CurrentScript = nativeRequire('./helpers/CurrentScript'); const prepareOptions = nativeRequire('./helpers/prepareOptions'); const captureStdio = nativeRequire('./helpers/captureStdio'); const asModule = nativeRequire('./helpers/asModule'); const filterInfraStructureErrors = nativeRequire( './helpers/infrastructureLogErrors', ); const { parseResource } = nativeRequire( normalizeWebpackPath('webpack/lib/util/identifier'), ); const casesPath = path.join(__dirname, 'configCases'); const ensureTreeShakingFixtures = (testDirectory: string) => { const nodeModulesDir = path.join(testDirectory, 'node_modules'); const isReshake = path.basename(testDirectory) === 'reshake-share'; fs.mkdirSync(nodeModulesDir, { recursive: true }); const ensurePackage = ( pkgName: string, entryContents: string, sideEffects: boolean = false, ) => { const pkgDir = path.join(nodeModulesDir, pkgName); fs.mkdirSync(pkgDir, { recursive: true }); fs.writeFileSync( path.join(pkgDir, 'package.json'), `${JSON.stringify( { name: pkgName, main: './index.js', version: '1.0.0', sideEffects: sideEffects, }, null, 2, )}\n`, ); fs.writeFileSync(path.join(pkgDir, 'index.js'), entryContents); }; if (isReshake) { ensurePackage( 'ui-lib-dep', [ "export const Message = 'Message';", "export const Spin = 'Spin';", '', ].join('\n'), ); ensurePackage( 'ui-lib', [ "import { Message, Spin } from 'ui-lib-dep';", '', "export const Button = 'Button';", "export const List = 'List';", "export const Badge = 'Badge';", '', 'export const MessagePro = `${Message}Pro`;', 'export const SpinPro = `${Spin}Pro`;', '', 'export default {', ' Button,', ' List,', ' Badge,', '};', '', ].join('\n'), ); } else { ensurePackage( 'ui-lib', [ "export const Button = 'Button';", "export const List = 'List';", "export const Badge = 'Badge';", '', 'export default {', ' Button,', ' List,', ' Badge,', '};', '', ].join('\n'), ); ensurePackage( 'ui-lib-es', [ "export const Button = 'Button';", "export const List = 'List'", "export const Badge = 'Badge'", '', ].join('\n'), ); ensurePackage( 'ui-lib-dynamic-specific-export', [ "export const Button = 'Button';", "export const List = 'List'", "export const Badge = 'Badge'", '', ].join('\n'), ); ensurePackage( 'ui-lib-dynamic-default-export', [ "export const Button = 'Button';", "export const List = 'List'", "export const Badge = 'Badge'", '', 'export default {', '\tButton,', '\tList,', '\tBadge', '}', '', ].join('\n'), ); ensurePackage( 'ui-lib-side-effect', [ "export const Button = 'Button';", "export const List = 'List'", "export const Badge = 'Badge'", '', 'globalThis.Button = Button;', 'globalThis.List = List;', 'globalThis.Badge = Badge;', 'export default {', '\tButton,', '\tList,', '\tBadge', '}', '', ].join('\n'), true, ); } }; const collectTreeShakingMissingModuleStubs = (outputDirectory: string) => { const independentPackagesDir = path.join( outputDirectory, 'independent-packages', ); if (!fs.existsSync(independentPackagesDir)) { return [] as string[]; } const missing = new Set(); const pending = [independentPackagesDir]; const pattern = /Cannot find module ['"]([^'"]*tree-shaking-share[^'"]*node_modules[^'"]*)['"]/g; while (pending.length) { const currentDir = pending.pop() as string; let entries: fs.Dirent[] = []; try { entries = fs.readdirSync(currentDir, { withFileTypes: true }); } catch { continue; } for (const entry of entries) { const fullPath = path.join(currentDir, entry.name); if (entry.isDirectory()) { pending.push(fullPath); continue; } if (!entry.isFile() || entry.name !== 'share-entry.js') { continue; } let content = ''; try { content = fs.readFileSync(fullPath, 'utf-8'); } catch { continue; } pattern.lastIndex = 0; let match: RegExpExecArray | null = null; while ((match = pattern.exec(content))) { missing.add(match[1]); } } } return Array.from(missing).sort(); }; const dedupeByMessage = (items: any[]) => { if (!Array.isArray(items) || items.length === 0) { return [] as any[]; } const seen = new Set(); const deduped: any[] = []; for (const item of items) { const key = (item && item.message) as string | undefined; if (key) { if (seen.has(key)) continue; seen.add(key); } deduped.push(item); } return deduped; }; const collectInfrastructureOutputs = ( infraLogs: string[], stderrOutput: string | undefined, config: any, ) => { const infrastructureCollection = filterInfraStructureErrors.collect( infraLogs, config, ); const stderrLines = stderrOutput ? stderrOutput .split(/\r?\n/) .map((line: string) => line.trim()) .filter(Boolean) : []; const stderrCollection = filterInfraStructureErrors.collect( stderrLines, config, ); const unhandled = [ ...infrastructureCollection.entries .filter( (entry: any) => !infrastructureCollection.handled.has(entry.normalized), ) .map((entry: any) => entry.normalized), ...stderrCollection.entries .filter((entry: any) => !stderrCollection.handled.has(entry.normalized)) .map((entry: any) => entry.normalized), ]; const combinedResults = dedupeByMessage([ ...infrastructureCollection.results, ...stderrCollection.results, ]); return { unhandled, results: combinedResults }; }; const categories = fs.readdirSync(casesPath).map((cat: string) => { return { name: cat, tests: fs .readdirSync(path.join(casesPath, cat)) .filter( (folder: string) => !folder.startsWith('_') && !folder.startsWith('.'), ) .sort(), }; }); const createLogger = (appendTarget: string[]) => { return { log: (l: string) => appendTarget.push(l), debug: (l: string) => appendTarget.push(l), trace: (l: string) => appendTarget.push(l), info: (l: string) => appendTarget.push(l), warn: console.warn.bind(console), error: console.error.bind(console), logTime: () => {}, group: () => {}, groupCollapsed: () => {}, groupEnd: () => {}, profile: () => {}, profileEnd: () => {}, clear: () => {}, status: () => {}, }; }; export const describeCases = (config: any) => { const includeCategories: string[] | undefined = Array.isArray( config?.includeCategories, ) ? config.includeCategories : undefined; const excludeCategories: string[] | undefined = Array.isArray( config?.excludeCategories, ) ? config.excludeCategories : undefined; const selectedCategories = categories.filter((category) => { if (includeCategories?.length && !includeCategories.includes(category.name)) return false; if (excludeCategories?.length && excludeCategories.includes(category.name)) return false; return true; }); describe(config.name, () => { let stderr: any; rs.setConfig({ testTimeout: 20000 }); beforeEach(() => { stderr = captureStdio(process.stderr, true); }); afterEach(() => { stderr.restore(); }); for (const category of selectedCategories) { describe(category.name, () => { for (const testName of category.tests) { if (config.includeTests && !config.includeTests.includes(testName)) continue; describe(testName, () => { const testDirectory = path.join(casesPath, category.name, testName); const filterPath = path.join(testDirectory, 'test.filter.js'); if (fs.existsSync(filterPath) && !require(filterPath)()) { it('filtered', () => undefined); return; } const infraStructureLog: string[] = []; const outBaseDir = path.join(__dirname, 'js'); const testSubPath = path.join(config.name, category.name, testName); const outputDirectory = path.join(outBaseDir, testSubPath); const cacheDirectory = path.join(outBaseDir, '.cache', testSubPath); let options: any, optionsArr: any[], testConfig: any; const ensureTreeShakingFixturesIfNeeded = () => { if ( testDirectory.includes( `${path.sep}tree-shaking-share${path.sep}`, ) ) { ensureTreeShakingFixtures(testDirectory); } }; const withLegacyTreeShakingBuildVersion = async ( run: () => Promise, ): Promise => { if ( !testDirectory.includes( `${path.sep}tree-shaking-share${path.sep}`, ) ) { return run(); } const previousBuildVersion = process.env.MF_BUILD_VERSION; process.env.MF_BUILD_VERSION = '0.0.0'; try { return await run(); } finally { if (previousBuildVersion === undefined) { delete process.env.MF_BUILD_VERSION; } else { process.env.MF_BUILD_VERSION = previousBuildVersion; } } }; beforeAll(() => { if ( testDirectory.includes( `${path.sep}tree-shaking-share${path.sep}`, ) ) { nativeRequire('./scripts/ensure-reshake-fixtures'); ensureTreeShakingFixturesIfNeeded(); } options = prepareOptions( require(path.join(testDirectory, 'webpack.config.js')), { testPath: outputDirectory, }, ); optionsArr = [].concat(options); optionsArr.forEach((opt: any, idx: number) => { // federation 选项注入 if (config.federation) { const mfp = opt.plugins && opt.plugins.find( (p: any) => p.name === 'ModuleFederationPlugin', ); if (mfp) { if (!mfp._options.experiments) mfp._options.experiments = {}; if (config.federation?.asyncStartup) { if ('asyncStartup' in mfp._options.experiments) { // respect explicit setting } else { Object.assign( mfp._options.experiments, config.federation, ); } } } } if (!opt.context) opt.context = testDirectory; if (!opt.mode) opt.mode = 'production'; if (!opt.optimization) opt.optimization = {}; if (opt.optimization.minimize === undefined) opt.optimization.minimize = false; if (opt.optimization.minimizer === undefined) { // 保留注释:不强制设置 minimizer } if (!opt.entry) opt.entry = './index.js'; if (!opt.target) opt.target = 'async-node'; if (!opt.output) opt.output = {}; if (!opt.output.path) opt.output.path = outputDirectory; if (typeof opt.output.pathinfo === 'undefined') opt.output.pathinfo = true; if (!opt.output.filename) opt.output.filename = 'bundle' + idx + (opt.experiments && opt.experiments.outputModule ? '.mjs' : '.js'); if (config.cache) { opt.cache = { cacheDirectory, name: `config-${idx}`, ...config.cache, }; opt.infrastructureLogging = { debug: true, ...opt.infrastructureLogging, console: createLogger(infraStructureLog), }; } if (!opt.snapshot) opt.snapshot = {}; if (!opt.snapshot.managedPaths) { opt.snapshot.managedPaths = [ path.resolve(__dirname, '../node_modules'), ]; } }); testConfig = { findBundle: function ( i: number, options: any, ): string | undefined { const ext = path.extname( parseResource(options.output.filename).path, ); if ( fs.existsSync( path.join(options.output.path, 'bundle' + i + ext), ) ) { return './bundle' + i + ext; } return undefined; }, timeout: 30000, }; try { testConfig = Object.assign( testConfig, require(path.join(testDirectory, 'test.config.js')), ); } catch (e) { // ignored } }); afterAll(() => { options = undefined as any; optionsArr = undefined as any; testConfig = undefined as any; }); beforeAll(() => { rimrafSync(cacheDirectory); }); const handleFatalError = (err: any) => { const fakeStats = { errors: [ { message: err.message, stack: err.stack, }, ], } as any; // error 期望匹配 if ( checkArrayExpectation( testDirectory, fakeStats, 'error', 'Error', (e: any) => { throw e; }, ) ) { return; // 让 rstest 认为测试通过(错误与期望一致) } // 否则抛出,交给 rstest 失败 throw err; }; // cache 预编译:第一次 if (config.cache) { it(`${testName} should pre-compile to fill disk cache (1st)`, async () => { ensureTreeShakingFixturesIfNeeded(); rimrafSync(outputDirectory); fs.mkdirSync(outputDirectory, { recursive: true }); infraStructureLog.length = 0; await new Promise((resolve, reject) => { try { // 单次 run withLegacyTreeShakingBuildVersion( () => new Promise((innerResolve, innerReject) => { nativeRequire(webpackEntry)(options, (err: any) => { const stderrOutput = stderr.toString(); const { unhandled, results: infrastructureLogErrors, } = collectInfrastructureOutputs( infraStructureLog, stderrOutput, { run: 1, options }, ); stderr.reset(); if (unhandled.length) { innerReject( new Error( 'Errors/Warnings during build:\n' + unhandled.join('\n'), ), ); return; } if ( infrastructureLogErrors.length && checkArrayExpectation( testDirectory, { infrastructureLogs: infrastructureLogErrors }, 'infrastructureLog', 'infrastructure-log', 'InfrastructureLog', (e: any) => { throw e; }, ) ) { innerResolve(); return; } if (err) { innerReject(err); return; } innerResolve(); }); }), ) .then(resolve) .catch(reject); } catch (e: any) { reject(e); } }); }, 60000); // cache 预编译:第二次 it(`${testName} should pre-compile to fill disk cache (2nd)`, async () => { ensureTreeShakingFixturesIfNeeded(); rimrafSync(outputDirectory); fs.mkdirSync(outputDirectory, { recursive: true }); infraStructureLog.length = 0; await new Promise((resolve, reject) => { try { withLegacyTreeShakingBuildVersion( () => new Promise((innerResolve, innerReject) => { nativeRequire(webpackEntry)( options, (err: any, stats: any) => { if (err) { innerReject(err); return; } const { modules, children, errorsCount } = stats.toJson({ all: false, modules: true, errorsCount: true, }); const stderrOutput = stderr.toString(); const { unhandled, results: infrastructureLogErrors, } = collectInfrastructureOutputs( infraStructureLog, stderrOutput, { run: 2, options }, ); stderr.reset(); if (errorsCount === 0) { if (unhandled.length) { innerReject( new Error( 'Errors/Warnings during build:\n' + unhandled.join('\n'), ), ); return; } const allModules = children ? children.reduce( (all: any[], { modules }: any) => all.concat(modules), modules || [], ) : modules; if ( allModules.some( (m: any) => m.type !== 'cached modules' && !m.cached, ) ) { innerReject( new Error( `Some modules were not cached:\n${stats.toString({ all: false, modules: true, modulesSpace: 100 })}`, ), ); return; } } if ( infrastructureLogErrors.length && checkArrayExpectation( testDirectory, { infrastructureLogs: infrastructureLogErrors, }, 'infrastructureLog', 'infrastructure-log', 'InfrastructureLog', (e: any) => { throw e; }, ) ) { innerResolve(); return; } innerResolve(); }, ); }), ) .then(resolve) .catch(reject); } catch (e: any) { reject(e); } }); }, 40000); } it( `${testName} should compile`, async () => { ensureTreeShakingFixturesIfNeeded(); const isTreeShakingFixtureCase = testDirectory.includes( `${path.sep}tree-shaking-share${path.sep}`, ); const cleanOutputDirectory = () => { try { // Robust cleanup to avoid ENOTEMPTY and race conditions (fs as any).rmSync?.(outputDirectory, { recursive: true, force: true, }); } catch { try { rimrafSync(outputDirectory); } catch { /* ignore */ } } }; const runWebpackCompile = async () => { infraStructureLog.length = 0; stderr.reset(); return withLegacyTreeShakingBuildVersion( () => new Promise<{ stats: any }>((resolve, reject) => { const onCompiled = (err: any, stats: any) => { if (err) return reject(err); resolve({ stats }); }; try { if (config.cache) { const compiler = nativeRequire(webpackEntry)(options); compiler.run((err: any) => { if (err) return reject(err); compiler.run((error: any, stats: any) => { compiler.close((cerr: any) => { if (cerr) return reject(cerr); if (error) return reject(error); resolve({ stats }); }); }); }); } else { nativeRequire(webpackEntry)(options, onCompiled); } } catch (e: any) { reject(e); } }), ); }; cleanOutputDirectory(); fs.mkdirSync(outputDirectory, { recursive: true }); let { stats } = await runWebpackCompile().catch((e) => { handleFatalError(e); throw e; // rethrow for rstest to mark failure otherwise }); // Under heavy IO/CPU contention, tree-shaking fixture packages can // transiently resolve as missing. Rebuild with bounded retries // after re-ensuring fixtures when share-entry stubs contain // webpackMissingModule. if (isTreeShakingFixtureCase) { const maxTreeShakingAttempts = 4; for ( let attempt = 1; attempt < maxTreeShakingAttempts; attempt++ ) { const missingModules = collectTreeShakingMissingModuleStubs(outputDirectory); if (!missingModules.length) { break; } ensureTreeShakingFixturesIfNeeded(); // Give the FS a tiny settle window under extreme IO pressure. await new Promise((resolve) => setTimeout(resolve, 25 * attempt), ); cleanOutputDirectory(); fs.mkdirSync(outputDirectory, { recursive: true }); ({ stats } = await runWebpackCompile().catch((e) => { handleFatalError(e); throw e; })); if (attempt === maxTreeShakingAttempts - 1) { const remainingMissingModules = collectTreeShakingMissingModuleStubs(outputDirectory); if (remainingMissingModules.length) { throw new Error( [ `Tree-shaking fixture modules remained unresolved after ${maxTreeShakingAttempts} compilation attempts:`, ...remainingMissingModules, ].join('\n'), ); } } } } // 写入 stats const statOptions = { preset: 'verbose', colors: false }; fs.mkdirSync(outputDirectory, { recursive: true }); fs.writeFileSync( path.join(outputDirectory, 'stats.txt'), stats.toString(statOptions), 'utf-8', ); const jsonStats = stats.toJson({ errorDetails: true }); try { fs.writeFileSync( path.join(outputDirectory, 'stats.json'), JSON.stringify(jsonStats, null, 2), 'utf-8', ); } catch {} // 错误/警告/infra/deprecation 检查 if ( checkArrayExpectation( testDirectory, jsonStats, 'error', 'Error', (e: any) => { throw e; }, ) ) { return; } if ( checkArrayExpectation( testDirectory, jsonStats, 'warning', 'Warning', (e: any) => { throw e; }, ) ) { return; } const stderrOutput = stderr.toString(); const { unhandled, results: infrastructureLogErrors } = collectInfrastructureOutputs( infraStructureLog, stderrOutput, { run: 3, options }, ); stderr.reset(); // Focused runs can route this log to Rstest instead of webpack's infrastructure logger. if ( unhandled.length && fs.existsSync( path.join(testDirectory, 'infrastructure-log.js'), ) ) { checkArrayExpectation( testDirectory, { infrastructureLogs: unhandled.map((message) => ({ message, })), }, 'infrastructureLog', 'infrastructure-log', 'InfrastructureLog', (e: any) => { throw e; }, ); } else if (unhandled.length) { throw new Error( 'Errors/Warnings during build:\n' + unhandled.join('\n'), ); } if ( checkArrayExpectation( testDirectory, { deprecations: [] }, 'deprecation', 'Deprecation', (e: any) => { throw e; }, ) ) { return; } if ( infrastructureLogErrors.length && checkArrayExpectation( testDirectory, { infrastructureLogs: infrastructureLogErrors }, 'infrastructureLog', 'infrastructure-log', 'InfrastructureLog', (e: any) => { throw e; }, ) ) { return; } // 执行 bundle 并收集导出的 it/beforeEach/afterEach(行为对齐 Jest harness) let filesCount = 0; if (testConfig.noTests) return; // 某些 case 明确无测试 if (testConfig.beforeExecute) testConfig.beforeExecute(); type Hook = () => any; type CallbackTestFn = (done: (err?: any) => void) => void; type PromiseTestFn = () => any | Promise; type TestFn = CallbackTestFn | PromiseTestFn; type TestItem = { name: string; fn: TestFn; timeout?: number }; const hooksBefore: Hook[] = []; const hooksAfter: Hook[] = []; const collectedTests: TestItem[] = []; let runTests = -1; let numberOfTests = 0; let globalTimeout = 10000; const disposables: Array<() => void> = []; const createDisposableFn = (fn: any, isTest?: boolean) => { if (!fn) return null; let rfn: any; if (fn.length >= 1) { rfn = (done: (err?: any) => void) => { fn((...args: any[]) => { if (isTest) runTests++; done(...args); }); }; } else { rfn = () => { const r = fn(); if (isTest) runTests++; return r; }; } disposables.push(() => { fn = null; }); return rfn; }; const caseIt = (name: string, fn: TestFn, timeout?: number) => { numberOfTests++; if (runTests >= numberOfTests) throw new Error('it called too late'); const wrapped = createDisposableFn(fn, true) as TestFn; collectedTests.push({ name, fn: wrapped, timeout: timeout || globalTimeout, }); }; const caseBeforeEach = (fn: Hook) => { if (runTests >= numberOfTests) throw new Error('beforeEach called too late'); hooksBefore.push(createDisposableFn(fn, false) as Hook); }; const caseAfterEach = (fn: Hook) => { if (runTests >= numberOfTests) throw new Error('afterEach called too late'); hooksAfter.push(createDisposableFn(fn, false) as Hook); }; const setDefaultTimeout = (time: number) => { globalTimeout = time; }; const getNumberOfTests = () => numberOfTests; const isCallbackStyle = (fn: TestFn): fn is CallbackTestFn => fn.length >= 1; const runMaybeDone = async (fn: TestFn, timeout?: number) => { if (!fn) return; const run = async () => { if (isCallbackStyle(fn)) { await new Promise((resolve, reject) => { try { fn((err?: any) => (err ? reject(err) : resolve())); } catch (e) { reject(e); } }); } else { const r = (fn as PromiseTestFn)(); if (r && typeof r.then === 'function') await r; } }; if (!timeout) { await run(); return; } await Promise.race([ run(), new Promise((_, reject) => { setTimeout(() => { reject(new Error('Test timed out')); }, timeout); }), ]); }; const results: any[] = []; for (let i = 0; i < optionsArr.length; i++) { const opt = optionsArr[i]; const bundlePath = testConfig.findBundle(i, optionsArr[i]); if (bundlePath) { filesCount++; const document = new FakeDocument(outputDirectory); const globalContext = { console: console, expect: expect, setTimeout: setTimeout, clearTimeout: clearTimeout, document, getComputedStyle: document.getComputedStyle.bind(document), location: { href: 'https://test.cases/path/index.html', origin: 'https://test.cases', toString() { return 'https://test.cases/path/index.html'; }, }, } as any; const requireCache: Record = Object.create(null); const esmCache = new Map(); const esmIdentifier = `${category.name}-${testName}-${i}`; const baseModuleScope: any = { console: console, it: caseIt, beforeEach: caseBeforeEach, afterEach: caseAfterEach, expect, jest: { resetModules: () => rs.resetModules(), setTimeout: (...args: any[]) => (setTimeout as any)(...args), }, __STATS__: jsonStats, supportsWebpackFeature, nsObj: (m: any) => { Object.defineProperty(m, Symbol.toStringTag, { value: 'Module', }); return m; }, }; let runInNewContext = false; if (opt.target === 'web' || opt.target === 'webworker') { baseModuleScope.window = globalContext; baseModuleScope.self = globalContext; baseModuleScope.document = globalContext.document; baseModuleScope.setTimeout = globalContext.setTimeout; baseModuleScope.clearTimeout = globalContext.clearTimeout; baseModuleScope.URL = URL; baseModuleScope.Worker = nativeRequire( './helpers/createFakeWorker', )({ outputDirectory, }); runInNewContext = true; } if (testConfig.moduleScope) { testConfig.moduleScope(baseModuleScope); } const esmContext = vm.createContext(baseModuleScope, { name: 'context for esm', }); const executeBundle = (entryRel: string) => { const _require = ( currentDirectory: string, opt: any, module: any, esmMode?: 'evaluated' | 'unlinked', parentModule?: any, ): any => { if (testConfig === undefined) { throw new Error( `_require(${module}) called after all tests from ${category.name} ${testName} have completed`, ); } if (Array.isArray(module) || /^\.\.?\//.test(module)) { let content = ''; let p = ''; let subPath = ''; if (Array.isArray(module)) { p = path.join( currentDirectory, '.array-require.js', ); content = `module.exports = (${module .map( (arg: any) => `require(${JSON.stringify(`./${arg}`)})`, ) .join(', ')});`; } else { const resolvedPath = path.resolve( currentDirectory, module, ); const outputRoot = path.resolve(outputDirectory); const sharedRoot = path.resolve(outBaseDir); if ( resolvedPath !== outputRoot && !resolvedPath.startsWith(outputRoot + path.sep) && resolvedPath !== sharedRoot && !resolvedPath.startsWith(sharedRoot + path.sep) ) { throw new Error( `Refusing to load module outside output directory: ${module}`, ); } p = resolvedPath; content = fs.readFileSync(p, 'utf-8'); const lastSlash = module.lastIndexOf('/'); let firstSlash = module.indexOf('/'); if (lastSlash !== -1 && firstSlash !== lastSlash) { if (firstSlash !== -1) { let next = module.indexOf('/', firstSlash + 1); let dir = module.slice(firstSlash + 1, next); while (dir === '.') { firstSlash = next; next = module.indexOf('/', firstSlash + 1); dir = module.slice(firstSlash + 1, next); } } subPath = module.slice( firstSlash + 1, lastSlash + 1, ); } } // Follow original template: ESM only when .mjs and outputModule enabled const isModule = p.endsWith('.mjs') && opt.experiments && opt.experiments.outputModule; if (isModule) { if (!vm.SourceTextModule) throw new Error( "Running this test requires '--experimental-vm-modules'.\nRun with 'node --experimental-vm-modules node_modules/jest-cli/bin/jest'.", ); let esm = esmCache.get(p); if (!esm) { esm = new vm.SourceTextModule(content, { identifier: esmIdentifier + '-' + p, url: pathToFileURL(p).href + '?' + esmIdentifier, context: esmContext, initializeImportMeta: (meta: any) => { meta.url = pathToFileURL(p).href; }, importModuleDynamically: async ( specifier: string, mod: any, ) => { const result = await _require( path.dirname(p), opt, specifier, 'evaluated', mod, ); return await asModule(result, mod.context); }, } as any); esmCache.set(p, esm); } if (esmMode === 'unlinked') return esm; return (async () => { if (esm.status === 'evaluated') { const ns = (esm as any).namespace; return ns.default && ns.default instanceof Promise ? ns.default : ns; } if ( esm.status !== 'unlinked' && esm.status !== 'linking' ) { await esm.evaluate(); const ns = (esm as any).namespace; return ns.default && ns.default instanceof Promise ? ns.default : ns; } await esm.link( async ( specifier: string, referencingModule: any, ) => { return await asModule( await _require( path.dirname( referencingModule.identifier ? referencingModule.identifier.slice( esmIdentifier.length + 1, ) : fileURLToPath( referencingModule.url, ), ), opt, specifier, 'unlinked', referencingModule, ), referencingModule.context, true, ); }, ); // Do not call instantiate(): Node's link() already performs // instantiation. Calling instantiate() on a linked module throws // "Module status must be unlinked" in Node 20+. await esm.evaluate(); if (esmMode === 'evaluated') return esm as any; const ns = (esm as any).namespace; return ns.default && ns.default instanceof Promise ? ns.default : ns; })(); } else { if (p in requireCache) { return requireCache[p].exports; } const m = { exports: {} } as any; requireCache[p] = m; const moduleScope: any = { ...baseModuleScope, require: _require.bind( null, path.dirname(p), opt, ), importScripts: (url: string) => { expect(url).toMatch( /^https:\/\/test\.cases\/path\//, ); _require( outputDirectory, opt, `.${url.slice('https://test.cases/path'.length)}`, ); }, module: m, exports: m.exports, __dirname: path.dirname(p), __filename: p, _globalAssign: { expect }, }; if (testConfig.moduleScope) testConfig.moduleScope(moduleScope); if (!runInNewContext) content = `Object.assign(global, _globalAssign); ${content}`; const args = Object.keys(moduleScope); const argValues = args.map( (arg) => moduleScope[arg], ); // codeql [js/code-injection] Test harness executes local bundle output. const code = `(function(${args.join( ', ', )}) {${content}\n})`; const oldCurrentScript = document.currentScript; (document as any).currentScript = new CurrentScript( subPath, ); const fn = runInNewContext ? vm.runInNewContext( code, globalContext as any, p, ) : vm.runInThisContext(code, p); fn.call( testConfig.nonEsmThis ? testConfig.nonEsmThis(module) : m.exports, ...argValues, ); (document as any).currentScript = oldCurrentScript; return m.exports; } } else if ( testConfig.modules && module in testConfig.modules ) { return testConfig.modules[module]; } else { return nativeRequire( module.startsWith('node:') ? module.slice(5) : module, ); } }; const target = entryRel.startsWith('./') ? entryRel : './' + entryRel; results.push(_require(outputDirectory, opt, target)); }; if (Array.isArray(bundlePath)) { for (const bundlePathItem of bundlePath) { executeBundle(bundlePathItem); } } else { executeBundle(bundlePath); } } } if ( !jsonStats.errors.length && filesCount !== optionsArr.length ) { throw new Error( 'Should have found at least one bundle file per webpack config', ); } // Wait for async bundles to complete (important for asyncStartup mode) await Promise.all(results); // Run afterExecute before checking collected tests. // Some cases (e.g. SystemJS) rely on afterExecute to trigger // deferred module evaluation which registers it() calls. try { if (testConfig.afterExecute) testConfig.afterExecute(); } catch {} // simulate the placeholder test used in createLazyTestEnv runTests = 0; if (collectedTests.length > 0) { for (const t of collectedTests) { for (const b of hooksBefore) { await runMaybeDone(b, t.timeout); } await runMaybeDone(t.fn, t.timeout); for (const a of hooksAfter) { await runMaybeDone(a, t.timeout); } } } for (const key of Object.keys(global)) { if (key.includes('webpack')) delete (global as any)[key]; } if (getNumberOfTests() < filesCount) { throw new Error('No tests exported by test case'); } for (const dispose of disposables) { dispose(); } }, testConfig?.timeout || 30000, ); }); } }); } }); };