Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions src/test/helpers/mocha-bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const path = require("path");
const chai = require("chai");
const chaiAsPromised = require("chai-as-promised");
const sinon = require("sinon");
Expand Down Expand Up @@ -27,6 +28,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")];
Comment on lines +54 to +57

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using require.resolve is more robust and idiomatic than manually constructing absolute paths with path.join and hardcoding .ts and .js extensions. It leverages Node's internal resolution mechanism to find the exact path used in require.cache, automatically handling whatever extension is active (e.g., .ts when running with ts-node or .js when running compiled code), and avoids potential platform-specific path casing or separator issues.

Once this is applied, the const path = require("path"); import on line 1 is no longer needed and can be removed.

Suggested change
// 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 customNockPath;
try {
customNockPath = require.resolve("./nock");
} catch {
// Ignore if the custom nock helper is not present
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

path is still used for path.relative in isIntegrationTestFile, and require.resolve resolves to the same key here (it only finds .ts because ts-node is registered first in .mocharc.yml). Fine to leave as-is.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, thanks @shettyvarun268! Leaving as-is. Branch is re-synchronized with latest main and all local benchmarks, build, compile, and lint checks pass cleanly.


let suiteFakes = new Set();

/**
Expand All @@ -50,19 +56,17 @@ function cleanup() {
nock.cleanAll();

// 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
}
}
Comment on lines +84 to 96

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Update the cleanup loop to use the resolved customNockPath directly instead of iterating over CUSTOM_NOCK_PATHS.

Suggested change
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
}
}
if (customNockPath) {
try {
const mod = require.cache[customNockPath];
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
}
}

}
Expand Down
Loading