From 3ec9e057e34a0cb71d0b0b3c9e895f54947beecb Mon Sep 17 00:00:00 2001 From: Biki-das Date: Fri, 6 Jan 2023 00:41:17 +0530 Subject: [PATCH 1/3] Using scriptTransformer cache in jest-runner --- packages/jest-runtime/src/index.ts | 38 +++++++++++------------------- 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index dcca9ceef75d..b5d1ca1a6693 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -1574,23 +1574,18 @@ export default class Runtime { return source; } - let transformedFile: TransformResult | undefined = - this._fileTransforms.get(filename); - - if (transformedFile) { - return transformedFile.code; - } - - transformedFile = this._scriptTransformer.transform( + const transformedFile = this._scriptTransformer.transform( filename, this._getFullTransformationOptions(options), source, ); - this._fileTransforms.set(filename, { - ...transformedFile, - wrapperLength: this.constructModuleWrapperStart().length, - }); + if (this._fileTransforms.get(filename)?.code !== transformedFile.code) { + this._fileTransforms.set(filename, { + ...transformedFile, + wrapperLength: this.constructModuleWrapperStart().length, + }); + } if (transformedFile.sourceMapPath) { this._sourceMapRegistry.set(filename, transformedFile.sourceMapPath); @@ -1608,23 +1603,18 @@ export default class Runtime { return source; } - let transformedFile: TransformResult | undefined = - this._fileTransforms.get(filename); - - if (transformedFile) { - return transformedFile.code; - } - - transformedFile = await this._scriptTransformer.transformAsync( + const transformedFile = await this._scriptTransformer.transformAsync( filename, this._getFullTransformationOptions(options), source, ); - this._fileTransforms.set(filename, { - ...transformedFile, - wrapperLength: 0, - }); + if (this._fileTransforms.get(filename)?.code !== transformedFile.code) { + this._fileTransforms.set(filename, { + ...transformedFile, + wrapperLength: 0, + }); + } if (transformedFile.sourceMapPath) { this._sourceMapRegistry.set(filename, transformedFile.sourceMapPath); From 503d86f82155c9c01ca2724d4917f8b98f98d2cd Mon Sep 17 00:00:00 2001 From: Biki-das Date: Fri, 6 Jan 2023 00:59:19 +0530 Subject: [PATCH 2/3] Removed transformed files import --- packages/jest-runtime/src/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index b5d1ca1a6693..af78dae7733f 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -42,7 +42,6 @@ import { CallerTransformOptions, ScriptTransformer, ShouldInstrumentOptions, - TransformResult, TransformationOptions, handlePotentialSyntaxError, shouldInstrument, From 99093ae9a0a7b4fb234ee488045d72f7b6181d5a Mon Sep 17 00:00:00 2001 From: Biki-das Date: Sat, 7 Jan 2023 10:30:46 +0530 Subject: [PATCH 3/3] Removed the if condition and Added the changelog --- CHANGELOG.md | 1 + packages/jest-runtime/src/index.ts | 10 ++++------ 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1984784c897e..2b1759760d32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ - `[@jest/expect-utils]` `toMatchObject` should handle `Symbol` properties ([#13639](https://github.com/facebook/jest/pull/13639)) - `[jest-resolve]` Add global paths to `require.resolve.paths` ([#13633](https://github.com/facebook/jest/pull/13633)) - `[jest-runtime]` Support Wasm files that import JS resources ([#13608](https://github.com/facebook/jest/pull/13608)) +- `[jest-runtime]` Using the scriptTransformer cache in jest-runner ([#13735](https://github.com/facebook/jest/pull/13735)) - `[jest-snapshot]` Make sure to import `babel` outside of the sandbox ([#13694](https://github.com/facebook/jest/pull/13694)) ### Chore & Maintenance diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index af78dae7733f..2729651f622a 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -1579,12 +1579,10 @@ export default class Runtime { source, ); - if (this._fileTransforms.get(filename)?.code !== transformedFile.code) { - this._fileTransforms.set(filename, { - ...transformedFile, - wrapperLength: this.constructModuleWrapperStart().length, - }); - } + this._fileTransforms.set(filename, { + ...transformedFile, + wrapperLength: this.constructModuleWrapperStart().length, + }); if (transformedFile.sourceMapPath) { this._sourceMapRegistry.set(filename, transformedFile.sourceMapPath);