From 6acbbeeb46a07466dbf337cf26864a42576a79bd Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 27 Apr 2020 20:11:03 +0200 Subject: [PATCH] fix: do not transform internally imported files --- CHANGELOG.md | 1 + packages/jest-runtime/src/index.ts | 59 +++++++++++++++++++----------- 2 files changed, 38 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 07c630555d7e..904dc0a2c843 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ - `[jest-resolve]` Update `resolve` to a version using native `realpath`, which is faster than the default JS implementation ([#9872](https://github.com/facebook/jest/pull/9872)) - `[jest-resolve]` Pass custom cached `realpath` function to `resolve` ([#9873](https://github.com/facebook/jest/pull/9873)) +- `[jest-runtime]` Do not pass files required internally through transformation when loading them ## 25.4.0 diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index ecb2d25f3ef8..06b50eab2b89 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -343,13 +343,13 @@ class Runtime { return core; } - const transformedFile = this.transformFile(modulePath, { + const transformedCode = this.transformFile(modulePath, { isInternalModule: false, supportsDynamicImport: true, supportsStaticESM: true, }); - const module = new SourceTextModule(transformedFile.code, { + const module = new SourceTextModule(transformedCode, { context, identifier: modulePath, importModuleDynamically: this.linkModules.bind(this), @@ -471,7 +471,7 @@ class Runtime { const manualMock = moduleName && this._resolver.getMockModule(from, moduleName); if ( - (!options || !options.isInternalModule) && + !options?.isInternalModule && !isRequireActual && !moduleResource && manualMock && @@ -491,7 +491,9 @@ class Runtime { let moduleRegistry; - if (!options || !options.isInternalModule) { + if (options?.isInternalModule) { + moduleRegistry = this._internalModuleRegistry; + } else { if ( this._moduleRegistry.get(modulePath) || !this._isolatedModuleRegistry @@ -500,8 +502,6 @@ class Runtime { } else { moduleRegistry = this._isolatedModuleRegistry; } - } else { - moduleRegistry = this._internalModuleRegistry; } const module = moduleRegistry.get(modulePath); @@ -641,7 +641,7 @@ class Runtime { moduleRegistry: ModuleRegistry, ) { if (path.extname(modulePath) === '.json') { - const text = stripBOM(fs.readFileSync(modulePath, 'utf8')); + const text = stripBOM(this.readFile(modulePath)); const transformedFile = this._scriptTransformer.transformJson( modulePath, @@ -937,7 +937,7 @@ class Runtime { value: this._createRequireImplementation(localModule, options), }); - const transformedFile = this.transformFile(filename, options); + const transformedCode = this.transformFile(filename, options); let compiledFunction: ModuleWrapper | null = null; @@ -949,7 +949,7 @@ class Runtime { if (typeof compileFunction === 'function') { try { compiledFunction = compileFunction( - transformedFile.code, + transformedCode, this.constructInjectedModuleParameters(), { filename, @@ -960,10 +960,7 @@ class Runtime { throw handlePotentialSyntaxError(e); } } else { - const script = this.createScriptFromCode( - transformedFile.code, - filename, - ); + const script = this.createScriptFromCode(transformedCode, filename); const runScript = script.runInContext( vmContext, @@ -977,7 +974,7 @@ class Runtime { } } } else { - const script = this.createScriptFromCode(transformedFile.code, filename); + const script = this.createScriptFromCode(transformedCode, filename); const runScript = this._environment.runScript( script, @@ -1030,22 +1027,28 @@ class Runtime { this._currentlyExecutingModulePath = lastExecutingModulePath; } - private transformFile(filename: string, options?: InternalModuleOptions) { + private transformFile( + filename: string, + options?: InternalModuleOptions, + ): string { + const source = this.readFile(filename); + + if (options?.isInternalModule) { + return source; + } + const transformedFile = this._scriptTransformer.transform( filename, this._getFullTransformationOptions(options), - this._cacheFS[filename], + source, ); - // we only care about non-internal modules - if (!options || !options.isInternalModule) { - this._fileTransforms.set(filename, transformedFile); - } + this._fileTransforms.set(filename, transformedFile); if (transformedFile.sourceMapPath) { this._sourceMapRegistry[filename] = transformedFile.sourceMapPath; } - return transformedFile; + return transformedFile.code; } private createScriptFromCode(scriptSource: string, filename: string) { @@ -1275,7 +1278,7 @@ class Runtime { resolve.paths = (moduleName: string) => this._requireResolvePaths(from.filename, moduleName); - const moduleRequire = (options && options.isInternalModule + const moduleRequire = (options?.isInternalModule ? (moduleName: string) => this.requireInternalModule(from.filename, moduleName) : this.requireModuleOrMock.bind( @@ -1607,6 +1610,18 @@ 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 {