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:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'@backstage/cli': patch
|
||||
---
|
||||
|
||||
Tweak the Jest Caching loader to only operate when in `watch` mode
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user