Skip to content

fix(mock): improve error message when intercepts are exhausted#4912

Merged
mcollina merged 1 commit intonodejs:mainfrom
travisbreaks:fix/better-mock-intercept-error
Mar 21, 2026
Merged

fix(mock): improve error message when intercepts are exhausted#4912
mcollina merged 1 commit intonodejs:mainfrom
travisbreaks:fix/better-mock-intercept-error

Conversation

@travisbreaks
Copy link
Contributor

Summary

  • When all mock intercepts for a request have been consumed, the error message now includes how many interceptors remain and how many were originally defined
  • Tracks total registered intercept count via a new kTotalDispatchCount symbol on the dispatches array, so the count persists even after consumed dispatches are cleaned up
  • Adds a dedicated test for the "intercepts exhausted" scenario

Before:

MockNotMatchedError: Mock dispatch not matched for path '/foo': subsequent request to origin https://api.example.com was not allowed (net.connect disabled)

After:

MockNotMatchedError: Mock dispatch not matched for path '/foo': subsequent request to origin https://api.example.com was not allowed (net.connect disabled), 0 interceptor(s) remaining out of 3 defined

Fixes #2219

Test plan

  • New test should include intercept count in error when intercepts are exhausted verifies the message includes counts after intercepts are consumed
  • All existing mock-agent tests updated and passing (99/99)
  • All mock-utils tests passing (31/31)
  • All mock-pool, mock-client, mock-interceptor, mock-scope tests passing (101/101)
  • Lint passes

🤖 Generated with Claude Code

When all mock intercepts for a request have been consumed, the error
message now includes how many interceptors remain and how many were
originally defined. This helps users understand that their intercepts
were used up rather than misconfigured.

Example: "Mock dispatch not matched for path '/foo': subsequent request
to origin https://api.example.com was not allowed (net.connect disabled),
0 interceptor(s) remaining out of 3 defined"

Fixes nodejs#2219

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@codecov-commenter
Copy link

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.90%. Comparing base (1c5dc1a) to head (5620df1).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4912   +/-   ##
=======================================
  Coverage   92.90%   92.90%           
=======================================
  Files         112      112           
  Lines       35638    35645    +7     
=======================================
+ Hits        33108    33116    +8     
+ Misses       2530     2529    -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mcollina mcollina merged commit 330f4e4 into nodejs:main Mar 21, 2026
37 checks passed
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.

when user creates less intercepts than is needed, throw error which explicitly states that the user has X intercepts, but Y requests were made

4 participants