From 00196b700ca13c1a7528e9cef7826ffd1a9419cc Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Thu, 8 Dec 2022 23:11:02 +0100 Subject: [PATCH] cli: remove redundant caching from jest module loader Signed-off-by: Patrik Oldsberg --- packages/cli/config/jest.js | 5 -- .../cli/config/jestCachingModuleLoader.js | 58 ------------------- 2 files changed, 63 deletions(-) diff --git a/packages/cli/config/jest.js b/packages/cli/config/jest.js index 2c224f8161..24385a6b99 100644 --- a/packages/cli/config/jest.js +++ b/packages/cli/config/jest.js @@ -25,11 +25,6 @@ const envOptions = { enableSourceMaps: Boolean(process.env.ENABLE_SOURCE_MAPS), }; -if (!envOptions.oldTests) { - // Needed so that, at import-time, it can hook into Jest's internals. - require('./jestCachingModuleLoader'); -} - const transformIgnorePattern = [ '@material-ui', 'ajv', diff --git a/packages/cli/config/jestCachingModuleLoader.js b/packages/cli/config/jestCachingModuleLoader.js index 7858a7cfb7..95c6212123 100644 --- a/packages/cli/config/jestCachingModuleLoader.js +++ b/packages/cli/config/jestCachingModuleLoader.js @@ -14,61 +14,11 @@ * limitations under the License. */ -const fs = require('fs'); const { default: JestRuntime } = require('jest-runtime'); -const fileTransformCache = new Map(); const scriptTransformCache = new Map(); -let runtimeGeneration = 0; -let isWatchMode; - module.exports = class CachingJestRuntime extends JestRuntime { - // Each Jest run creates a new runtime, including when rerunning tests in - // watch mode. This keeps track of whether we've switched runtime instance. - __runtimeGeneration = runtimeGeneration++; - - transformFile(filename, options) { - if (!isWatchMode) { - return super.transformFile(filename, options); - } - - const entry = fileTransformCache.get(filename); - if (entry) { - // Only check modification time if it's from a different runtime generation - if (entry.generation === this.__runtimeGeneration) { - return entry.code; - } - - // Keep track of the modification time of files so that we can properly - // reprocess them in watch mode. - const { mtimeMs } = fs.statSync(filename); - if (mtimeMs > entry.mtimeMs) { - const code = super.transformFile(filename, options); - fileTransformCache.set(filename, { - code, - mtimeMs, - generation: this.__runtimeGeneration, - }); - return code; - } - - fileTransformCache.set(filename, { - ...entry, - generation: this.__runtimeGeneration, - }); - return entry.code; - } - - const code = super.transformFile(filename, options); - fileTransformCache.set(filename, { - code, - mtimeMs: fs.statSync(filename).mtimeMs, - generation: this.__runtimeGeneration, - }); - return code; - } - // This may or may not be a good idea. Theoretically I don't know why this would impact // test correctness and flakiness, but it seems like it may introduce flakiness and strange failures. // It does seem to speed up test execution by a fair amount though. @@ -84,11 +34,3 @@ module.exports = class CachingJestRuntime extends JestRuntime { return script; } }; - -// Inject hook into createHasteMap, as it's the only way that we can -// determine (from our scope here) if we're in "watch mode" or not. -const originalCreateHasteMap = JestRuntime.createHasteMap; -JestRuntime.createHasteMap = (config, options = undefined) => { - isWatchMode = options && options.watch; - return originalCreateHasteMap(config, options); -};