Skip to content

test_runner: add mock call assertions to t.assert - #66476

Open
araujogui wants to merge 1 commit into
nodejs:mainfrom
araujogui:test-runner-mock-call-assertions
Open

araujogui wants to merge 1 commit into
nodejs:mainfrom
araujogui:test-runner-mock-call-assertions

Conversation

@araujogui

@araujogui araujogui commented Oct 3, 2026 •

Copy link
Copy Markdown
Member

Adds three assertions to t.assert for 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.calls and assert.deepStrictEqual(). It is also possible today in userland with assert.register(). The goal is to make the most common mock checks shorter, and to give clearer failure messages: calledWith prints the expected arguments and every recorded call. Like the other t.assert methods, these count toward t.plan().

calledWith passes if any recorded call deep-strict-equals the given arguments.

Examples from nodejs/doc-kit

logger.test.mjs

// Before
strictEqual(transport.mock.callCount(), 1);

const call = transport.mock.calls[0];
deepStrictEqual(call.arguments, [
  {
    level: LogLevel.debug,
    message: 'Hello, World!',
    metadata,
    module: undefined,
    timestamp: 0,
  },
]);

// After
t.assert.callCount(transport, 1);
t.assert.calledWith(transport, {
  level: LogLevel.debug,
  message: 'Hello, World!',
  metadata,
  module: undefined,
  timestamp: 0,
});

transports/github.test.mjs

// Before
const callsArgs = process.stdout.write.mock.calls.map(
  call => call.arguments[0]
);

strictEqual(process.stdout.write.mock.callCount(), 1);
strictEqual(
  callsArgs[0].trim(),
  '::debug::[00:00:00.000] \x1B[34mDEBUG\x1B[39m: Test message'
);

// After
t.assert.callCount(process.stdout.write, 1);
t.assert.calledWith(
  process.stdout.write,
  '::debug::[00:00:00.000] \x1B[34mDEBUG\x1B[39m: Test message\n'
);

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>
Copilot AI balanced review requested due to automatic review settings October 3, 2026 10:22
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/test_runner

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. test_runner Issues and PRs related to the test runner subsystem. labels Oct 3, 2026
@codecov

codecov Bot commented Oct 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.42%. Comparing base (4486dce) to head (236ff00).
⚠️ Report is 21 commits behind head on main.

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     
Files with missing lines Coverage Δ
lib/internal/test_runner/assert.js 100.00% <100.00%> (ø)
lib/internal/test_runner/mock/mock.js 97.32% <100.00%> (+<0.01%) ⬆️

... and 41 files with indirect coverage changes

🚀 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.

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,
});
}
}

@atlowChemi atlowChemi Oct 4, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-ci PRs that need a full CI run. test_runner Issues and PRs related to the test runner subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants