feat(mock): support asynchronous reply options callbacks#5534
Merged
mcollina merged 1 commit intoJul 9, 2026
Conversation
Fixes nodejs#1348 reply(callback) assumed the callback returns a plain object, so an async callback's promise passed the object check, spread into nothing, and dispatch threw InvalidArgumentError('statusCode must be defined'). Extend the isPromise pattern introduced for async data callbacks (nodejs#1758) to the reply options callback: resolve the dispatch data once the promise settles, and surface rejections through onResponseError since they can no longer throw synchronously from dispatch. Consumed bookkeeping now happens before callback resolution so a pending async callback cannot be re-matched beyond its allowed times, matching how async data callbacks already behave. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5534 +/- ##
=======================================
Coverage 93.44% 93.45%
=======================================
Files 110 110
Lines 37329 37376 +47
=======================================
+ Hits 34883 34928 +45
- Misses 2446 2448 +2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This relates to...
Fixes #1348 (see my comment there clarifying the scope vs #1757/#1758)
Rationale
#1758 made the data argument awaitable (
reply(200, async (opts) => body)), but the ask in #1348 — an async reply options callback — still fails onmain:wrappedDefaultsCallbackassumes the callback returns a plain object: a Promise passes thetypeof === 'object'check, contributes nothing when spread into{ data: '', responseOptions: {}, ...promise }, and validation throws. This is what Polly.JS/nock-style record-and-replay adapters need (loading recorded responses from disk asynchronously).Changes
Extends the existing
isPromisepattern from #1758:lib/mock/mock-interceptor.js:wrappedDefaultsCallbackdetects a promise result and resolves the dispatch data (format check + validation +createMockScopeDispatchData) once it settles.lib/mock/mock-utils.js:mockDispatchdetects a promise from the dispatch-data callback and defers the reply (extracted intodispatchMockReply) until it resolves. A rejection can no longer throw synchronously from dispatch, so it is surfaced viahandler.onResponseError— the same route asreplyWithError. Sync callbacks keep their synchronous-throw semantics.times— consistent with how async data callbacks already behave (they mark consumption at dispatch time).MockReplyOptionsCallbackmay return aPromiseof the reply options (extracted a namedMockReplyOptionstype).MockPool.md— callback description + a new async example.defaultReplyHeaders/replyContentLengthinteraction,times()bookkeeping, invalid resolved format, and rejection propagation; tsd assertions for the async signature.Features
MockInterceptor.reply()accepts an asynchronous reply options callback.Bug Fixes
N/A
Breaking Changes and Deprecations
N/A
Status
🤖 Generated with Claude Code