diff --git a/packages/jest-cli/src/runTest.js b/packages/jest-cli/src/runTest.js index 31f55f150c16..9c8720fabd77 100644 --- a/packages/jest-cli/src/runTest.js +++ b/packages/jest-cli/src/runTest.js @@ -67,8 +67,9 @@ function runTest(path: Path, config: Config, resolver: Resolver) { BufferedConsole.write([], type, message, 4), ), ); + const cacheFS = {[path]: testSource}; setGlobal(env.global, 'console', testConsole); - const runtime = new ModuleLoader(config, env, resolver); + const runtime = new ModuleLoader(config, env, resolver, cacheFS); const start = Date.now(); return TestRunner(config, env, runtime, path) .then((result: TestResult) => { diff --git a/packages/jest-runtime/src/index.js b/packages/jest-runtime/src/index.js index 1685e8edd3ec..6d724052869e 100644 --- a/packages/jest-runtime/src/index.js +++ b/packages/jest-runtime/src/index.js @@ -92,11 +92,17 @@ class Runtime { _unmockList: ?RegExp; _virtualMocks: BooleanObject; - constructor(config: Config, environment: Environment, resolver: Resolver) { + constructor( + config: Config, + environment: Environment, + resolver: Resolver, + cacheFS?: {[path: string]: string} + ) { this._moduleRegistry = Object.create(null); this._internalModuleRegistry = Object.create(null); this._mockRegistry = Object.create(null); this._sourceMapRegistry = Object.create(null); + this._cacheFS = cacheFS || Object.create(null); this._config = config; this._environment = environment; this._resolver = resolver; @@ -455,9 +461,15 @@ class Runtime { localModule.paths = this._resolver.getModulePaths(dirname); localModule.require = this._createRequireImplementation(filename, options); - const transformedFile = transform(filename, this._config, { - isInternalModule, - }); + const transformedFile = transform( + filename, + this._config, + { + isInternalModule, + }, + this._cacheFS[filename] + ); + if (transformedFile.sourceMapPath) { this._sourceMapRegistry[filename] = transformedFile.sourceMapPath; diff --git a/packages/jest-runtime/src/transform.js b/packages/jest-runtime/src/transform.js index a3b063600576..8d141d56a543 100644 --- a/packages/jest-runtime/src/transform.js +++ b/packages/jest-runtime/src/transform.js @@ -346,9 +346,10 @@ const transformAndBuildScript = ( config: Config, options: ?Options, instrument: boolean, + fileSource?: string ): BuiltTransformResult => { const isInternalModule = !!(options && options.isInternalModule); - const content = stripShebang(fs.readFileSync(filename, 'utf8')); + const content = stripShebang(fileSource || fs.readFileSync(filename, 'utf8')); let wrappedCode: string; let sourceMapPath: ?string = null; const willTransform = !isInternalModule && @@ -396,6 +397,7 @@ module.exports = ( filename: Path, config: Config, options: Options, + fileSource?: string ): BuiltTransformResult => { const instrument = shouldInstrument(filename, config); const scriptCacheKey = getScriptCacheKey(filename, config, instrument); @@ -403,7 +405,13 @@ module.exports = ( if (result) { return result; } else { - result = transformAndBuildScript(filename, config, options, instrument); + result = transformAndBuildScript( + filename, + config, + options, + instrument, + fileSource + ); cache.set(scriptCacheKey, result); return result; }