GeezerVison is showing github.com — test_runner: add mock call assertions to t.assert · nodejs/node@d9ca5c8
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/test.md +87Lines changed: 87 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -4332,6 +4332,46 @@ test('test', (t) => { | 4332 | 4332 | }); | 4333 | 4333 | ``` | 4334 | 4334 |
| | 4335 | +#### `context.assert.callCount(fn, times[, message])` | | 4336 | + | | 4337 | +<!-- YAML | | 4338 | +added: REPLACEME | | 4339 | +--> | | 4340 | + | | 4341 | +* `fn` {Function} A mock function created by the test runner's mocking API. | | 4342 | +* `times` {integer} The expected number of calls. | | 4343 | +* `message` {string} Optional error message. | | 4344 | + | | 4345 | +Asserts that the mock function `fn` has been called exactly `times` times. | | 4346 | + | | 4347 | +```js | | 4348 | +test('mock was called twice', (t) => { | | 4349 | + const fn = t.mock.fn(); | | 4350 | + fn(); | | 4351 | + fn(); | | 4352 | + t.assert.callCount(fn, 2); | | 4353 | +}); | | 4354 | +``` | | 4355 | + | | 4356 | +#### `context.assert.called(fn[, message])` | | 4357 | + | | 4358 | +<!-- YAML | | 4359 | +added: REPLACEME | | 4360 | +--> | | 4361 | + | | 4362 | +* `fn` {Function} A mock function created by the test runner's mocking API. | | 4363 | +* `message` {string} Optional error message. | | 4364 | + | | 4365 | +Asserts that the mock function `fn` has been called at least once. | | 4366 | + | | 4367 | +```js | | 4368 | +test('mock was called', (t) => { | | 4369 | + const fn = t.mock.fn(); | | 4370 | + fn(); | | 4371 | + t.assert.called(fn); | | 4372 | +}); | | 4373 | +``` | | 4374 | + | 4335 | 4375 | #### `context.assert.fileSnapshot(value, path[, options])` | 4336 | 4376 |
| 4337 | 4377 | <!-- YAML | @@ -4371,6 +4411,52 @@ This function differs from `context.assert.snapshot()` in the following ways: | 4371 | 4411 | These differences allow snapshot files to better support features such as syntax | 4372 | 4412 | highlighting. | 4373 | 4413 |
| | 4414 | +#### `context.assert.lastCalledWith(fn[, ...args])` | | 4415 | + | | 4416 | +<!-- YAML | | 4417 | +added: REPLACEME | | 4418 | +--> | | 4419 | + | | 4420 | +* `fn` {Function} A mock function created by the test runner's mocking API. | | 4421 | +* `...args` {any} The expected arguments. | | 4422 | + | | 4423 | +Asserts that the most recent call to the mock function `fn` received arguments | | 4424 | +deeply and strictly equal to `args`, using the same comparison as | | 4425 | +[`assert.deepStrictEqual()`][]. | | 4426 | + | | 4427 | +```js | | 4428 | +test('mock was last called with arguments', (t) => { | | 4429 | + const fn = t.mock.fn(); | | 4430 | + fn(1); | | 4431 | + fn(2); | | 4432 | + t.assert.lastCalledWith(fn, 2); | | 4433 | +}); | | 4434 | +``` | | 4435 | + | | 4436 | +#### `context.assert.nthCalledWith(fn, n[, ...args])` | | 4437 | + | | 4438 | +<!-- YAML | | 4439 | +added: REPLACEME | | 4440 | +--> | | 4441 | + | | 4442 | +* `fn` {Function} A mock function created by the test runner's mocking API. | | 4443 | +* `n` {integer} The 1-based index of the call to check. | | 4444 | +* `...args` {any} The expected arguments. | | 4445 | + | | 4446 | +Asserts that the `n`th call to the mock function `fn` received arguments | | 4447 | +deeply and strictly equal to `args`, using the same comparison as | | 4448 | +[`assert.deepStrictEqual()`][]. `n` starts at `1`, so | | 4449 | +`t.assert.nthCalledWith(fn, 1)` checks `fn.mock.calls[0]`. | | 4450 | + | | 4451 | +```js | | 4452 | +test('mock was called with arguments on the second call', (t) => { | | 4453 | + const fn = t.mock.fn(); | | 4454 | + fn(1); | | 4455 | + fn(2); | | 4456 | + t.assert.nthCalledWith(fn, 2, 2); | | 4457 | +}); | | 4458 | +``` | | 4459 | + | 4374 | 4460 | #### `context.assert.snapshot(value[, options])` | 4375 | 4461 |
| 4376 | 4462 | <!-- YAML | @@ -4993,6 +5079,7 @@ test.describe('my suite', (suite) => { | 4993 | 5079 | [`SuiteContext`]: #class-suitecontext | 4994 | 5080 | [`TestContext`]: #class-testcontext | 4995 | 5081 | [`TracingChannel`]: diagnostics_channel.md#class-tracingchannel | | 5082 | +[`assert.deepStrictEqual()`]: assert.md#assertdeepstrictequalactual-expected-message | 4996 | 5083 | [`assert.throws`]: assert.md#assertthrowsfn-error-message | 4997 | 5084 | [`context.diagnostic`]: #contextdiagnosticmessage | 4998 | 5085 | [`context.log`]: #contextlogmessage-data | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/test_runner/assert.js +105Lines changed: 105 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 | const { | 3 | 3 | SafeMap, | 4 | 4 | } = primordials; | | 5 | +const { | | 6 | + codes: { | | 7 | + ERR_INVALID_ARG_TYPE, | | 8 | + }, | | 9 | +} = require('internal/errors'); | 5 | 10 | const { | 6 | 11 | validateFunction, | | 12 | + validateInteger, | 7 | 13 | validateString, | 8 | 14 | } = require('internal/validators'); | | 15 | +const { isDeepStrictEqual } = require('internal/util/comparisons'); | 9 | 16 | const assert = require('assert'); | | 17 | +const { AssertionError } = assert; | 10 | 18 | const methodsToCopy = [ | 11 | 19 | 'deepEqual', | 12 | 20 | 'deepStrictEqual', | @@ -28,13 +36,110 @@ const methodsToCopy = [ | 28 | 36 | ]; | 29 | 37 | let assertMap; | 30 | 38 |
| | 39 | +function getMockContext(fn) { | | 40 | + const { MockFunctionContext } = require('internal/test_runner/mock/mock'); | | 41 | + const ctx = fn?.mock; | | 42 | + | | 43 | + if (!(ctx instanceof MockFunctionContext)) { | | 44 | + throw new ERR_INVALID_ARG_TYPE('fn', 'mock function', fn); | | 45 | + } | | 46 | + | | 47 | + return ctx; | | 48 | +} | | 49 | + | | 50 | +function called(fn, message) { | | 51 | + const count = getMockContext(fn).callCount(); | | 52 | + | | 53 | + if (count === 0) { | | 54 | + throw new AssertionError({ | | 55 | + __proto__: null, | | 56 | + message: message ?? 'Expected mock function to have been called', | | 57 | + actual: count, | | 58 | + operator: 'called', | | 59 | + stackStartFn: called, | | 60 | + }); | | 61 | + } | | 62 | +} | | 63 | + | | 64 | +function callCount(fn, times, message) { | | 65 | + validateInteger(times, 'times', 0); | | 66 | + const count = getMockContext(fn).callCount(); | | 67 | + | | 68 | + if (count !== times) { | | 69 | + throw new AssertionError({ | | 70 | + __proto__: null, | | 71 | + message: message ?? | | 72 | + `Expected mock function to have been called ${times} times, but it was called ${count} times`, | | 73 | + actual: count, | | 74 | + expected: times, | | 75 | + operator: 'callCount', | | 76 | + stackStartFn: callCount, | | 77 | + }); | | 78 | + } | | 79 | +} | | 80 | + | | 81 | +function assertCalledWithAt(fn, n, args, stackStartFn) { | | 82 | + const calls = getMockContext(fn).calls; | | 83 | + | | 84 | + if (n > calls.length) { | | 85 | + throw new AssertionError({ | | 86 | + __proto__: null, | | 87 | + message: `Expected mock function to have been called at least ${n} times, ` + | | 88 | + `but it was called ${calls.length} times`, | | 89 | + actual: calls.length, | | 90 | + expected: n, | | 91 | + operator: stackStartFn.name, | | 92 | + stackStartFn, | | 93 | + }); | | 94 | + } | | 95 | + | | 96 | + const actual = calls[n - 1].arguments; | | 97 | + | | 98 | + if (!isDeepStrictEqual(actual, args)) { | | 99 | + throw new AssertionError({ | | 100 | + __proto__: null, | | 101 | + message: `Expected call ${n} of mock function to have been called with:`, | | 102 | + actual, | | 103 | + expected: args, | | 104 | + operator: 'deepStrictEqual', | | 105 | + stackStartFn, | | 106 | + }); | | 107 | + } | | 108 | +} | | 109 | + | | 110 | +function nthCalledWith(fn, n, ...args) { | | 111 | + validateInteger(n, 'n', 1); | | 112 | + assertCalledWithAt(fn, n, args, nthCalledWith); | | 113 | +} | | 114 | + | | 115 | +function lastCalledWith(fn, ...args) { | | 116 | + const count = getMockContext(fn).callCount(); | | 117 | + | | 118 | + if (count === 0) { | | 119 | + throw new AssertionError({ | | 120 | + __proto__: null, | | 121 | + message: 'Expected mock function to have been called', | | 122 | + actual: count, | | 123 | + operator: 'lastCalledWith', | | 124 | + stackStartFn: lastCalledWith, | | 125 | + }); | | 126 | + } | | 127 | + | | 128 | + assertCalledWithAt(fn, count, args, lastCalledWith); | | 129 | +} | | 130 | + | 31 | 131 | function getAssertionMap() { | 32 | 132 | if (assertMap === undefined) { | 33 | 133 | assertMap = new SafeMap(); | 34 | 134 |
| 35 | 135 | for (let i = 0; i < methodsToCopy.length; i++) { | 36 | 136 | assertMap.set(methodsToCopy[i], assert[methodsToCopy[i]]); | 37 | 137 | } | | 138 | + | | 139 | + assertMap.set('called', called); | | 140 | + assertMap.set('callCount', callCount); | | 141 | + assertMap.set('lastCalledWith', lastCalledWith); | | 142 | + assertMap.set('nthCalledWith', nthCalledWith); | 38 | 143 | } | 39 | 144 |
| 40 | 145 | return assertMap; | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/test_runner/mock/mock.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1118,5 +1118,6 @@ if (!enableModuleMocking) { | 1118 | 1118 |
| 1119 | 1119 | module.exports = { | 1120 | 1120 | ensureNodeScheme, | | 1121 | + MockFunctionContext, | 1121 | 1122 | MockTracker, | 1122 | 1123 | }; |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-runner-assert.js +83-1Lines changed: 83 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,10 +12,92 @@ test('expected methods are on t.assert', (t) => { | 12 | 12 | 'options', | 13 | 13 | ]; | 14 | 14 | const assertKeys = Object.keys(assert).filter((key) => !uncopiedKeys.includes(key)); | 15 | | - const expectedKeys = ['snapshot', 'fileSnapshot'].concat(assertKeys).sort(); | | 15 | + const expectedKeys = [ | | 16 | + 'snapshot', | | 17 | + 'fileSnapshot', | | 18 | + 'called', | | 19 | + 'callCount', | | 20 | + 'lastCalledWith', | | 21 | + 'nthCalledWith', | | 22 | + ].concat(assertKeys).sort(); | 16 | 23 | assert.deepStrictEqual(Object.keys(t.assert).sort(), expectedKeys); | 17 | 24 | }); | 18 | 25 |
| 19 | 26 | test('t.assert.ok correctly parses the stacktrace', (t) => { | 20 | 27 | t.assert.throws(() => t.assert.ok(1 === 2), /t\.assert\.ok\(1 === 2\)/); | 21 | 28 | }); | | 29 | + | | 30 | +test('t.assert.called', (t) => { | | 31 | + const fn = t.mock.fn(); | | 32 | + t.assert.throws(() => t.assert.called(fn), { | | 33 | + code: 'ERR_ASSERTION', | | 34 | + message: 'Expected mock function to have been called', | | 35 | + }); | | 36 | + fn(); | | 37 | + t.assert.called(fn); | | 38 | + t.assert.throws(() => t.assert.called(() => {}), { | | 39 | + code: 'ERR_INVALID_ARG_TYPE', | | 40 | + }); | | 41 | +}); | | 42 | + | | 43 | +test('t.assert.callCount', (t) => { | | 44 | + const fn = t.mock.fn(); | | 45 | + t.assert.callCount(fn, 0); | | 46 | + fn(); | | 47 | + fn(); | | 48 | + t.assert.callCount(fn, 2); | | 49 | + t.assert.throws(() => t.assert.callCount(fn, 1), { | | 50 | + code: 'ERR_ASSERTION', | | 51 | + message: 'Expected mock function to have been called 1 times, but it was called 2 times', | | 52 | + }); | | 53 | + t.assert.throws(() => t.assert.callCount(fn, -1), { | | 54 | + code: 'ERR_OUT_OF_RANGE', | | 55 | + }); | | 56 | +}); | | 57 | + | | 58 | +test('t.assert.nthCalledWith', (t) => { | | 59 | + const fn = t.mock.fn(); | | 60 | + fn(1); | | 61 | + fn({ a: 2 }); | | 62 | + t.assert.nthCalledWith(fn, 1, 1); | | 63 | + t.assert.nthCalledWith(fn, 2, { a: 2 }); | | 64 | + t.assert.throws(() => t.assert.nthCalledWith(fn, 2, { a: 3 }), { | | 65 | + code: 'ERR_ASSERTION', | | 66 | + message: /^Expected call 2 of mock function to have been called with:/, | | 67 | + actual: [{ a: 2 }], | | 68 | + expected: [{ a: 3 }], | | 69 | + }); | | 70 | + t.assert.throws(() => t.assert.nthCalledWith(fn, 3, 1), { | | 71 | + code: 'ERR_ASSERTION', | | 72 | + message: 'Expected mock function to have been called at least 3 times, but it was called 2 times', | | 73 | + }); | | 74 | + t.assert.throws(() => t.assert.nthCalledWith(fn, 0, 1), { | | 75 | + code: 'ERR_OUT_OF_RANGE', | | 76 | + }); | | 77 | +}); | | 78 | + | | 79 | +test('t.assert.lastCalledWith', (t) => { | | 80 | + const fn = t.mock.fn(); | | 81 | + t.assert.throws(() => t.assert.lastCalledWith(fn), { | | 82 | + code: 'ERR_ASSERTION', | | 83 | + message: 'Expected mock function to have been called', | | 84 | + }); | | 85 | + fn(1); | | 86 | + fn(2); | | 87 | + t.assert.lastCalledWith(fn, 2); | | 88 | + t.assert.throws(() => t.assert.lastCalledWith(fn, 1), { | | 89 | + code: 'ERR_ASSERTION', | | 90 | + actual: [2], | | 91 | + expected: [1], | | 92 | + }); | | 93 | +}); | | 94 | + | | 95 | +test('mock matchers count towards t.plan()', (t) => { | | 96 | + t.plan(4); | | 97 | + const fn = t.mock.fn(); | | 98 | + fn(1); | | 99 | + t.assert.called(fn); | | 100 | + t.assert.callCount(fn, 1); | | 101 | + t.assert.nthCalledWith(fn, 1, 1); | | 102 | + t.assert.lastCalledWith(fn, 1); | | 103 | +}); |
|