From d2420aaf42055097dfe1e6a54bf3701214b06402 Mon Sep 17 00:00:00 2001 From: BIKI DAS Date: Sat, 7 Jan 2023 18:24:02 +0530 Subject: [PATCH] JestRuntime:-Using scriptTransformer cache in jest-runner (#13735) --- CHANGELOG.md | 1 + packages/jest-runtime/src/index.ts | 29 ++++++++--------------------- 2 files changed, 9 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d84c581f30c8..067791482e22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ - `[jest-mock]` fix mockReset and resetAllMocks undefined return ([#13692](https://github.com/facebook/jest/pull/13692)) - `[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 dcca9ceef75d..2729651f622a 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, @@ -1574,14 +1573,7 @@ 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, @@ -1608,23 +1600,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);