@@ -147,6 +147,136 @@ 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, an exception is thrown.
193
+
194
+ ``` js
195
+ const assert = require (' assert' );
196
+
197
+ // Creates call tracker.
198
+ const tracker = new assert.CallTracker ();
199
+
200
+ function func () {}
201
+
202
+ // Returns a function that wraps func() that must be called exact times
203
+ // before tracker.verify().
204
+ const callsfunc = tracker .calls (func);
205
+ ```
206
+
207
+ ### ` tracker.report() `
208
+ <!-- YAML
209
+ added: REPLACEME
210
+ -->
211
+
212
+ * Returns: {Array} of objects containing information about the wrapper functions
213
+ returned by [ ` tracker.calls() ` ] [ ] .
214
+ * Object {Object}
215
+ * ` message ` {string}
216
+ * ` actual ` {number} The actual number of times the function was called.
217
+ * ` expected ` {number} The number of times the function was expected to be
218
+ called.
219
+ * ` operator ` {string} The name of the function that is wrapped.
220
+ * ` stack ` {Object} A stack trace of the function.
221
+
222
+ The arrays contains information about the expected and actual number of calls of
223
+ the functions that have not been called the expected number of times.
224
+
225
+ ``` js
226
+ const assert = require (' assert' );
227
+
228
+ // Creates call tracker.
229
+ const tracker = new assert.CallTracker ();
230
+
231
+ function func () {}
232
+
233
+ function foo () {}
234
+
235
+ // Returns a function that wraps func() that must be called exact times
236
+ // before tracker.verify().
237
+ const callsfunc = tracker .calls (func, 2 );
238
+
239
+ // Returns an array containing information on callsfunc()
240
+ tracker .report ();
241
+ // [
242
+ // {
243
+ // message: 'Expected the func function to be executed 2 time(s) but was
244
+ // executed 0 time(s).',
245
+ // actual: 0,
246
+ // expected: 2,
247
+ // operator: 'func',
248
+ // stack: stack trace
249
+ // }
250
+ // ]
251
+ ```
252
+
253
+ ### ` tracker.verify() `
254
+ <!-- YAML
255
+ added: REPLACEME
256
+ -->
257
+
258
+ Iterates through the list of functions passed to
259
+ [ ` tracker.calls() ` ] [ ] and will throw an error for functions that
260
+ have not been called the expected number of times.
261
+
262
+ ``` js
263
+ const assert = require (' assert' );
264
+
265
+ // Creates call tracker.
266
+ const tracker = new assert.CallTracker ();
267
+
268
+ function func () {}
269
+
270
+ // Returns a function that wraps func() that must be called exact times
271
+ // before tracker.verify().
272
+ const callsfunc = tracker .calls (func, 2 );
273
+
274
+ callsfunc ();
275
+
276
+ // Will throw an error since callsfunc() was only called once.
277
+ tracker .verify ();
278
+ ```
279
+
150
280
## ` assert(value[, message]) `
151
281
<!-- YAML
152
282
added: v0.5.9
@@ -1423,6 +1553,7 @@ argument.
1423
1553
[ `TypeError` ] : errors.html#errors_class_typeerror
1424
1554
[ `WeakMap` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap
1425
1555
[ `WeakSet` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet
1556
+ [ `CallTracker` ] : #assert_class_assert_calltracker
1426
1557
[ `assert.deepEqual()` ] : #assert_assert_deepequal_actual_expected_message
1427
1558
[ `assert.deepStrictEqual()` ] : #assert_assert_deepstrictequal_actual_expected_message
1428
1559
[ `assert.doesNotThrow()` ] : #assert_assert_doesnotthrow_fn_error_message
@@ -1434,6 +1565,9 @@ argument.
1434
1565
[ `assert.ok()` ] : #assert_assert_ok_value_message
1435
1566
[ `assert.strictEqual()` ] : #assert_assert_strictequal_actual_expected_message
1436
1567
[ `assert.throws()` ] : #assert_assert_throws_fn_error_message
1568
+ [ `process.on('exit')` ] : process.html#process_event_exit
1569
+ [ `tracker.calls()` ] : #assert_class_assert_CallTracker#tracker_calls
1570
+ [ `tracker.verify()` ] : #assert_class_assert_CallTracker#tracker_verify
1437
1571
[ strict assertion mode ] : #assert_strict_assertion_mode
1438
1572
[ Abstract Equality Comparison ] : https://tc39.github.io/ecma262/#sec-abstract-equality-comparison
1439
1573
[ Object wrappers ] : https://developer.mozilla.org/en-US/docs/Glossary/Primitive#Primitive_wrapper_objects_in_JavaScript
0 commit comments