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 four assertions to t.assert for checking mock function calls:

  • t.assert.called(fn[, message])
  • t.assert.callCount(fn, times[, message])
  • t.assert.nthCalledWith(fn, n, ...args)
  • t.assert.lastCalledWith(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. Like the other t.assert methods, these count toward t.plan().

nthCalledWith (1-based, like Jest/Vitest) and lastCalledWith check a single call, so their failures show a regular deepStrictEqual diff.

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.nthCalledWith(transport, 1, {
  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.nthCalledWith(
  process.stdout.write,
  1,
  '::debug::[00:00:00.000] \x1B[34mDEBUG\x1B[39m: Test message\n'
);

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.40%. Comparing base (bbd566d) to head (be90007).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #66476      +/-   ##
==========================================
- Coverage   92.74%   90.40%   -2.35%     
==========================================
  Files         422      791     +369     
  Lines      193170   276097   +82927     
  Branches    29783    52986   +23203     
==========================================
+ Hits       179160   249605   +70445     
- Misses      13682    16905    +3223     
- Partials      328     9587    +9259     
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.44%) ⬆️

... and 499 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 thread lib/internal/test_runner/assert.js Outdated
@araujogui
araujogui force-pushed the test-runner-mock-call-assertions branch 2 times, most recently from 62f7866 to 32a98df Compare October 4, 2026 21:02
Add t.assert.called(), t.assert.callCount(), t.assert.nthCalledWith()
and t.assert.lastCalledWith() for asserting on mock function calls.

Assisted-by: Claude
Signed-off-by: Guilherme Araújo <arauujogui@gmail.com>
@araujogui
araujogui force-pushed the test-runner-mock-call-assertions branch from 32a98df to be90007 Compare October 5, 2026 14:40
@araujogui
araujogui requested a review from atlowChemi October 5, 2026 14:42
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