Conversation
…410594) Enforces automatic Sinon and Nock cleanup between tests by registering a root hook plugin in mocha-bootstrap.js. This ensures all stubs, spies, fakes, and HTTP interceptors are restored after each test across the entire suite, preventing mock leaks and order-dependent test failures.
There was a problem hiding this comment.
Code Review
This pull request updates the Mocha bootstrap helper to automatically restore Sinon mocks and clean up Nock interceptors after each test. The review feedback highlights two issues: first, nock.cleanAll() does not clear interceptors created by the custom nock helper (which uses undici's MockAgent), potentially leading to state leakage; second, the conditional afterEach registration is redundant and potentially problematic when exports.mochaHooks is already used, so it should be removed.
| if (typeof afterEach === "function") { | ||
| afterEach(cleanup); | ||
| } |
There was a problem hiding this comment.
Mocha 8+ officially supports root hooks via exports.mochaHooks in required files. When this file is loaded via Mocha's --require flag, the global afterEach function is typically not yet defined, making this if block dead code. If it is defined in some environments, it would register the cleanup function twice (once globally and once via mochaHooks), causing it to run twice per test.
To keep the bootstrap clean and avoid redundant execution, we should remove this conditional block and rely solely on exports.mochaHooks.
There was a problem hiding this comment.
Removed the redundant conditional check. We now rely exclusively on exports.mochaHooks = { afterEach: cleanup }, which resolves the ESLint no-undef error and avoids duplicate execution.
- Remove redundant conditional afterEach check to resolve ESLint no-undef error - Add JSDoc documentation and fix Prettier trailing newline formatting - Dynamically invoke cleanAll on custom nock (src/test/helpers/nock.ts) if loaded in require.cache
…ss tests Preserve stubs registered in suite before() hooks while continuing to automatically restore stubs, spies, and fakes created within beforeEach() and individual it() tests.
Summary
Enforces universal mock and stub teardown across all Mocha test suites in
src/test/helpers/mocha-bootstrap.js.Changes
sinonand registered a root hook plugin (exports.mochaHooks = { afterEach: cleanup }) with a globalafterEachfallback.sinon.restore()andnock.cleanAll()run hermetically after every test case across all suites.afterEachhooks.Buganizer Reference