From b111f31e4f3ccc3dca70a6721a7700bdfffac1fd Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 27 Apr 2020 20:11:03 +0200 Subject: [PATCH 1/3] 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 99e5f01ac925..89c7faac2588 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,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]` Add `teardown` method to clear any caches when tests complete ([#9906](https://github.com/facebook/jest/pull/9906)) +- `[jest-runtime]` Do not pass files required internally through transformation when loading them ([#9900](https://github.com/facebook/jest/pull/9900)) ## 25.4.0 diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 59b48d66bc04..737258df6c92 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, @@ -965,7 +965,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; @@ -977,7 +977,7 @@ class Runtime { if (typeof compileFunction === 'function') { try { compiledFunction = compileFunction( - transformedFile.code, + transformedCode, this.constructInjectedModuleParameters(), { filename, @@ -988,10 +988,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, @@ -1005,7 +1002,7 @@ class Runtime { } } } else { - const script = this.createScriptFromCode(transformedFile.code, filename); + const script = this.createScriptFromCode(transformedCode, filename); const runScript = this._environment.runScript( script, @@ -1058,22 +1055,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) { @@ -1303,7 +1306,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( @@ -1635,6 +1638,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 { From 240f3aa4ae1ee3ed080a9b36c3483e9979cd9109 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 27 Apr 2020 22:34:28 +0200 Subject: [PATCH 2/3] no cached readfile --- packages/jest-runtime/src/index.ts | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 737258df6c92..dc23b3c63a1d 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, @@ -1059,16 +1059,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); @@ -1638,18 +1636,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 { From 5cb78d89ee4b5885966ce0da4fc610b88884fba4 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 28 Apr 2020 13:16:33 +0200 Subject: [PATCH 3/3] Revert "no cached readfile" This reverts commit 00c572ae43942321d35d7bffefa460c4bee60619. --- packages/jest-runtime/src/index.ts | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index dc23b3c63a1d..737258df6c92 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(fs.readFileSync(modulePath, 'utf8')); + const text = stripBOM(this.readFile(modulePath)); const transformedFile = this._scriptTransformer.transformJson( modulePath, @@ -1059,14 +1059,16 @@ class Runtime { filename: string, options?: InternalModuleOptions, ): string { + const source = this.readFile(filename); + if (options?.isInternalModule) { - return this._cacheFS[filename] ?? fs.readFileSync(filename, 'utf8'); + return source; } const transformedFile = this._scriptTransformer.transform( filename, this._getFullTransformationOptions(options), - this._cacheFS[filename], + source, ); this._fileTransforms.set(filename, transformedFile); @@ -1636,6 +1638,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 {