diff --git a/doc/api/assert.md b/doc/api/assert.md index 4958e6dd7df474..9aafaaebf0ab7b 100644 --- a/doc/api/assert.md +++ b/doc/api/assert.md @@ -322,6 +322,47 @@ function func() {} const callsfunc = tracker.calls(func); ``` +### `tracker.getCalls(fn)` + + + +* `fn` {Function}. + +* Returns: {Array} with all the calls to a tracked function. + +* Object {Object} + * `thisArg` {Object} + * `arguments` {Array} the arguments passed to the tracked function + +```mjs +import assert from 'node:assert'; + +const tracker = new assert.CallTracker(); + +function func() {} +const callsfunc = tracker.calls(func); +callsfunc(1, 2, 3); + +assert.deepStrictEqual(tracker.getCalls(callsfunc), + [{ thisArg: this, arguments: [1, 2, 3 ] }]); +``` + +```cjs +const assert = require('node:assert'); + +// Creates call tracker. +const tracker = new assert.CallTracker(); + +function func() {} +const callsfunc = tracker.calls(func); +callsfunc(1, 2, 3); + +assert.deepStrictEqual(tracker.getCalls(callsfunc), + [{ thisArg: this, arguments: [1, 2, 3 ] }]); +``` + ### `tracker.report()` + +* `fn` {Function} a tracked function to reset. + +reset calls of the call tracker. +if a tracked function is passed as an argument, the calls will be reset for it. +if no arguments are passed, all tracked functions will be reset + +```mjs +import assert from 'node:assert'; + +const tracker = new assert.CallTracker(); + +function func() {} +const callsfunc = tracker.calls(func); + +callsfunc(); +// Tracker was called once +tracker.getCalls(callsfunc).length === 1; + +tracker.reset(callsfunc); +tracker.getCalls(callsfunc).length === 0; +``` + +```cjs +const assert = require('node:assert'); + +function func() {} +const callsfunc = tracker.calls(func); + +callsfunc(); +// Tracker was called once +tracker.getCalls(callsfunc).length === 1; + +tracker.reset(callsfunc); +tracker.getCalls(callsfunc).length === 0; +``` + ### `tracker.verify()`