diff --git a/docs/CodeTransformation.md b/docs/CodeTransformation.md index 2d16208eb10f..3be3e0ac9a0e 100644 --- a/docs/CodeTransformation.md +++ b/docs/CodeTransformation.md @@ -40,7 +40,7 @@ interface TransformOptions { supportsTopLevelAwait: boolean; instrument: boolean; /** Cached file system which is used by `jest-runtime` to improve performance. */ - cacheFS: Map; + cacheFS: Map; /** Jest configuration of currently running project. */ config: ProjectConfig; /** Stringified version of the `config` - useful in cache busting. */ diff --git a/packages/babel-jest/src/__tests__/index.ts b/packages/babel-jest/src/__tests__/index.ts index 9903c2b773b0..4808bd0e591a 100644 --- a/packages/babel-jest/src/__tests__/index.ts +++ b/packages/babel-jest/src/__tests__/index.ts @@ -52,7 +52,7 @@ test('Returns source string with inline maps when no transformOptions is passed' sourceString, 'dummy_path.js', { - cacheFS: new Map(), + cacheFS: new Map(), config: makeProjectConfig(), configString: JSON.stringify(makeProjectConfig()), instrument: false, @@ -76,7 +76,7 @@ test('Returns source string with inline maps when no transformOptions is passed sourceString, 'dummy_path.js', { - cacheFS: new Map(), + cacheFS: new Map(), config: makeProjectConfig(), configString: JSON.stringify(makeProjectConfig()), instrument: false, @@ -141,7 +141,7 @@ describe('caller option correctly merges from defaults and options', () => { ], ])('%j -> %j', (input, output) => { defaultBabelJestTransformer.process(sourceString, 'dummy_path.js', { - cacheFS: new Map(), + cacheFS: new Map(), config: makeProjectConfig(), configString: JSON.stringify(makeProjectConfig()), instrument: false, @@ -166,7 +166,7 @@ describe('caller option correctly merges from defaults and options', () => { test('can pass null to createTransformer', () => { const transformer = createTransformer(); transformer.process(sourceString, 'dummy_path.js', { - cacheFS: new Map(), + cacheFS: new Map(), config: makeProjectConfig(), configString: JSON.stringify(makeProjectConfig()), instrument: false, diff --git a/packages/jest-repl/src/cli/repl.ts b/packages/jest-repl/src/cli/repl.ts index 3a486c6bfbb7..aceb9f1fb180 100644 --- a/packages/jest-repl/src/cli/repl.ts +++ b/packages/jest-repl/src/cli/repl.ts @@ -33,7 +33,7 @@ const evalCommand: repl.REPLEval = ( cmd, jestGlobalConfig.replname ?? 'jest.js', { - cacheFS: new Map(), + cacheFS: new Map(), config: jestProjectConfig, configString: JSON.stringify(jestProjectConfig), instrument: false, diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index ed0d5f9e50c2..5941ec3076db 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -153,7 +153,7 @@ const supportsNodeColonModulePrefixInRequire = (() => { })(); export default class Runtime { - private readonly _cacheFS: Map; + private readonly _cacheFS: Map; private readonly _config: Config.ProjectConfig; private readonly _globalConfig?: Config.GlobalConfig; private readonly _coverageOptions: ShouldInstrumentOptions; @@ -211,7 +211,7 @@ export default class Runtime { environment: JestEnvironment, resolver: Resolver, transformer: ScriptTransformer, - cacheFS: Map, + cacheFS: Map, coverageOptions: ShouldInstrumentOptions, testPath: string, // TODO: make mandatory in Jest 30 @@ -446,7 +446,7 @@ export default class Runtime { if (modulePath.endsWith('.wasm')) { const wasm = this._importWasmModule( - fs.readFileSync(modulePath), + this.readFileBuffer(modulePath), modulePath, context, ); @@ -2376,6 +2376,23 @@ export default class Runtime { }; } + private readFileBuffer(filename: string): Buffer { + let source = this._cacheFS.get(filename); + + if (!source) { + source = fs.readFileSync(filename); + + this._cacheFS.set(filename, source); + } + + if (typeof source === 'string') { + // This may occur if readFileBuffer and readFile are called interchangeably for one file + throw new Error("Expected source to be a 'Buffer', but got 'string'"); + } + + return source; + } + private readFile(filename: string): string { let source = this._cacheFS.get(filename); @@ -2385,6 +2402,11 @@ export default class Runtime { this._cacheFS.set(filename, source); } + if (typeof source !== 'string') { + // This may occur if readFileBuffer and readFile are called interchangeably for one file + throw new Error("Expected source to be a 'string', but got 'Buffer'"); + } + return source; }