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 <mhentges@spotify.com>
This commit is contained in:
Mitchell Hentges
2022-09-30 14:13:09 +02:00
parent 1c0aed4dd2
commit 78d5eb299e
3 changed files with 23 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/cli': patch
---
Tweak the Jest Caching loader to only operate when in `watch` mode
+5
View File
@@ -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',
@@ -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);
};