Skip to content

fix: isolate callId counter per sandbox for parallel test support#2715

Open
singhvishalkr wants to merge 1 commit into
sinonjs:mainfrom
singhvishalkr:fix-parallel-sandbox-callid
Open

fix: isolate callId counter per sandbox for parallel test support#2715
singhvishalkr wants to merge 1 commit into
sinonjs:mainfrom
singhvishalkr:fix-parallel-sandbox-callid

Conversation

@singhvishalkr
Copy link
Copy Markdown

When tests run in parallel with separate sandboxes, the \calledImmediatelyBefore\ and \calledImmediatelyAfter\ assertions fail unexpectedly. This happens because proxy-invoke.js uses a global \callId\ counter shared across all sandboxes.

The fix introduces a per-sandbox context object that holds its own \callId\ counter. Each sandbox passes this context through the spy/stub/fake creation chain, and the proxy stores it for use during invocation.

Implementation:

  • proxy-invoke.js: Use \ his.sinonContext.callId\ instead of a global variable, with a default context for backward compatibility when used outside sandboxes
  • proxy.js: Accept optional context parameter and store it on the proxy
  • spy.js/stub.js/fake.js: Thread context through creation functions
  • sandbox.js: Create a sandbox-specific context and pass it to spy/stub/fake via new \withContext\ methods

Added tests that verify call order assertions work correctly across independent sandboxes.

The public API remains unchanged. Direct use of \sinon.spy()\ without a sandbox uses a shared default context (same behavior as before), while sandbox-created spies/stubs/fakes get isolated counters.

Fixes #2472

Made with Cursor

The global callId counter in proxy-invoke.js caused calledImmediatelyBefore
and calledImmediatelyAfter to fail when tests run in parallel with separate
sandboxes. Each sandbox now maintains its own callId counter, passed through
the spy/stub/fake creation chain via a context object.

Fixes sinonjs#2472
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.

Sinon fails in comparing call order when running tests in parallel

1 participant