Skip to content

Commit a14d9bb

Browse files
committed
assert: change callTracker.calls signature use an object param
it changes the callTracker API, its docs and tests that use this module Refs: #43161
1 parent c4781ea commit a14d9bb

File tree

6 files changed

+85
-38
lines changed

6 files changed

+85
-38
lines changed

doc/api/assert.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ const tracker = new assert.CallTracker();
249249
function func() {}
250250

251251
// callsfunc() must be called exactly 1 time before tracker.verify().
252-
const callsfunc = tracker.calls(func, 1);
252+
const callsfunc = tracker.calls({ fn: func, exact: 1 });
253253

254254
callsfunc();
255255

@@ -268,7 +268,7 @@ const tracker = new assert.CallTracker();
268268
function func() {}
269269

270270
// callsfunc() must be called exactly 1 time before tracker.verify().
271-
const callsfunc = tracker.calls(func, 1);
271+
const callsfunc = tracker.calls({ fn: func, exact: 1 });
272272

273273
callsfunc();
274274

@@ -279,16 +279,17 @@ process.on('exit', () => {
279279
});
280280
```
281281

282-
### `tracker.calls([fn][, exact])`
282+
### `tracker.calls(options)`
283283

284284
<!-- YAML
285285
added:
286286
- v14.2.0
287287
- v12.19.0
288288
-->
289289

290-
* `fn` {Function} **Default:** A no-op function.
291-
* `exact` {number} **Default:** `1`.
290+
* `options` {Object}
291+
* `fn` {Function} **Default:** A no-op function.
292+
* `exact` {number} **Default:** `1`.
292293
* Returns: {Function} that wraps `fn`.
293294

294295
The wrapper function is expected to be called exactly `exact` times. If the
@@ -306,7 +307,7 @@ function func() {}
306307

307308
// Returns a function that wraps func() that must be called exact times
308309
// before tracker.verify().
309-
const callsfunc = tracker.calls(func);
310+
const callsfunc = tracker.calls({ fn: func });
310311
```
311312

312313
```cjs
@@ -319,7 +320,7 @@ function func() {}
319320

320321
// Returns a function that wraps func() that must be called exact times
321322
// before tracker.verify().
322-
const callsfunc = tracker.calls(func);
323+
const callsfunc = tracker.calls({ fn: func });
323324
```
324325

325326
### `tracker.report()`
@@ -355,7 +356,7 @@ function foo() {}
355356

356357
// Returns a function that wraps func() that must be called exact times
357358
// before tracker.verify().
358-
const callsfunc = tracker.calls(func, 2);
359+
const callsfunc = tracker.calls({ fn: func, exact: 2 });
359360

360361
// Returns an array containing information on callsfunc()
361362
tracker.report();
@@ -383,7 +384,7 @@ function foo() {}
383384

384385
// Returns a function that wraps func() that must be called exact times
385386
// before tracker.verify().
386-
const callsfunc = tracker.calls(func, 2);
387+
const callsfunc = tracker.calls({ fn: func, exact: 2 });
387388

388389
// Returns an array containing information on callsfunc()
389390
tracker.report();
@@ -421,7 +422,7 @@ function func() {}
421422

422423
// Returns a function that wraps func() that must be called exact times
423424
// before tracker.verify().
424-
const callsfunc = tracker.calls(func, 2);
425+
const callsfunc = tracker.calls({ fn: func, exact: 2 });
425426

426427
callsfunc();
427428

@@ -439,7 +440,7 @@ function func() {}
439440

440441
// Returns a function that wraps func() that must be called exact times
441442
// before tracker.verify().
442-
const callsfunc = tracker.calls(func, 2);
443+
const callsfunc = tracker.calls({ fn: func, exact: 2 });
443444

444445
callsfunc();
445446

@@ -2475,7 +2476,7 @@ argument.
24752476
[`assert.throws()`]: #assertthrowsfn-error-message
24762477
[`getColorDepth()`]: tty.md#writestreamgetcolordepthenv
24772478
[`process.on('exit')`]: process.md#event-exit
2478-
[`tracker.calls()`]: #trackercallsfn-exact
2479+
[`tracker.calls()`]: #trackercallsoptions
24792480
[`tracker.verify()`]: #trackerverify
24802481
[enumerable "own" properties]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties
24812482
[prototype-spec]: https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots

lib/internal/assert/calltracker.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,9 @@ class CallTracker {
2424

2525
#callChecks = new SafeSet();
2626

27-
calls(fn, exact = 1) {
27+
calls({ fn = noop, exact = 1 } = { fn: noop, exact: 1 }) {
2828
if (process._exiting)
2929
throw new ERR_UNAVAILABLE_DURING_EXIT();
30-
if (typeof fn === 'number') {
31-
exact = fn;
32-
fn = noop;
33-
} else if (fn === undefined) {
34-
fn = noop;
35-
}
3630

3731
validateUint32(exact, 'exact', true);
3832

test/parallel/test-assert-calltracker-calls.js

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,57 @@ const err = {
1414

1515
// Ensures calls() throws on invalid input types.
1616
assert.throws(() => {
17-
const callsbar = tracker.calls(bar, '1');
17+
const callsbar = tracker.calls({
18+
fn: bar,
19+
exact: '1'
20+
});
1821
callsbar();
1922
}, err
2023
);
2124

2225
assert.throws(() => {
23-
const callsbar = tracker.calls(bar, 0.1);
26+
const callsbar = tracker.calls({
27+
fn: bar,
28+
exact: 0.1
29+
});
2430
callsbar();
2531
}, { code: 'ERR_OUT_OF_RANGE' }
2632
);
2733

2834
assert.throws(() => {
29-
const callsbar = tracker.calls(bar, true);
35+
const callsbar = tracker.calls({
36+
fn: bar,
37+
exact: true
38+
});
3039
callsbar();
3140
}, err
3241
);
3342

3443
assert.throws(() => {
35-
const callsbar = tracker.calls(bar, () => {});
44+
const callsbar = tracker.calls({
45+
fn: bar,
46+
exact: () => {}
47+
});
3648
callsbar();
3749
}, err
3850
);
3951

4052
assert.throws(() => {
41-
const callsbar = tracker.calls(bar, null);
53+
const callsbar = tracker.calls({
54+
fn: bar,
55+
exact: null
56+
});
4257
callsbar();
4358
}, err
4459
);
4560

4661
// Expects an error as tracker.calls() cannot be called within a process exit
4762
// handler.
4863
process.on('exit', () => {
49-
assert.throws(() => tracker.calls(bar, 1), {
64+
assert.throws(() => tracker.calls({
65+
fn: bar,
66+
exact: 1
67+
}), {
5068
code: 'ERR_UNAVAILABLE_DURING_EXIT',
5169
});
5270
});
@@ -57,7 +75,10 @@ function func() {
5775
throw new Error(msg);
5876
}
5977

60-
const callsfunc = tracker.calls(func, 1);
78+
const callsfunc = tracker.calls({
79+
fn: func,
80+
exact: 1
81+
});
6182

6283
// Expects callsfunc() to call func() which throws an error.
6384
assert.throws(
@@ -67,14 +88,35 @@ assert.throws(
6788

6889
{
6990
const tracker = new assert.CallTracker();
70-
const callsNoop = tracker.calls(1);
91+
const callsNoop = tracker.calls({
92+
exact: 1
93+
});
94+
callsNoop();
95+
tracker.verify();
96+
}
97+
98+
{
99+
const tracker = new assert.CallTracker();
100+
const callsNoop = tracker.calls({
101+
fn: bar
102+
});
103+
callsNoop('abc');
104+
tracker.verify();
105+
}
106+
107+
{
108+
const tracker = new assert.CallTracker();
109+
const callsNoop = tracker.calls({
110+
fn: undefined,
111+
exact: 1
112+
});
71113
callsNoop();
72114
tracker.verify();
73115
}
74116

75117
{
76118
const tracker = new assert.CallTracker();
77-
const callsNoop = tracker.calls(undefined, 1);
119+
const callsNoop = tracker.calls();
78120
callsNoop();
79121
tracker.verify();
80122
}

test/parallel/test-assert-calltracker-report.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ const tracker = new assert.CallTracker();
88

99
function foo() {}
1010

11-
const callsfoo = tracker.calls(foo, 1);
11+
const callsfoo = tracker.calls({
12+
fn: foo,
13+
exact: 1
14+
});
1215

1316
// Ensures that foo was added to the callChecks array.
1417
assert.strictEqual(tracker.report()[0].operator, 'foo');

test/parallel/test-assert-calltracker-verify.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ const msg = 'Function(s) were not called the expected number of times';
1010

1111
function foo() {}
1212

13-
const callsfoo = tracker.calls(foo, 1);
13+
const callsfoo = tracker.calls({
14+
fn: foo,
15+
exact: 1
16+
});
1417

1518
// Expects an error as callsfoo() was called less than one time.
1619
assert.throws(

test/parallel/test-whatwg-webstreams-transfer.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -393,14 +393,18 @@ const theData = 'hello';
393393
tracker.verify();
394394
});
395395
396-
parentPort.onmessage = tracker.calls(({ data }) => {
397-
assert(isReadableStream(data));
398-
const reader = data.getReader();
399-
reader.read().then(tracker.calls((result) => {
400-
assert(!result.done);
401-
assert(result.value instanceof Uint8Array);
402-
}));
403-
parentPort.close();
396+
parentPort.onmessage = tracker.calls({
397+
fn: ({ data }) => {
398+
assert(isReadableStream(data));
399+
const reader = data.getReader();
400+
reader.read().then(tracker.calls({
401+
fn: (result) => {
402+
assert(!result.done);
403+
assert(result.value instanceof Uint8Array);
404+
}
405+
}));
406+
parentPort.close();
407+
}
404408
});
405409
parentPort.onmessageerror = () => assert.fail('should not be called');
406410
`, { eval: true });

0 commit comments

Comments
 (0)