mirror of
https://github.com/nodejs/node.git
synced 2026-10-28 10:15:07 +08:00
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> PR-URL: https://github.com/nodejs/node/pull/66476 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: James M Snell <jasnell@gmail.com>
103 lines
2.7 KiB
JavaScript
103 lines
2.7 KiB
JavaScript
'use strict';
|
|
require('../common');
|
|
const assert = require('node:assert');
|
|
const test = require('node:test');
|
|
|
|
test('expected methods are on t.assert', (t) => {
|
|
const uncopiedKeys = [
|
|
'AssertionError',
|
|
'strict',
|
|
'Assert',
|
|
'options',
|
|
];
|
|
const assertKeys = Object.keys(assert).filter((key) => !uncopiedKeys.includes(key));
|
|
const expectedKeys = [
|
|
'snapshot',
|
|
'fileSnapshot',
|
|
'called',
|
|
'callCount',
|
|
'lastCalledWith',
|
|
'nthCalledWith',
|
|
].concat(assertKeys).sort();
|
|
assert.deepStrictEqual(Object.keys(t.assert).sort(), expectedKeys);
|
|
});
|
|
|
|
test('t.assert.ok correctly parses the stacktrace', (t) => {
|
|
t.assert.throws(() => t.assert.ok(1 === 2), /t\.assert\.ok\(1 === 2\)/);
|
|
});
|
|
|
|
test('t.assert.called', (t) => {
|
|
const fn = t.mock.fn();
|
|
t.assert.throws(() => t.assert.called(fn), {
|
|
code: 'ERR_ASSERTION',
|
|
message: 'Expected mock function to have been called',
|
|
});
|
|
fn();
|
|
t.assert.called(fn);
|
|
t.assert.throws(() => t.assert.called(() => {}), {
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
});
|
|
});
|
|
|
|
test('t.assert.callCount', (t) => {
|
|
const fn = t.mock.fn();
|
|
t.assert.callCount(fn, 0);
|
|
fn();
|
|
fn();
|
|
t.assert.callCount(fn, 2);
|
|
t.assert.throws(() => t.assert.callCount(fn, 1), {
|
|
code: 'ERR_ASSERTION',
|
|
message: 'Expected mock function to have been called 1 times, but it was called 2 times',
|
|
});
|
|
t.assert.throws(() => t.assert.callCount(fn, -1), {
|
|
code: 'ERR_OUT_OF_RANGE',
|
|
});
|
|
});
|
|
|
|
test('t.assert.nthCalledWith', (t) => {
|
|
const fn = t.mock.fn();
|
|
fn(1);
|
|
fn({ a: 2 });
|
|
t.assert.nthCalledWith(fn, 1, 1);
|
|
t.assert.nthCalledWith(fn, 2, { a: 2 });
|
|
t.assert.throws(() => t.assert.nthCalledWith(fn, 2, { a: 3 }), {
|
|
code: 'ERR_ASSERTION',
|
|
message: /^Expected call 2 of mock function to have been called with:/,
|
|
actual: [{ a: 2 }],
|
|
expected: [{ a: 3 }],
|
|
});
|
|
t.assert.throws(() => t.assert.nthCalledWith(fn, 3, 1), {
|
|
code: 'ERR_ASSERTION',
|
|
message: 'Expected mock function to have been called at least 3 times, but it was called 2 times',
|
|
});
|
|
t.assert.throws(() => t.assert.nthCalledWith(fn, 0, 1), {
|
|
code: 'ERR_OUT_OF_RANGE',
|
|
});
|
|
});
|
|
|
|
test('t.assert.lastCalledWith', (t) => {
|
|
const fn = t.mock.fn();
|
|
t.assert.throws(() => t.assert.lastCalledWith(fn), {
|
|
code: 'ERR_ASSERTION',
|
|
message: 'Expected mock function to have been called',
|
|
});
|
|
fn(1);
|
|
fn(2);
|
|
t.assert.lastCalledWith(fn, 2);
|
|
t.assert.throws(() => t.assert.lastCalledWith(fn, 1), {
|
|
code: 'ERR_ASSERTION',
|
|
actual: [2],
|
|
expected: [1],
|
|
});
|
|
});
|
|
|
|
test('mock matchers count towards t.plan()', (t) => {
|
|
t.plan(4);
|
|
const fn = t.mock.fn();
|
|
fn(1);
|
|
t.assert.called(fn);
|
|
t.assert.callCount(fn, 1);
|
|
t.assert.nthCalledWith(fn, 1, 1);
|
|
t.assert.lastCalledWith(fn, 1);
|
|
});
|