diff --git a/doc/api/assert.md b/doc/api/assert.md index cc71195985791c..930d4974b31754 100644 --- a/doc/api/assert.md +++ b/doc/api/assert.md @@ -249,7 +249,7 @@ const tracker = new assert.CallTracker(); function func() {} // callsfunc() must be called exactly 1 time before tracker.verify(). -const callsfunc = tracker.calls(func, 1); +const callsfunc = tracker.calls({ fn: func, exact: 1 }); callsfunc(); @@ -268,7 +268,7 @@ const tracker = new assert.CallTracker(); function func() {} // callsfunc() must be called exactly 1 time before tracker.verify(). -const callsfunc = tracker.calls(func, 1); +const callsfunc = tracker.calls({ fn: func, exact: 1 }); callsfunc(); @@ -279,7 +279,7 @@ process.on('exit', () => { }); ``` -### `tracker.calls([fn][, exact])` +### `tracker.calls(options)` -* `fn` {Function} **Default:** A no-op function. -* `exact` {number} **Default:** `1`. +* `options` {Object} + * `fn` {Function} **Default:** A no-op function. + * `exact` {number} **Default:** `1`. * Returns: {Function} that wraps `fn`. The wrapper function is expected to be called exactly `exact` times. If the @@ -306,7 +307,7 @@ function func() {} // Returns a function that wraps func() that must be called exact times // before tracker.verify(). -const callsfunc = tracker.calls(func); +const callsfunc = tracker.calls({ fn: func }); ``` ```cjs @@ -319,7 +320,7 @@ function func() {} // Returns a function that wraps func() that must be called exact times // before tracker.verify(). -const callsfunc = tracker.calls(func); +const callsfunc = tracker.calls({ fn: func }); ``` ### `tracker.report()` @@ -355,7 +356,7 @@ function foo() {} // Returns a function that wraps func() that must be called exact times // before tracker.verify(). -const callsfunc = tracker.calls(func, 2); +const callsfunc = tracker.calls({ fn: func, exact: 2 }); // Returns an array containing information on callsfunc() tracker.report(); @@ -383,7 +384,7 @@ function foo() {} // Returns a function that wraps func() that must be called exact times // before tracker.verify(). -const callsfunc = tracker.calls(func, 2); +const callsfunc = tracker.calls({ fn: func, exact: 2 }); // Returns an array containing information on callsfunc() tracker.report(); @@ -421,7 +422,7 @@ function func() {} // Returns a function that wraps func() that must be called exact times // before tracker.verify(). -const callsfunc = tracker.calls(func, 2); +const callsfunc = tracker.calls({ fn: func, exact: 2 }); callsfunc(); @@ -439,7 +440,7 @@ function func() {} // Returns a function that wraps func() that must be called exact times // before tracker.verify(). -const callsfunc = tracker.calls(func, 2); +const callsfunc = tracker.calls({ fn: func, exact: 2 }); callsfunc(); @@ -2475,7 +2476,7 @@ argument. [`assert.throws()`]: #assertthrowsfn-error-message [`getColorDepth()`]: tty.md#writestreamgetcolordepthenv [`process.on('exit')`]: process.md#event-exit -[`tracker.calls()`]: #trackercallsfn-exact +[`tracker.calls()`]: #trackercallsoptions [`tracker.verify()`]: #trackerverify [enumerable "own" properties]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties [prototype-spec]: https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots diff --git a/lib/internal/assert/calltracker.js b/lib/internal/assert/calltracker.js index 0fbdf70e5d825c..d9077073331447 100644 --- a/lib/internal/assert/calltracker.js +++ b/lib/internal/assert/calltracker.js @@ -24,15 +24,9 @@ class CallTracker { #callChecks = new SafeSet(); - calls(fn, exact = 1) { + calls({ fn = noop, exact = 1 } = { fn: noop, exact: 1 }) { if (process._exiting) throw new ERR_UNAVAILABLE_DURING_EXIT(); - if (typeof fn === 'number') { - exact = fn; - fn = noop; - } else if (fn === undefined) { - fn = noop; - } validateUint32(exact, 'exact', true); diff --git a/test/parallel/test-assert-calltracker-calls.js b/test/parallel/test-assert-calltracker-calls.js index 99db4ee284be81..24319acde81e78 100644 --- a/test/parallel/test-assert-calltracker-calls.js +++ b/test/parallel/test-assert-calltracker-calls.js @@ -14,31 +14,46 @@ const err = { // Ensures calls() throws on invalid input types. assert.throws(() => { - const callsbar = tracker.calls(bar, '1'); + const callsbar = tracker.calls({ + fn: bar, + exact: '1' + }); callsbar(); }, err ); assert.throws(() => { - const callsbar = tracker.calls(bar, 0.1); + const callsbar = tracker.calls({ + fn: bar, + exact: 0.1 + }); callsbar(); }, { code: 'ERR_OUT_OF_RANGE' } ); assert.throws(() => { - const callsbar = tracker.calls(bar, true); + const callsbar = tracker.calls({ + fn: bar, + exact: true + }); callsbar(); }, err ); assert.throws(() => { - const callsbar = tracker.calls(bar, () => {}); + const callsbar = tracker.calls({ + fn: bar, + exact: () => {} + }); callsbar(); }, err ); assert.throws(() => { - const callsbar = tracker.calls(bar, null); + const callsbar = tracker.calls({ + fn: bar, + exact: null + }); callsbar(); }, err ); @@ -46,7 +61,10 @@ assert.throws(() => { // Expects an error as tracker.calls() cannot be called within a process exit // handler. process.on('exit', () => { - assert.throws(() => tracker.calls(bar, 1), { + assert.throws(() => tracker.calls({ + fn: bar, + exact: 1 + }), { code: 'ERR_UNAVAILABLE_DURING_EXIT', }); }); @@ -57,7 +75,10 @@ function func() { throw new Error(msg); } -const callsfunc = tracker.calls(func, 1); +const callsfunc = tracker.calls({ + fn: func, + exact: 1 +}); // Expects callsfunc() to call func() which throws an error. assert.throws( @@ -67,14 +88,35 @@ assert.throws( { const tracker = new assert.CallTracker(); - const callsNoop = tracker.calls(1); + const callsNoop = tracker.calls({ + exact: 1 + }); + callsNoop(); + tracker.verify(); +} + +{ + const tracker = new assert.CallTracker(); + const callsNoop = tracker.calls({ + fn: bar + }); + callsNoop('abc'); + tracker.verify(); +} + +{ + const tracker = new assert.CallTracker(); + const callsNoop = tracker.calls({ + fn: undefined, + exact: 1 + }); callsNoop(); tracker.verify(); } { const tracker = new assert.CallTracker(); - const callsNoop = tracker.calls(undefined, 1); + const callsNoop = tracker.calls(); callsNoop(); tracker.verify(); } diff --git a/test/parallel/test-assert-calltracker-report.js b/test/parallel/test-assert-calltracker-report.js index 87ef0bff17fc0b..3e37dbabce93db 100644 --- a/test/parallel/test-assert-calltracker-report.js +++ b/test/parallel/test-assert-calltracker-report.js @@ -8,7 +8,10 @@ const tracker = new assert.CallTracker(); function foo() {} -const callsfoo = tracker.calls(foo, 1); +const callsfoo = tracker.calls({ + fn: foo, + exact: 1 +}); // Ensures that foo was added to the callChecks array. assert.strictEqual(tracker.report()[0].operator, 'foo'); diff --git a/test/parallel/test-assert-calltracker-verify.js b/test/parallel/test-assert-calltracker-verify.js index 75d20bf9c49c38..8f8effddcf8938 100644 --- a/test/parallel/test-assert-calltracker-verify.js +++ b/test/parallel/test-assert-calltracker-verify.js @@ -10,7 +10,10 @@ const msg = 'Function(s) were not called the expected number of times'; function foo() {} -const callsfoo = tracker.calls(foo, 1); +const callsfoo = tracker.calls({ + fn: foo, + exact: 1 +}); // Expects an error as callsfoo() was called less than one time. assert.throws( diff --git a/test/parallel/test-whatwg-webstreams-transfer.js b/test/parallel/test-whatwg-webstreams-transfer.js index 9699dc8dc794e8..0a377dbc7b1839 100644 --- a/test/parallel/test-whatwg-webstreams-transfer.js +++ b/test/parallel/test-whatwg-webstreams-transfer.js @@ -393,14 +393,18 @@ const theData = 'hello'; tracker.verify(); }); - parentPort.onmessage = tracker.calls(({ data }) => { - assert(isReadableStream(data)); - const reader = data.getReader(); - reader.read().then(tracker.calls((result) => { - assert(!result.done); - assert(result.value instanceof Uint8Array); - })); - parentPort.close(); + parentPort.onmessage = tracker.calls({ + fn: ({ data }) => { + assert(isReadableStream(data)); + const reader = data.getReader(); + reader.read().then(tracker.calls({ + fn: (result) => { + assert(!result.done); + assert(result.value instanceof Uint8Array); + } + })); + parentPort.close(); + } }); parentPort.onmessageerror = () => assert.fail('should not be called'); `, { eval: true });