From 78d5eb299e2f337abac2858e21dc2c338431e1ad Mon Sep 17 00:00:00 2001 From: Mitchell Hentges Date: Fri, 30 Sep 2022 14:13:09 +0200 Subject: [PATCH] CachingJestRuntime should not do extra work if not watching The benefits of `CachingJestRuntime` only apply if in `watch` mode. If not, then we're doing FS calls but not getting benefit from them. This is a small optimization that will hopefully make CI happier with our caching runtime, if/when it's used. Signed-off-by: Mitchell Hentges --- .changeset/curly-rats-itch.md | 5 +++++ packages/cli/config/jest.js | 5 +++++ packages/cli/config/jestCachingModuleLoader.js | 13 +++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 .changeset/curly-rats-itch.md diff --git a/.changeset/curly-rats-itch.md b/.changeset/curly-rats-itch.md new file mode 100644 index 0000000000..b6fba9dc1e --- /dev/null +++ b/.changeset/curly-rats-itch.md @@ -0,0 +1,5 @@ +--- +'@backstage/cli': patch +--- + +Tweak the Jest Caching loader to only operate when in `watch` mode diff --git a/packages/cli/config/jest.js b/packages/cli/config/jest.js index 8b6c37bda4..18f563a3a3 100644 --- a/packages/cli/config/jest.js +++ b/packages/cli/config/jest.js @@ -25,6 +25,11 @@ const envOptions = { enableSourceMaps: Boolean(process.env.ENABLE_SOURCE_MAPS), }; +if (envOptions.nextTests) { + // 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 9ef29d7485..7858a7cfb7 100644 --- a/packages/cli/config/jestCachingModuleLoader.js +++ b/packages/cli/config/jestCachingModuleLoader.js @@ -21,6 +21,7 @@ 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 @@ -28,6 +29,10 @@ module.exports = class CachingJestRuntime extends JestRuntime { __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 @@ -79,3 +84,11 @@ 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); +};