@@ -215,331 +215,6 @@ try {
215
215
}
216
216
```
217
217
218
- ## Class: ` assert.CallTracker `
219
-
220
- <!-- YAML
221
- added:
222
- - v14.2.0
223
- - v12.19.0
224
- changes:
225
- - version: v20.1.0
226
- pr-url: https://github.com/nodejs/node/pull/47740
227
- description: the `assert.CallTracker` class has been deprecated and will be
228
- removed in a future version.
229
- -->
230
-
231
- > Stability: 0 - Deprecated
232
-
233
- This feature is deprecated and will be removed in a future version.
234
- Please consider using alternatives such as the
235
- [ ` mock ` ] [ ] helper function.
236
-
237
- ### ` new assert.CallTracker() `
238
-
239
- <!-- YAML
240
- added:
241
- - v14.2.0
242
- - v12.19.0
243
- -->
244
-
245
- Creates a new [ ` CallTracker ` ] [ ] object which can be used to track if functions
246
- were called a specific number of times. The ` tracker.verify() ` must be called
247
- for the verification to take place. The usual pattern would be to call it in a
248
- [ ` process.on('exit') ` ] [ ] handler.
249
-
250
- ``` mjs
251
- import assert from ' node:assert' ;
252
- import process from ' node:process' ;
253
-
254
- const tracker = new assert.CallTracker ();
255
-
256
- function func () {}
257
-
258
- // callsfunc() must be called exactly 1 time before tracker.verify().
259
- const callsfunc = tracker .calls (func, 1 );
260
-
261
- callsfunc ();
262
-
263
- // Calls tracker.verify() and verifies if all tracker.calls() functions have
264
- // been called exact times.
265
- process .on (' exit' , () => {
266
- tracker .verify ();
267
- });
268
- ```
269
-
270
- ``` cjs
271
- const assert = require (' node:assert' );
272
- const process = require (' node:process' );
273
-
274
- const tracker = new assert.CallTracker ();
275
-
276
- function func () {}
277
-
278
- // callsfunc() must be called exactly 1 time before tracker.verify().
279
- const callsfunc = tracker .calls (func, 1 );
280
-
281
- callsfunc ();
282
-
283
- // Calls tracker.verify() and verifies if all tracker.calls() functions have
284
- // been called exact times.
285
- process .on (' exit' , () => {
286
- tracker .verify ();
287
- });
288
- ```
289
-
290
- ### ` tracker.calls([fn][, exact]) `
291
-
292
- <!-- YAML
293
- added:
294
- - v14.2.0
295
- - v12.19.0
296
- -->
297
-
298
- * ` fn ` {Function} ** Default:** A no-op function.
299
- * ` exact ` {number} ** Default:** ` 1 ` .
300
- * Returns: {Function} A function that wraps ` fn ` .
301
-
302
- The wrapper function is expected to be called exactly ` exact ` times. If the
303
- function has not been called exactly ` exact ` times when
304
- [ ` tracker.verify() ` ] [ ] is called, then [ ` tracker.verify() ` ] [ ] will throw an
305
- error.
306
-
307
- ``` mjs
308
- import assert from ' node:assert' ;
309
-
310
- // Creates call tracker.
311
- const tracker = new assert.CallTracker ();
312
-
313
- function func () {}
314
-
315
- // Returns a function that wraps func() that must be called exact times
316
- // before tracker.verify().
317
- const callsfunc = tracker .calls (func);
318
- ```
319
-
320
- ``` cjs
321
- const assert = require (' node:assert' );
322
-
323
- // Creates call tracker.
324
- const tracker = new assert.CallTracker ();
325
-
326
- function func () {}
327
-
328
- // Returns a function that wraps func() that must be called exact times
329
- // before tracker.verify().
330
- const callsfunc = tracker .calls (func);
331
- ```
332
-
333
- ### ` tracker.getCalls(fn) `
334
-
335
- <!-- YAML
336
- added:
337
- - v18.8.0
338
- - v16.18.0
339
- -->
340
-
341
- * ` fn ` {Function}
342
-
343
- * Returns: {Array} An array with all the calls to a tracked function.
344
-
345
- * Object {Object}
346
- * ` thisArg ` {Object}
347
- * ` arguments ` {Array} the arguments passed to the tracked function
348
-
349
- ``` mjs
350
- import assert from ' node:assert' ;
351
-
352
- const tracker = new assert.CallTracker ();
353
-
354
- function func () {}
355
- const callsfunc = tracker .calls (func);
356
- callsfunc (1 , 2 , 3 );
357
-
358
- assert .deepStrictEqual (tracker .getCalls (callsfunc),
359
- [{ thisArg: undefined , arguments: [1 , 2 , 3 ] }]);
360
- ```
361
-
362
- ``` cjs
363
- const assert = require (' node:assert' );
364
-
365
- // Creates call tracker.
366
- const tracker = new assert.CallTracker ();
367
-
368
- function func () {}
369
- const callsfunc = tracker .calls (func);
370
- callsfunc (1 , 2 , 3 );
371
-
372
- assert .deepStrictEqual (tracker .getCalls (callsfunc),
373
- [{ thisArg: undefined , arguments: [1 , 2 , 3 ] }]);
374
- ```
375
-
376
- ### ` tracker.report() `
377
-
378
- <!-- YAML
379
- added:
380
- - v14.2.0
381
- - v12.19.0
382
- -->
383
-
384
- * Returns: {Array} An array of objects containing information about the wrapper
385
- functions returned by [ ` tracker.calls() ` ] [ ] .
386
- * Object {Object}
387
- * ` message ` {string}
388
- * ` actual ` {number} The actual number of times the function was called.
389
- * ` expected ` {number} The number of times the function was expected to be
390
- called.
391
- * ` operator ` {string} The name of the function that is wrapped.
392
- * ` stack ` {Object} A stack trace of the function.
393
-
394
- The arrays contains information about the expected and actual number of calls of
395
- the functions that have not been called the expected number of times.
396
-
397
- ``` mjs
398
- import assert from ' node:assert' ;
399
-
400
- // Creates call tracker.
401
- const tracker = new assert.CallTracker ();
402
-
403
- function func () {}
404
-
405
- // Returns a function that wraps func() that must be called exact times
406
- // before tracker.verify().
407
- const callsfunc = tracker .calls (func, 2 );
408
-
409
- // Returns an array containing information on callsfunc()
410
- console .log (tracker .report ());
411
- // [
412
- // {
413
- // message: 'Expected the func function to be executed 2 time(s) but was
414
- // executed 0 time(s).',
415
- // actual: 0,
416
- // expected: 2,
417
- // operator: 'func',
418
- // stack: stack trace
419
- // }
420
- // ]
421
- ```
422
-
423
- ``` cjs
424
- const assert = require (' node:assert' );
425
-
426
- // Creates call tracker.
427
- const tracker = new assert.CallTracker ();
428
-
429
- function func () {}
430
-
431
- // Returns a function that wraps func() that must be called exact times
432
- // before tracker.verify().
433
- const callsfunc = tracker .calls (func, 2 );
434
-
435
- // Returns an array containing information on callsfunc()
436
- console .log (tracker .report ());
437
- // [
438
- // {
439
- // message: 'Expected the func function to be executed 2 time(s) but was
440
- // executed 0 time(s).',
441
- // actual: 0,
442
- // expected: 2,
443
- // operator: 'func',
444
- // stack: stack trace
445
- // }
446
- // ]
447
- ```
448
-
449
- ### ` tracker.reset([fn]) `
450
-
451
- <!-- YAML
452
- added:
453
- - v18.8.0
454
- - v16.18.0
455
- -->
456
-
457
- * ` fn ` {Function} a tracked function to reset.
458
-
459
- Reset calls of the call tracker.
460
- If a tracked function is passed as an argument, the calls will be reset for it.
461
- If no arguments are passed, all tracked functions will be reset.
462
-
463
- ``` mjs
464
- import assert from ' node:assert' ;
465
-
466
- const tracker = new assert.CallTracker ();
467
-
468
- function func () {}
469
- const callsfunc = tracker .calls (func);
470
-
471
- callsfunc ();
472
- // Tracker was called once
473
- assert .strictEqual (tracker .getCalls (callsfunc).length , 1 );
474
-
475
- tracker .reset (callsfunc);
476
- assert .strictEqual (tracker .getCalls (callsfunc).length , 0 );
477
- ```
478
-
479
- ``` cjs
480
- const assert = require (' node:assert' );
481
-
482
- const tracker = new assert.CallTracker ();
483
-
484
- function func () {}
485
- const callsfunc = tracker .calls (func);
486
-
487
- callsfunc ();
488
- // Tracker was called once
489
- assert .strictEqual (tracker .getCalls (callsfunc).length , 1 );
490
-
491
- tracker .reset (callsfunc);
492
- assert .strictEqual (tracker .getCalls (callsfunc).length , 0 );
493
- ```
494
-
495
- ### ` tracker.verify() `
496
-
497
- <!-- YAML
498
- added:
499
- - v14.2.0
500
- - v12.19.0
501
- -->
502
-
503
- Iterates through the list of functions passed to
504
- [ ` tracker.calls() ` ] [ ] and will throw an error for functions that
505
- have not been called the expected number of times.
506
-
507
- ``` mjs
508
- import assert from ' node:assert' ;
509
-
510
- // Creates call tracker.
511
- const tracker = new assert.CallTracker ();
512
-
513
- function func () {}
514
-
515
- // Returns a function that wraps func() that must be called exact times
516
- // before tracker.verify().
517
- const callsfunc = tracker .calls (func, 2 );
518
-
519
- callsfunc ();
520
-
521
- // Will throw an error since callsfunc() was only called once.
522
- tracker .verify ();
523
- ```
524
-
525
- ``` cjs
526
- const assert = require (' node:assert' );
527
-
528
- // Creates call tracker.
529
- const tracker = new assert.CallTracker ();
530
-
531
- function func () {}
532
-
533
- // Returns a function that wraps func() that must be called exact times
534
- // before tracker.verify().
535
- const callsfunc = tracker .calls (func, 2 );
536
-
537
- callsfunc ();
538
-
539
- // Will throw an error since callsfunc() was only called once.
540
- tracker .verify ();
541
- ```
542
-
543
218
## ` assert(value[, message]) `
544
219
545
220
<!-- YAML
@@ -2760,7 +2435,6 @@ assert.partialDeepStrictEqual(
2760
2435
[ `===` operator ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality
2761
2436
[ `==` operator ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality
2762
2437
[ `AssertionError` ] : #class-assertassertionerror
2763
- [ `CallTracker` ] : #class-assertcalltracker
2764
2438
[ `Class` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
2765
2439
[ `ERR_INVALID_RETURN_VALUE` ] : errors.md#err_invalid_return_value
2766
2440
[ `Error.captureStackTrace` ] : errors.md#errorcapturestacktracetargetobject-constructoropt
@@ -2777,9 +2451,5 @@ assert.partialDeepStrictEqual(
2777
2451
[ `assert.strictEqual()` ] : #assertstrictequalactual-expected-message
2778
2452
[ `assert.throws()` ] : #assertthrowsfn-error-message
2779
2453
[ `getColorDepth()` ] : tty.md#writestreamgetcolordepthenv
2780
- [ `mock` ] : test.md#mocking
2781
- [ `process.on('exit')` ] : process.md#event-exit
2782
- [ `tracker.calls()` ] : #trackercallsfn-exact
2783
- [ `tracker.verify()` ] : #trackerverify
2784
2454
[ enumerable "own" properties ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties
2785
2455
[ prototype-spec ] : https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots
0 commit comments