cli: remove redundant caching from jest module loader

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2022-12-08 23:11:02 +01:00
parent 07283a62c1
commit 00196b700c
2 changed files with 0 additions and 63 deletions
-5
View File
@@ -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',
@@ -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);
};