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