Merge pull request #13943 from mitchhentgesspotify/caching-jest-only-watch-mode

CachingJestRuntime should not do extra work if not watching
This commit is contained in:
Patrik Oldsberg
2022-10-04 13:42:07 +02:00
committed by GitHub
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);
};