Skip to content

[AI Improvement] [Task] Enforce global mock and stub teardown in mocha-bootstrap - #11105

Open
joehan wants to merge 3 commits into
mainfrom
ai-improve-563410594-task-enforce-global-mock-and-stub-t
Open

joehan wants to merge 3 commits into
mainfrom
ai-improve-563410594-task-enforce-global-mock-and-stub-t

Conversation

@joehan

@joehan joehan commented Sep 18, 2026

Copy link
Copy Markdown
Member

Summary

Enforces universal mock and stub teardown across all Mocha test suites in src/test/helpers/mocha-bootstrap.js.

Changes

  • Required sinon and registered a root hook plugin (exports.mochaHooks = { afterEach: cleanup }) with a global afterEach fallback.
  • Ensures sinon.restore() and nock.cleanAll() run hermetically after every test case across all suites.
  • Preserves all localized test intentions and existing afterEach hooks.

Buganizer Reference

  • Buganizer Issue: b/563410594
  • Parent Goal: b/563410897

…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.
@joehan joehan self-assigned this Sep 18, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

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.

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.

Comment thread src/test/helpers/mocha-bootstrap.js
Comment thread src/test/helpers/mocha-bootstrap.js Outdated
Comment on lines +35 to +37
if (typeof afterEach === "function") {
afterEach(cleanup);
}

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

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.

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.

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.
@joehan
joehan marked this pull request as ready for review September 18, 2026 20:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants