Conversation
Add t.assert.called(), t.assert.callCount() and t.assert.calledWith() for asserting on mock function calls. Assisted-by: Claude Signed-off-by: Guilherme Araújo <arauujogui@gmail.com>
Collaborator
|
Review requested:
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #66476 +/- ##
========================================
Coverage 90.42% 90.42%
========================================
Files 791 790 -1
Lines 275611 275509 -102
Branches 52845 52834 -11
========================================
- Hits 249209 249135 -74
+ Misses 16810 16771 -39
- Partials 9592 9603 +11
🚀 New features to boost your workflow:
|
atlowChemi
reviewed
Oct 4, 2026
Comment on lines
+84
to
+98
| function calledWith(fn, ...args) { | ||
| const calls = ArrayPrototypeMap(getMockContext(fn).calls, (call) => call.arguments); | ||
|
|
||
| if (!ArrayPrototypeSome(calls, (callArgs) => isDeepStrictEqual(callArgs, args))) { | ||
| throw new AssertionError({ | ||
| __proto__: null, | ||
| message: `Expected mock function to have been called with ${inspect(args)}\n` + | ||
| `Received calls: ${inspect(calls)}`, | ||
| actual: calls, | ||
| expected: args, | ||
| operator: 'calledWith', | ||
| stackStartFn: calledWith, | ||
| }); | ||
| } | ||
| } |
Member
There was a problem hiding this comment.
I am not sure about this one, it feels to me it might be better to have an nthCalledWith etc, as right now this shows the actual as all args from all calls, while the expected is for a specific call (of any index)
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.
Adds three assertions to
t.assertfor checking mock function calls:t.assert.called(fn[, message])t.assert.callCount(fn, times[, message])t.assert.calledWith(fn, ...args)This is syntactic sugar. Everything here can already be written with
fn.mock.callCount(),fn.mock.callsandassert.deepStrictEqual(). It is also possible today in userland withassert.register(). The goal is to make the most common mock checks shorter, and to give clearer failure messages:calledWithprints the expected arguments and every recorded call. Like the othert.assertmethods, these count towardt.plan().calledWithpasses if any recorded call deep-strict-equals the given arguments.Examples from nodejs/doc-kit
logger.test.mjstransports/github.test.mjs