cli: remove jest script module cache

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2025-09-29 13:53:28 +02:00
parent 9bfc64016c
commit f2cf564518
2 changed files with 5 additions and 17 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/cli': patch
---
Removed the script transform cache from the default Jest configuration. The script cache provided a moderate performance boost, but it is incomatible with Jest 30.
@@ -16,29 +16,12 @@
const { default: JestRuntime } = require('jest-runtime');
const scriptTransformCache = new Map();
module.exports = class CachingJestRuntime extends JestRuntime {
constructor(config, ...restArgs) {
super(config, ...restArgs);
this.allowLoadAsEsm = config.extensionsToTreatAsEsm.includes('.mts');
}
// 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.
createScriptFromCode(scriptSource, filename) {
let script = scriptTransformCache.get(scriptSource);
if (!script) {
script = super.createScriptFromCode(scriptSource, filename);
// Tried to store the script object in a WeakRef here. It starts out at
// about 90% hit rate, but eventually drops all the way to 20%, and overall
// it seemed to increase memory usage by 20% or so.
scriptTransformCache.set(scriptSource, script);
}
return script;
}
// Unfortunately we need to use this unstable API to make sure that .js files
// are only loaded as modules where ESM is supported, i.e. Node.js packages.
unstable_shouldLoadAsEsm(path, ...restArgs) {