From c87ed21fdf6e393e6f7601bd867f61e529cc058d Mon Sep 17 00:00:00 2001 From: ConorDavenport Date: Thu, 27 Feb 2020 11:41:05 +0000 Subject: [PATCH] assert: port common.mustCall() to assert Fixes: https://github.com/nodejs/node/issues/31392 PR-URL: https://github.com/nodejs/node/pull/31982 Reviewed-By: James M Snell Reviewed-By: Matteo Collina Reviewed-By: Zeyu Yang Reviewed-By: Colin Ihrig Reviewed-By: Denys Otrishko --- doc/api/assert.md | 135 ++++++++++++++++++ doc/api/errors.md | 7 + lib/assert.js | 3 + lib/internal/assert/assertion_error.js | 20 ++- lib/internal/assert/calltracker.js | 93 ++++++++++++ lib/internal/errors.js | 2 + node.gyp | 1 + .../parallel/test-assert-calltracker-calls.js | 66 +++++++++ .../test-assert-calltracker-report.js | 32 +++++ .../test-assert-calltracker-verify.js | 32 +++++ 10 files changed, 388 insertions(+), 3 deletions(-) create mode 100644 lib/internal/assert/calltracker.js create mode 100644 test/parallel/test-assert-calltracker-calls.js create mode 100644 test/parallel/test-assert-calltracker-report.js create mode 100644 test/parallel/test-assert-calltracker-verify.js diff --git a/doc/api/assert.md b/doc/api/assert.md index 9731ddaa13f315..f8178ed6ec1249 100644 --- a/doc/api/assert.md +++ b/doc/api/assert.md @@ -149,6 +149,137 @@ try { } ``` +## Class: `assert.CallTracker` + +### `new assert.CallTracker()` + + +Creates a new [`CallTracker`][] object which can be used to track if functions +were called a specific number of times. The `tracker.verify()` must be called +for the verification to take place. The usual pattern would be to call it in a +[`process.on('exit')`][] handler. + +```js +const assert = require('assert'); + +const tracker = new assert.CallTracker(); + +function func() {} + +// callsfunc() must be called exactly 1 time before tracker.verify(). +const callsfunc = tracker.calls(func, 1); + +callsfunc(); + +// Calls tracker.verify() and verifies if all tracker.calls() functions have +// been called exact times. +process.on('exit', () => { + tracker.verify(); +}); +``` + +### `tracker.calls([fn][, exact])` + + +* `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 +function has not been called exactly `exact` times when +[`tracker.verify()`][] is called, then [`tracker.verify()`][] will throw an +error. + +```js +const assert = require('assert'); + +// Creates call tracker. +const tracker = new assert.CallTracker(); + +function func() {} + +// Returns a function that wraps func() that must be called exact times +// before tracker.verify(). +const callsfunc = tracker.calls(func); +``` + +### `tracker.report()` + + +* Returns: {Array} of objects containing information about the wrapper functions +returned by [`tracker.calls()`][]. +* Object {Object} + * `message` {string} + * `actual` {number} The actual number of times the function was called. + * `expected` {number} The number of times the function was expected to be + called. + * `operator` {string} The name of the function that is wrapped. + * `stack` {Object} A stack trace of the function. + +The arrays contains information about the expected and actual number of calls of +the functions that have not been called the expected number of times. + +```js +const assert = require('assert'); + +// Creates call tracker. +const tracker = new assert.CallTracker(); + +function func() {} + +function foo() {} + +// Returns a function that wraps func() that must be called exact times +// before tracker.verify(). +const callsfunc = tracker.calls(func, 2); + +// Returns an array containing information on callsfunc() +tracker.report(); +// [ +// { +// message: 'Expected the func function to be executed 2 time(s) but was +// executed 0 time(s).', +// actual: 0, +// expected: 2, +// operator: 'func', +// stack: stack trace +// } +// ] +``` + +### `tracker.verify()` + + +Iterates through the list of functions passed to +[`tracker.calls()`][] and will throw an error for functions that +have not been called the expected number of times. + +```js +const assert = require('assert'); + +// Creates call tracker. +const tracker = new assert.CallTracker(); + +function func() {} + +// Returns a function that wraps func() that must be called exact times +// before tracker.verify(). +const callsfunc = tracker.calls(func, 2); + +callsfunc(); + +// Will throw an error since callsfunc() was only called once. +tracker.verify(); +``` + ## `assert(value[, message])`