diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 06b50eab2b89..86dedbce5827 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -641,7 +641,7 @@ class Runtime { moduleRegistry: ModuleRegistry, ) { if (path.extname(modulePath) === '.json') { - const text = stripBOM(this.readFile(modulePath)); + const text = stripBOM(fs.readFileSync(modulePath, 'utf8')); const transformedFile = this._scriptTransformer.transformJson( modulePath, @@ -1031,16 +1031,14 @@ class Runtime { filename: string, options?: InternalModuleOptions, ): string { - const source = this.readFile(filename); - if (options?.isInternalModule) { - return source; + return this._cacheFS[filename] ?? fs.readFileSync(filename, 'utf8'); } const transformedFile = this._scriptTransformer.transform( filename, this._getFullTransformationOptions(options), - source, + this._cacheFS[filename], ); this._fileTransforms.set(filename, transformedFile); @@ -1610,18 +1608,6 @@ class Runtime { xtest: this._environment.global.xtest, }; } - - private readFile(filename: Config.Path): string { - let source = this._cacheFS[filename]; - - if (!source) { - source = fs.readFileSync(filename, 'utf8'); - - this._cacheFS[filename] = source; - } - - return source; - } } function invariant(condition: unknown, message?: string): asserts condition {