diff --git a/src/test/helpers/mocha-bootstrap.js b/src/test/helpers/mocha-bootstrap.js index 6b952ee62e3..c00e1ca1822 100644 --- a/src/test/helpers/mocha-bootstrap.js +++ b/src/test/helpers/mocha-bootstrap.js @@ -51,6 +51,11 @@ process.on("unhandledRejection", (error) => { throw error; }); +// Absolute require.cache keys of the custom undici nock helper (src/test/helpers/nock.ts), +// which sits next to this file. Looking them up directly avoids scanning ~3800 cache keys +// after every test. +const CUSTOM_NOCK_PATHS = [path.join(__dirname, "nock.ts"), path.join(__dirname, "nock.js")]; + let suiteFakes = new Set(); /** @@ -76,19 +81,17 @@ function cleanup(ctx) { enforceNetConnectPolicy(ctx); // Safely clean up custom nock (src/test/helpers/nock.ts) if required by tests - for (const key of Object.keys(require.cache)) { - if (key.endsWith("test/helpers/nock.ts") || key.endsWith("test/helpers/nock.js")) { - try { - const mod = require.cache[key]; - if (mod && mod.exports) { - const customNock = mod.exports.default || mod.exports; - if (typeof customNock.cleanAll === "function") { - customNock.cleanAll(); - } + for (const key of CUSTOM_NOCK_PATHS) { + try { + const mod = require.cache[key]; + if (mod && mod.exports) { + const customNock = mod.exports.default || mod.exports; + if (typeof customNock.cleanAll === "function") { + customNock.cleanAll(); } - } catch { - // Ignore cleanup errors from custom nock } + } catch { + // Ignore cleanup errors from custom nock } } }