[snap] patch bug in clearing require cache

Snap currently has a bug in which the require cache is not correctly cleared 
when running in filter mode (#tests < 2 * #workers). 

- We're currently clearing all entries in the require cache of worker threads, 
including `jest-worker` and `snap/dist/...` files 

- jest-worker seems to `require` these files on every dispatch (i.e. 
`worker.compile` seems to call `require(`compiler-worker`).compile`)
This commit is contained in:
mofeiZ
2023-05-11 13:53:06 -04:00
parent 5de02b881b
commit 04cee98166
@@ -11,10 +11,26 @@ import { exists } from "./utils";
const originalConsoleError = console.error;
// Subpaths to ignore when clearing the require cache
const ignoredRequireSubpaths: Array<string> = [
// compiler worker runner files
"node_modules/jest-worker",
// snap source files
"packages/snap",
];
const ignoredRequirePaths: Set<string> = new Set(
Object.keys(require.cache).filter(
(path) =>
!ignoredRequireSubpaths.every((ignored) => !path.includes(ignored))
)
);
let version: number | null = null;
export function clearRequireCache() {
Object.keys(require.cache).forEach(function (key) {
delete require.cache[key];
Object.keys(require.cache).forEach(function (path) {
if (!ignoredRequirePaths.has(path)) {
delete require.cache[path];
}
});
}