@@ -147,6 +147,137 @@ try {
147
147
}
148
148
```
149
149
150
+ ## Class: ` assert.CallTracker `
151
+
152
+ ### ` new assert.CallTracker() `
153
+ <!-- YAML
154
+ added: REPLACEME
155
+ -->
156
+
157
+ Creates a new [ ` CallTracker ` ] [ ] object which can be used to track if functions
158
+ were called a specific number of times. The ` tracker.verify() ` must be called
159
+ for the verification to take place. The usual pattern would be to call it in a
160
+ [ ` process.on('exit') ` ] [ ] handler.
161
+
162
+ ``` js
163
+ const assert = require (' assert' );
164
+
165
+ const tracker = new assert.CallTracker ();
166
+
167
+ function func () {}
168
+
169
+ // callsfunc() must be called exactly 1 time before tracker.verify().
170
+ const callsfunc = tracker .calls (func, 1 );
171
+
172
+ callsfunc ();
173
+
174
+ // Calls tracker.verify() and verifies if all tracker.calls() functions have
175
+ // been called exact times.
176
+ process .on (' exit' , () => {
177
+ tracker .verify ();
178
+ });
179
+ ```
180
+
181
+ ### ` tracker.calls([fn][, exact]) `
182
+ <!-- YAML
183
+ added: REPLACEME
184
+ -->
185
+
186
+ * ` fn ` {Function} ** Default** A no-op function.
187
+ * ` exact ` {number} ** Default** ` 1 ` .
188
+ * Returns: {Function} that wraps ` fn ` .
189
+
190
+ The wrapper function is expected to be called exactly ` exact ` times. If the
191
+ function has not been called exactly ` exact ` times when
192
+ [ ` tracker.verify() ` ] [ ] is called, then [ ` tracker.verify() ` ] [ ] will throw an
193
+ error.
194
+
195
+ ``` js
196
+ const assert = require (' assert' );
197
+
198
+ // Creates call tracker.
199
+ const tracker = new assert.CallTracker ();
200
+
201
+ function func () {}
202
+
203
+ // Returns a function that wraps func() that must be called exact times
204
+ // before tracker.verify().
205
+ const callsfunc = tracker .calls (func);
206
+ ```
207
+
208
+ ### ` tracker.report() `
209
+ <!-- YAML
210
+ added: REPLACEME
211
+ -->
212
+
213
+ * Returns: {Array} of objects containing information about the wrapper functions
214
+ returned by [ ` tracker.calls() ` ] [ ] .
215
+ * Object {Object}
216
+ * ` message ` {string}
217
+ * ` actual ` {number} The actual number of times the function was called.
218
+ * ` expected ` {number} The number of times the function was expected to be
219
+ called.
220
+ * ` operator ` {string} The name of the function that is wrapped.
221
+ * ` stack ` {Object} A stack trace of the function.
222
+
223
+ The arrays contains information about the expected and actual number of calls of
224
+ the functions that have not been called the expected number of times.
225
+
226
+ ``` js
227
+ const assert = require (' assert' );
228
+
229
+ // Creates call tracker.
230
+ const tracker = new assert.CallTracker ();
231
+
232
+ function func () {}
233
+
234
+ function foo () {}
235
+
236
+ // Returns a function that wraps func() that must be called exact times
237
+ // before tracker.verify().
238
+ const callsfunc = tracker .calls (func, 2 );
239
+
240
+ // Returns an array containing information on callsfunc()
241
+ tracker .report ();
242
+ // [
243
+ // {
244
+ // message: 'Expected the func function to be executed 2 time(s) but was
245
+ // executed 0 time(s).',
246
+ // actual: 0,
247
+ // expected: 2,
248
+ // operator: 'func',
249
+ // stack: stack trace
250
+ // }
251
+ // ]
252
+ ```
253
+
254
+ ### ` tracker.verify() `
255
+ <!-- YAML
256
+ added: REPLACEME
257
+ -->
258
+
259
+ Iterates through the list of functions passed to
260
+ [ ` tracker.calls() ` ] [ ] and will throw an error for functions that
261
+ have not been called the expected number of times.
262
+
263
+ ``` js
264
+ const assert = require (' assert' );
265
+
266
+ // Creates call tracker.
267
+ const tracker = new assert.CallTracker ();
268
+
269
+ function func () {}
270
+
271
+ // Returns a function that wraps func() that must be called exact times
272
+ // before tracker.verify().
273
+ const callsfunc = tracker .calls (func, 2 );
274
+
275
+ callsfunc ();
276
+
277
+ // Will throw an error since callsfunc() was only called once.
278
+ tracker .verify ();
279
+ ```
280
+
150
281
## ` assert(value[, message]) `
151
282
<!-- YAML
152
283
added: v0.5.9
@@ -1400,6 +1531,7 @@ argument.
1400
1531
[ `TypeError` ] : errors.html#errors_class_typeerror
1401
1532
[ `WeakMap` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap
1402
1533
[ `WeakSet` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet
1534
+ [ `CallTracker` ] : #assert_class_assert_calltracker
1403
1535
[ `assert.deepEqual()` ] : #assert_assert_deepequal_actual_expected_message
1404
1536
[ `assert.deepStrictEqual()` ] : #assert_assert_deepstrictequal_actual_expected_message
1405
1537
[ `assert.doesNotThrow()` ] : #assert_assert_doesnotthrow_fn_error_message
@@ -1411,6 +1543,9 @@ argument.
1411
1543
[ `assert.ok()` ] : #assert_assert_ok_value_message
1412
1544
[ `assert.strictEqual()` ] : #assert_assert_strictequal_actual_expected_message
1413
1545
[ `assert.throws()` ] : #assert_assert_throws_fn_error_message
1546
+ [ `process.on('exit')` ] : process.html#process_event_exit
1547
+ [ `tracker.calls()` ] : #assert_class_assert_CallTracker#tracker_calls
1548
+ [ `tracker.verify()` ] : #assert_class_assert_CallTracker#tracker_verify
1414
1549
[ strict assertion mode ] : #assert_strict_assertion_mode
1415
1550
[ Abstract Equality Comparison ] : https://tc39.github.io/ecma262/#sec-abstract-equality-comparison
1416
1551
[ Object wrappers ] : https://developer.mozilla.org/en-US/docs/Glossary/Primitive#Primitive_wrapper_objects_in_JavaScript
0 commit comments