Skip to content

Commit

Permalink
Implement readFileBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
kachkaev committed Oct 30, 2022
1 parent 79e0f40 commit 0d4945e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/CodeTransformation.md
Expand Up @@ -40,7 +40,7 @@ interface TransformOptions<TransformerConfig = unknown> {
supportsTopLevelAwait: boolean;
instrument: boolean;
/** Cached file system which is used by `jest-runtime` to improve performance. */
cacheFS: Map<string, string>;
cacheFS: Map<string, Buffer | string>;
/** Jest configuration of currently running project. */
config: ProjectConfig;
/** Stringified version of the `config` - useful in cache busting. */
Expand Down
8 changes: 4 additions & 4 deletions packages/babel-jest/src/__tests__/index.ts
Expand Up @@ -52,7 +52,7 @@ test('Returns source string with inline maps when no transformOptions is passed'
sourceString,
'dummy_path.js',
{
cacheFS: new Map<string, string>(),
cacheFS: new Map<string, Buffer | string>(),
config: makeProjectConfig(),
configString: JSON.stringify(makeProjectConfig()),
instrument: false,
Expand All @@ -76,7 +76,7 @@ test('Returns source string with inline maps when no transformOptions is passed
sourceString,
'dummy_path.js',
{
cacheFS: new Map<string, string>(),
cacheFS: new Map<string, Buffer | string>(),
config: makeProjectConfig(),
configString: JSON.stringify(makeProjectConfig()),
instrument: false,
Expand Down Expand Up @@ -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<string, string>(),
cacheFS: new Map<string, Buffer | string>(),
config: makeProjectConfig(),
configString: JSON.stringify(makeProjectConfig()),
instrument: false,
Expand All @@ -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<string, string>(),
cacheFS: new Map<string, Buffer | string>(),
config: makeProjectConfig(),
configString: JSON.stringify(makeProjectConfig()),
instrument: false,
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-repl/src/cli/repl.ts
Expand Up @@ -33,7 +33,7 @@ const evalCommand: repl.REPLEval = (
cmd,
jestGlobalConfig.replname ?? 'jest.js',
{
cacheFS: new Map<string, string>(),
cacheFS: new Map<string, Buffer | string>(),
config: jestProjectConfig,
configString: JSON.stringify(jestProjectConfig),
instrument: false,
Expand Down
28 changes: 25 additions & 3 deletions packages/jest-runtime/src/index.ts
Expand Up @@ -153,7 +153,7 @@ const supportsNodeColonModulePrefixInRequire = (() => {
})();

export default class Runtime {
private readonly _cacheFS: Map<string, string>;
private readonly _cacheFS: Map<string, Buffer | string>;
private readonly _config: Config.ProjectConfig;
private readonly _globalConfig?: Config.GlobalConfig;
private readonly _coverageOptions: ShouldInstrumentOptions;
Expand Down Expand Up @@ -211,7 +211,7 @@ export default class Runtime {
environment: JestEnvironment,
resolver: Resolver,
transformer: ScriptTransformer,
cacheFS: Map<string, string>,
cacheFS: Map<string, Buffer | string>,
coverageOptions: ShouldInstrumentOptions,
testPath: string,
// TODO: make mandatory in Jest 30
Expand Down Expand Up @@ -446,7 +446,7 @@ export default class Runtime {

if (modulePath.endsWith('.wasm')) {
const wasm = this._importWasmModule(
fs.readFileSync(modulePath),
this.readFileBuffer(modulePath),
modulePath,
context,
);
Expand Down Expand Up @@ -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);

Expand All @@ -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;
}

Expand Down

0 comments on commit 0d4945e

Please sign in to comment.