@types/node 20.0.0 → 20.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. node/README.md +1 -1
  2. node/assert.d.ts +4 -3
  3. node/async_hooks.d.ts +1 -1
  4. node/buffer.d.ts +6 -6
  5. node/child_process.d.ts +5 -2
  6. node/cluster.d.ts +1 -1
  7. node/console.d.ts +1 -1
  8. node/crypto.d.ts +1 -1
  9. node/dgram.d.ts +1 -1
  10. node/diagnostics_channel.d.ts +1 -1
  11. node/dns.d.ts +9 -1
  12. node/domain.d.ts +2 -2
  13. node/events.d.ts +1 -1
  14. node/fs/promises.d.ts +4 -0
  15. node/fs.d.ts +27 -1
  16. node/http.d.ts +12 -4
  17. node/http2.d.ts +1 -1
  18. node/https.d.ts +1 -1
  19. node/index.d.ts +1 -1
  20. node/inspector.d.ts +1 -1
  21. node/net.d.ts +1 -1
  22. node/os.d.ts +1 -1
  23. node/package.json +2 -2
  24. node/path.d.ts +1 -1
  25. node/perf_hooks.d.ts +1 -1
  26. node/punycode.d.ts +1 -1
  27. node/querystring.d.ts +1 -1
  28. node/readline.d.ts +1 -1
  29. node/repl.d.ts +1 -1
  30. node/stream.d.ts +15 -1
  31. node/string_decoder.d.ts +1 -1
  32. node/test.d.ts +423 -110
  33. node/timers.d.ts +1 -1
  34. node/tls.d.ts +3 -2
  35. node/trace_events.d.ts +1 -1
  36. node/ts4.8/assert.d.ts +5 -4
  37. node/ts4.8/async_hooks.d.ts +5 -13
  38. node/ts4.8/buffer.d.ts +6 -6
  39. node/ts4.8/child_process.d.ts +5 -2
  40. node/ts4.8/cluster.d.ts +1 -1
  41. node/ts4.8/console.d.ts +1 -1
  42. node/ts4.8/crypto.d.ts +1 -1
  43. node/ts4.8/dgram.d.ts +1 -1
  44. node/ts4.8/diagnostics_channel.d.ts +1 -1
  45. node/ts4.8/dns.d.ts +9 -1
  46. node/ts4.8/domain.d.ts +2 -2
  47. node/ts4.8/events.d.ts +1 -1
  48. node/ts4.8/fs/promises.d.ts +24 -0
  49. node/ts4.8/fs.d.ts +123 -1
  50. node/ts4.8/globals.d.ts +29 -28
  51. node/ts4.8/http.d.ts +67 -2
  52. node/ts4.8/http2.d.ts +1 -1
  53. node/ts4.8/https.d.ts +1 -1
  54. node/ts4.8/inspector.d.ts +1 -1
  55. node/ts4.8/net.d.ts +7 -7
  56. node/ts4.8/os.d.ts +1 -1
  57. node/ts4.8/path.d.ts +1 -1
  58. node/ts4.8/perf_hooks.d.ts +1 -1
  59. node/ts4.8/punycode.d.ts +1 -1
  60. node/ts4.8/querystring.d.ts +1 -1
  61. node/ts4.8/readline.d.ts +1 -1
  62. node/ts4.8/repl.d.ts +1 -1
  63. node/ts4.8/stream.d.ts +15 -1
  64. node/ts4.8/string_decoder.d.ts +1 -1
  65. node/ts4.8/test.d.ts +423 -111
  66. node/ts4.8/timers.d.ts +8 -1
  67. node/ts4.8/tls.d.ts +3 -2
  68. node/ts4.8/trace_events.d.ts +1 -1
  69. node/ts4.8/tty.d.ts +1 -1
  70. node/ts4.8/url.d.ts +1 -1
  71. node/ts4.8/util.d.ts +2 -2
  72. node/ts4.8/v8.d.ts +4 -1
  73. node/ts4.8/vm.d.ts +1 -1
  74. node/ts4.8/wasi.d.ts +6 -6
  75. node/ts4.8/worker_threads.d.ts +1 -1
  76. node/ts4.8/zlib.d.ts +1 -1
  77. node/tty.d.ts +1 -1
  78. node/url.d.ts +1 -1
  79. node/util.d.ts +2 -2
  80. node/v8.d.ts +1 -1
  81. node/vm.d.ts +1 -1
  82. node/wasi.d.ts +6 -6
  83. node/worker_threads.d.ts +1 -1
  84. node/zlib.d.ts +1 -1
node/test.d.ts CHANGED
@@ -1,27 +1,112 @@
1
1
  /**
2
- * The `node:test` module provides a standalone testing module.
3
- * @see [source](https://github.com/nodejs/node/blob/v18.x/lib/test.js)
2
+ * The `node:test` module facilitates the creation of JavaScript tests.
3
+ * To access it:
4
+ *
5
+ * ```js
6
+ * import test from 'node:test';
7
+ * ```
8
+ *
9
+ * This module is only available under the `node:` scheme. The following will not
10
+ * work:
11
+ *
12
+ * ```js
13
+ * import test from 'test';
14
+ * ```
15
+ *
16
+ * Tests created via the `test` module consist of a single function that is
17
+ * processed in one of three ways:
18
+ *
19
+ * 1. A synchronous function that is considered failing if it throws an exception,
20
+ * and is considered passing otherwise.
21
+ * 2. A function that returns a `Promise` that is considered failing if the`Promise` rejects, and is considered passing if the `Promise` resolves.
22
+ * 3. A function that receives a callback function. If the callback receives any
23
+ * truthy value as its first argument, the test is considered failing. If a
24
+ * falsy value is passed as the first argument to the callback, the test is
25
+ * considered passing. If the test function receives a callback function and
26
+ * also returns a `Promise`, the test will fail.
27
+ *
28
+ * The following example illustrates how tests are written using the`test` module.
29
+ *
30
+ * ```js
31
+ * test('synchronous passing test', (t) => {
32
+ * // This test passes because it does not throw an exception.
33
+ * assert.strictEqual(1, 1);
34
+ * });
35
+ *
36
+ * test('synchronous failing test', (t) => {
37
+ * // This test fails because it throws an exception.
38
+ * assert.strictEqual(1, 2);
39
+ * });
40
+ *
41
+ * test('asynchronous passing test', async (t) => {
42
+ * // This test passes because the Promise returned by the async
43
+ * // function is not rejected.
44
+ * assert.strictEqual(1, 1);
45
+ * });
46
+ *
47
+ * test('asynchronous failing test', async (t) => {
48
+ * // This test fails because the Promise returned by the async
49
+ * // function is rejected.
50
+ * assert.strictEqual(1, 2);
51
+ * });
52
+ *
53
+ * test('failing test using Promises', (t) => {
54
+ * // Promises can be used directly as well.
55
+ * return new Promise((resolve, reject) => {
56
+ * setImmediate(() => {
57
+ * reject(new Error('this will cause the test to fail'));
58
+ * });
59
+ * });
60
+ * });
61
+ *
62
+ * test('callback passing test', (t, done) => {
63
+ * // done() is the callback function. When the setImmediate() runs, it invokes
64
+ * // done() with no arguments.
65
+ * setImmediate(done);
66
+ * });
67
+ *
68
+ * test('callback failing test', (t, done) => {
69
+ * // When the setImmediate() runs, done() is invoked with an Error object and
70
+ * // the test fails.
71
+ * setImmediate(() => {
72
+ * done(new Error('callback failure'));
73
+ * });
74
+ * });
75
+ * ```
76
+ *
77
+ * If any tests fail, the process exit code is set to `1`.
78
+ * @since v18.0.0, v16.17.0
79
+ * @see [source](https://github.com/nodejs/node/blob/v20.1.0/lib/test.js)
4
80
  */
5
81
  declare module 'node:test' {
82
+ import { Readable } from 'node:stream';
6
83
  /**
7
- * Programmatically start the test runner.
8
- * @since v18.9.0
9
- * @param options Configuration options for running tests.
10
- * @returns A {@link TestsStream} that emits events about the test execution.
84
+ * ```js
85
+ * import { tap } from 'node:test/reporters';
86
+ * import process from 'node:process';
87
+ *
88
+ * run({ files: [path.resolve('./tests/test.js')] })
89
+ * .compose(tap)
90
+ * .pipe(process.stdout);
91
+ * ```
92
+ * @since v18.9.0, v16.19.0
93
+ * @param options Configuration options for running tests. The following properties are supported:
11
94
  */
12
95
  function run(options?: RunOptions): TestsStream;
13
96
  /**
14
- * The `test()` function is the value imported from the test module. Each invocation of this
15
- * function results in reporting the test to the {@link TestsStream}.
97
+ * The `test()` function is the value imported from the `test` module. Each
98
+ * invocation of this function results in reporting the test to the `TestsStream`.
16
99
  *
17
- * The {@link TestContext} object passed to the fn argument can be used to perform actions
18
- * related to the current test. Examples include skipping the test, adding additional
19
- * diagnostic information, or creating subtests.
100
+ * The `TestContext` object passed to the `fn` argument can be used to perform
101
+ * actions related to the current test. Examples include skipping the test, adding
102
+ * additional diagnostic information, or creating subtests.
20
103
  *
21
- * `test()` returns a {@link Promise} that resolves once the test completes. The return value
22
- * can usually be discarded for top level tests. However, the return value from subtests should
23
- * be used to prevent the parent test from finishing first and cancelling the subtest as shown
24
- * in the following example.
104
+ * `test()` returns a `Promise` that resolves once the test completes.
105
+ * if `test()` is called within a `describe()` block, it resolve immediately.
106
+ * The return value can usually be discarded for top level tests.
107
+ * However, the return value from subtests should be used to prevent the parent
108
+ * test from finishing first and cancelling the subtest
109
+ * as shown in the following example.
25
110
  *
26
111
  * ```js
27
112
  * test('top level test', async (t) => {
@@ -35,49 +120,56 @@ declare module 'node:test' {
35
120
  * });
36
121
  * });
37
122
  * ```
38
- * @since v18.0.0
39
- * @param name The name of the test, which is displayed when reporting test results.
40
- * Default: The `name` property of fn, or `'<anonymous>'` if `fn` does not have a name.
41
- * @param options Configuration options for the test
42
- * @param fn The function under test. The first argument to this function is a
43
- * {@link TestContext} object. If the test uses callbacks, the callback function is
44
- * passed as the second argument. Default: A no-op function.
45
- * @returns A {@link Promise} resolved with `undefined` once the test completes.
123
+ *
124
+ * The `timeout` option can be used to fail the test if it takes longer than`timeout` milliseconds to complete. However, it is not a reliable mechanism for
125
+ * canceling tests because a running test might block the application thread and
126
+ * thus prevent the scheduled cancellation.
127
+ * @since v18.0.0, v16.17.0
128
+ * @param [name='The name'] The name of the test, which is displayed when reporting test results.
129
+ * @param options Configuration options for the test. The following properties are supported:
130
+ * @param [fn='A no-op function'] The function under test. The first argument to this function is a {@link TestContext} object. If the test uses callbacks, the callback function is passed as the
131
+ * second argument.
132
+ * @return Resolved with `undefined` once the test completes, or immediately if the test runs within {@link describe}.
46
133
  */
47
134
  function test(name?: string, fn?: TestFn): Promise<void>;
48
135
  function test(name?: string, options?: TestOptions, fn?: TestFn): Promise<void>;
49
136
  function test(options?: TestOptions, fn?: TestFn): Promise<void>;
50
137
  function test(fn?: TestFn): Promise<void>;
51
138
  /**
52
- * @since v18.6.0
53
- * @param name The name of the suite, which is displayed when reporting suite results.
54
- * Default: The `name` property of fn, or `'<anonymous>'` if `fn` does not have a name.
55
- * @param options Configuration options for the suite
56
- * @param fn The function under suite. Default: A no-op function.
139
+ * The `describe()` function imported from the `node:test` module. Each
140
+ * invocation of this function results in the creation of a Subtest.
141
+ * After invocation of top level `describe` functions,
142
+ * all top level tests and suites will execute.
143
+ * @param [name='The name'] The name of the suite, which is displayed when reporting test results.
144
+ * @param options Configuration options for the suite. supports the same options as `test([name][, options][, fn])`.
145
+ * @param [fn='A no-op function'] The function under suite declaring all subtests and subsuites. The first argument to this function is a {@link SuiteContext} object.
146
+ * @return `undefined`.
57
147
  */
58
148
  function describe(name?: string, options?: TestOptions, fn?: SuiteFn): void;
59
149
  function describe(name?: string, fn?: SuiteFn): void;
60
150
  function describe(options?: TestOptions, fn?: SuiteFn): void;
61
151
  function describe(fn?: SuiteFn): void;
62
152
  namespace describe {
63
- // Shorthand for skipping a suite, same as `describe([name], { skip: true }[, fn])`.
153
+ /**
154
+ * Shorthand for skipping a suite, same as `describe([name], { skip: true }[, fn])`.
155
+ */
64
156
  function skip(name?: string, options?: TestOptions, fn?: SuiteFn): void;
65
157
  function skip(name?: string, fn?: SuiteFn): void;
66
158
  function skip(options?: TestOptions, fn?: SuiteFn): void;
67
159
  function skip(fn?: SuiteFn): void;
68
- // Shorthand for marking a suite as `TODO`, same as `describe([name], { todo: true }[, fn])`.
160
+ /**
161
+ * Shorthand for marking a suite as `TODO`, same as `describe([name], { todo: true }[, fn])`.
162
+ */
69
163
  function todo(name?: string, options?: TestOptions, fn?: SuiteFn): void;
70
164
  function todo(name?: string, fn?: SuiteFn): void;
71
165
  function todo(options?: TestOptions, fn?: SuiteFn): void;
72
166
  function todo(fn?: SuiteFn): void;
73
167
  }
74
168
  /**
75
- * @since v18.6.0
76
- * @param name The name of the test, which is displayed when reporting test results.
77
- * Default: The `name` property of fn, or `'<anonymous>'` if `fn` does not have a name.
78
- * @param options Configuration options for the test
79
- * @param fn The function under test. If the test uses callbacks, the callback function is
80
- * passed as the second argument. Default: A no-op function.
169
+ * Shorthand for `test()`.
170
+ *
171
+ * The `it()` function is imported from the `node:test` module.
172
+ * @since v18.6.0, v16.17.0
81
173
  */
82
174
  function it(name?: string, options?: TestOptions, fn?: ItFn): void;
83
175
  function it(name?: string, fn?: ItFn): void;
@@ -142,14 +234,19 @@ declare module 'node:test' {
142
234
  * incremented from the primary's `process.debugPort`.
143
235
  */
144
236
  inspectPort?: number | (() => number) | undefined;
237
+ /**
238
+ * That can be used to only run tests whose name matches the provided pattern.
239
+ * Test name patterns are interpreted as JavaScript regular expressions.
240
+ * For each test that is executed, any corresponding test hooks, such as `beforeEach()`, are also run.
241
+ */
242
+ testNamePatterns?: string | RegExp | string[] | RegExp[];
145
243
  }
146
244
  /**
147
- * A successful call of the `run()` method will return a new `TestsStream` object,
148
- * streaming a series of events representing the execution of the tests.
149
- * `TestsStream` will emit events in the order of the tests' definitions.
150
- * @since v18.9.0
245
+ * A successful call to `run()` method will return a new `TestsStream` object, streaming a series of events representing the execution of the tests.`TestsStream` will emit events, in the
246
+ * order of the tests definition
247
+ * @since v18.9.0, v16.19.0
151
248
  */
152
- interface TestsStream extends NodeJS.ReadableStream {
249
+ class TestsStream extends Readable implements NodeJS.ReadableStream {
153
250
  addListener(event: 'test:diagnostic', listener: (data: DiagnosticData) => void): this;
154
251
  addListener(event: 'test:fail', listener: (data: TestFail) => void): this;
155
252
  addListener(event: 'test:pass', listener: (data: TestPass) => void): this;
@@ -284,11 +381,20 @@ declare module 'node:test' {
284
381
  nesting: number;
285
382
  }
286
383
  /**
287
- * An instance of `TestContext` is passed to each test function in order to interact with the
288
- * test runner. However, the `TestContext` constructor is not exposed as part of the API.
289
- * @since v18.0.0
384
+ * An instance of `TestContext` is passed to each test function in order to
385
+ * interact with the test runner. However, the `TestContext` constructor is not
386
+ * exposed as part of the API.
387
+ * @since v18.0.0, v16.17.0
290
388
  */
291
- interface TestContext {
389
+ class TestContext {
390
+ /**
391
+ * This function is used to create a hook running before subtest of the current test.
392
+ * @param fn The hook function. If the hook uses callbacks, the callback function is passed as
393
+ * the second argument. Default: A no-op function.
394
+ * @param options Configuration options for the hook.
395
+ * @since v20.1.0
396
+ */
397
+ before: typeof before;
292
398
  /**
293
399
  * This function is used to create a hook running before each subtest of the current test.
294
400
  * @param fn The hook function. If the hook uses callbacks, the callback function is passed as
@@ -314,44 +420,81 @@ declare module 'node:test' {
314
420
  */
315
421
  afterEach: typeof afterEach;
316
422
  /**
317
- * This function is used to write diagnostics to the output. Any diagnostic information is
318
- * included at the end of the test's results. This function does not return a value.
423
+ * This function is used to write diagnostics to the output. Any diagnostic
424
+ * information is included at the end of the test's results. This function does
425
+ * not return a value.
426
+ *
427
+ * ```js
428
+ * test('top level test', (t) => {
429
+ * t.diagnostic('A diagnostic message');
430
+ * });
431
+ * ```
432
+ * @since v18.0.0, v16.17.0
319
433
  * @param message Message to be reported.
320
- * @since v18.0.0
321
434
  */
322
435
  diagnostic(message: string): void;
323
436
  /**
324
437
  * The name of the test.
325
- * @since v18.8.0
438
+ * @since v18.8.0, v16.18.0
326
439
  */
327
440
  readonly name: string;
328
441
  /**
329
- * If `shouldRunOnlyTests` is truthy, the test context will only run tests that have the `only`
330
- * option set. Otherwise, all tests are run. If Node.js was not started with the `--test-only`
331
- * command-line option, this function is a no-op.
442
+ * If `shouldRunOnlyTests` is truthy, the test context will only run tests that
443
+ * have the `only` option set. Otherwise, all tests are run. If Node.js was not
444
+ * started with the `--test-only` command-line option, this function is a
445
+ * no-op.
446
+ *
447
+ * ```js
448
+ * test('top level test', (t) => {
449
+ * // The test context can be set to run subtests with the 'only' option.
450
+ * t.runOnly(true);
451
+ * return Promise.all([
452
+ * t.test('this subtest is now skipped'),
453
+ * t.test('this subtest is run', { only: true }),
454
+ * ]);
455
+ * });
456
+ * ```
457
+ * @since v18.0.0, v16.17.0
332
458
  * @param shouldRunOnlyTests Whether or not to run `only` tests.
333
- * @since v18.0.0
334
459
  */
335
460
  runOnly(shouldRunOnlyTests: boolean): void;
336
461
  /**
337
- * Can be used to abort test subtasks when the test has been aborted.
338
- * @since v18.7.0
462
+ * ```js
463
+ * test('top level test', async (t) => {
464
+ * await fetch('some/uri', { signal: t.signal });
465
+ * });
466
+ * ```
467
+ * @since v18.7.0, v16.17.0
339
468
  */
340
469
  readonly signal: AbortSignal;
341
470
  /**
342
- * This function causes the test's output to indicate the test as skipped. If `message` is
343
- * provided, it is included in the output. Calling `skip()` does not terminate execution of
344
- * the test function. This function does not return a value.
471
+ * This function causes the test's output to indicate the test as skipped. If`message` is provided, it is included in the output. Calling `skip()` does
472
+ * not terminate execution of the test function. This function does not return a
473
+ * value.
474
+ *
475
+ * ```js
476
+ * test('top level test', (t) => {
477
+ * // Make sure to return here as well if the test contains additional logic.
478
+ * t.skip('this is skipped');
479
+ * });
480
+ * ```
481
+ * @since v18.0.0, v16.17.0
345
482
  * @param message Optional skip message.
346
- * @since v18.0.0
347
483
  */
348
484
  skip(message?: string): void;
349
485
  /**
350
- * This function adds a `TODO` directive to the test's output. If `message` is provided, it is
351
- * included in the output. Calling `todo()` does not terminate execution of the test
352
- * function. This function does not return a value.
486
+ * This function adds a `TODO` directive to the test's output. If `message` is
487
+ * provided, it is included in the output. Calling `todo()` does not terminate
488
+ * execution of the test function. This function does not return a value.
489
+ *
490
+ * ```js
491
+ * test('top level test', (t) => {
492
+ * // This test is marked as `TODO`
493
+ * t.todo('this is a todo');
494
+ * });
495
+ * ```
496
+ * @since v18.0.0, v16.17.0
353
497
  * @param message Optional `TODO` message.
354
- * @since v18.0.0
355
498
  */
356
499
  todo(message?: string): void;
357
500
  /**
@@ -415,34 +558,68 @@ declare module 'node:test' {
415
558
  }
416
559
  /**
417
560
  * This function is used to create a hook running before running a suite.
418
- * @param fn The hook function. If the hook uses callbacks, the callback function is passed as
419
- * the second argument. Default: A no-op function.
420
- * @param options Configuration options for the hook.
421
- * @since v18.8.0
561
+ *
562
+ * ```js
563
+ * describe('tests', async () => {
564
+ * before(() => console.log('about to run some test'));
565
+ * it('is a subtest', () => {
566
+ * assert.ok('some relevant assertion here');
567
+ * });
568
+ * });
569
+ * ```
570
+ * @since v18.8.0, v16.18.0
571
+ * @param [fn='A no-op function'] The hook function. If the hook uses callbacks, the callback function is passed as the second argument.
572
+ * @param options Configuration options for the hook. The following properties are supported:
422
573
  */
423
574
  function before(fn?: HookFn, options?: HookOptions): void;
424
575
  /**
425
- * This function is used to create a hook running after running a suite.
426
- * @param fn The hook function. If the hook uses callbacks, the callback function is passed as
427
- * the second argument. Default: A no-op function.
428
- * @param options Configuration options for the hook.
429
- * @since v18.8.0
576
+ * This function is used to create a hook running after running a suite.
577
+ *
578
+ * ```js
579
+ * describe('tests', async () => {
580
+ * after(() => console.log('finished running tests'));
581
+ * it('is a subtest', () => {
582
+ * assert.ok('some relevant assertion here');
583
+ * });
584
+ * });
585
+ * ```
586
+ * @since v18.8.0, v16.18.0
587
+ * @param [fn='A no-op function'] The hook function. If the hook uses callbacks, the callback function is passed as the second argument.
588
+ * @param options Configuration options for the hook. The following properties are supported:
430
589
  */
431
590
  function after(fn?: HookFn, options?: HookOptions): void;
432
591
  /**
433
- * This function is used to create a hook running before each subtest of the current suite.
434
- * @param fn The hook function. If the hook uses callbacks, the callback function is passed as
435
- * the second argument. Default: A no-op function.
436
- * @param options Configuration options for the hook.
437
- * @since v18.8.0
592
+ * This function is used to create a hook running
593
+ * before each subtest of the current suite.
594
+ *
595
+ * ```js
596
+ * describe('tests', async () => {
597
+ * beforeEach(() => console.log('about to run a test'));
598
+ * it('is a subtest', () => {
599
+ * assert.ok('some relevant assertion here');
600
+ * });
601
+ * });
602
+ * ```
603
+ * @since v18.8.0, v16.18.0
604
+ * @param [fn='A no-op function'] The hook function. If the hook uses callbacks, the callback function is passed as the second argument.
605
+ * @param options Configuration options for the hook. The following properties are supported:
438
606
  */
439
607
  function beforeEach(fn?: HookFn, options?: HookOptions): void;
440
608
  /**
441
- * This function is used to create a hook running after each subtest of the current test.
442
- * @param fn The hook function. If the hook uses callbacks, the callback function is passed as
443
- * the second argument. Default: A no-op function.
444
- * @param options Configuration options for the hook.
445
- * @since v18.8.0
609
+ * This function is used to create a hook running
610
+ * after each subtest of the current test.
611
+ *
612
+ * ```js
613
+ * describe('tests', async () => {
614
+ * afterEach(() => console.log('finished running a test'));
615
+ * it('is a subtest', () => {
616
+ * assert.ok('some relevant assertion here');
617
+ * });
618
+ * });
619
+ * ```
620
+ * @since v18.8.0, v16.18.0
621
+ * @param [fn='A no-op function'] The hook function. If the hook uses callbacks, the callback function is passed as the second argument.
622
+ * @param options Configuration options for the hook. The following properties are supported:
446
623
  */
447
624
  function afterEach(fn?: HookFn, options?: HookOptions): void;
448
625
  /**
@@ -495,22 +672,87 @@ declare module 'node:test' {
495
672
  type FunctionPropertyNames<T> = {
496
673
  [K in keyof T]: T[K] extends Function ? K : never;
497
674
  }[keyof T];
498
- interface MockTracker {
675
+ /**
676
+ * The `MockTracker` class is used to manage mocking functionality. The test runner
677
+ * module provides a top level `mock` export which is a `MockTracker` instance.
678
+ * Each test also provides its own `MockTracker` instance via the test context's`mock` property.
679
+ * @since v19.1.0, v18.13.0
680
+ */
681
+ class MockTracker {
499
682
  /**
500
683
  * This function is used to create a mock function.
501
- * @param original An optional function to create a mock on.
502
- * @param implementation An optional function used as the mock implementation for `original`.
503
- * This is useful for creating mocks that exhibit one behavior for a specified number of calls and then restore the behavior of `original`.
504
- * @param options Optional configuration options for the mock function.
684
+ *
685
+ * The following example creates a mock function that increments a counter by one
686
+ * on each invocation. The `times` option is used to modify the mock behavior such
687
+ * that the first two invocations add two to the counter instead of one.
688
+ *
689
+ * ```js
690
+ * test('mocks a counting function', (t) => {
691
+ * let cnt = 0;
692
+ *
693
+ * function addOne() {
694
+ * cnt++;
695
+ * return cnt;
696
+ * }
697
+ *
698
+ * function addTwo() {
699
+ * cnt += 2;
700
+ * return cnt;
701
+ * }
702
+ *
703
+ * const fn = t.mock.fn(addOne, addTwo, { times: 2 });
704
+ *
705
+ * assert.strictEqual(fn(), 2);
706
+ * assert.strictEqual(fn(), 4);
707
+ * assert.strictEqual(fn(), 5);
708
+ * assert.strictEqual(fn(), 6);
709
+ * });
710
+ * ```
711
+ * @since v19.1.0, v18.13.0
712
+ * @param [original='A no-op function'] An optional function to create a mock on.
713
+ * @param implementation An optional function used as the mock implementation for `original`. This is useful for creating mocks that exhibit one behavior for a specified number of calls and
714
+ * then restore the behavior of `original`.
715
+ * @param options Optional configuration options for the mock function. The following properties are supported:
716
+ * @return The mocked function. The mocked function contains a special `mock` property, which is an instance of {@link MockFunctionContext}, and can be used for inspecting and changing the
717
+ * behavior of the mocked function.
505
718
  */
506
719
  fn<F extends Function = NoOpFunction>(original?: F, options?: MockFunctionOptions): Mock<F>;
507
720
  fn<F extends Function = NoOpFunction, Implementation extends Function = F>(original?: F, implementation?: Implementation, options?: MockFunctionOptions): Mock<F | Implementation>;
508
721
  /**
509
- * This function is used to create a mock on an existing object method.
722
+ * This function is used to create a mock on an existing object method. The
723
+ * following example demonstrates how a mock is created on an existing object
724
+ * method.
725
+ *
726
+ * ```js
727
+ * test('spies on an object method', (t) => {
728
+ * const number = {
729
+ * value: 5,
730
+ * subtract(a) {
731
+ * return this.value - a;
732
+ * },
733
+ * };
734
+ *
735
+ * t.mock.method(number, 'subtract');
736
+ * assert.strictEqual(number.subtract.mock.calls.length, 0);
737
+ * assert.strictEqual(number.subtract(3), 2);
738
+ * assert.strictEqual(number.subtract.mock.calls.length, 1);
739
+ *
740
+ * const call = number.subtract.mock.calls[0];
741
+ *
742
+ * assert.deepStrictEqual(call.arguments, [3]);
743
+ * assert.strictEqual(call.result, 2);
744
+ * assert.strictEqual(call.error, undefined);
745
+ * assert.strictEqual(call.target, undefined);
746
+ * assert.strictEqual(call.this, number);
747
+ * });
748
+ * ```
749
+ * @since v19.1.0, v18.13.0
510
750
  * @param object The object whose method is being mocked.
511
751
  * @param methodName The identifier of the method on `object` to mock. If `object[methodName]` is not a function, an error is thrown.
512
752
  * @param implementation An optional function used as the mock implementation for `object[methodName]`.
513
- * @param options Optional configuration options for the mock method.
753
+ * @param options Optional configuration options for the mock method. The following properties are supported:
754
+ * @return The mocked method. The mocked method contains a special `mock` property, which is an instance of {@link MockFunctionContext}, and can be used for inspecting and changing the
755
+ * behavior of the mocked method.
514
756
  */
515
757
  method<
516
758
  MockedObject extends object,
@@ -547,7 +789,8 @@ declare module 'node:test' {
547
789
  ): Mock<Function>;
548
790
 
549
791
  /**
550
- * This function is syntax sugar for {@link MockTracker.method} with `options.getter` set to `true`.
792
+ * This function is syntax sugar for `MockTracker.method` with `options.getter`set to `true`.
793
+ * @since v19.3.0, v18.13.0
551
794
  */
552
795
  getter<
553
796
  MockedObject extends object,
@@ -568,7 +811,8 @@ declare module 'node:test' {
568
811
  options?: MockFunctionOptions,
569
812
  ): Mock<(() => MockedObject[MethodName]) | Implementation>;
570
813
  /**
571
- * This function is syntax sugar for {@link MockTracker.method} with `options.setter` set to `true`.
814
+ * This function is syntax sugar for `MockTracker.method` with `options.setter`set to `true`.
815
+ * @since v19.3.0, v18.13.0
572
816
  */
573
817
  setter<
574
818
  MockedObject extends object,
@@ -589,17 +833,21 @@ declare module 'node:test' {
589
833
  options?: MockFunctionOptions,
590
834
  ): Mock<((value: MockedObject[MethodName]) => void) | Implementation>;
591
835
  /**
592
- * This function restores the default behavior of all mocks that were previously created by this `MockTracker`
593
- * and disassociates the mocks from the `MockTracker` instance. Once disassociated, the mocks can still be used,
594
- * but the `MockTracker` instance can no longer be used to reset their behavior or otherwise interact with them.
836
+ * This function restores the default behavior of all mocks that were previously
837
+ * created by this `MockTracker` and disassociates the mocks from the`MockTracker` instance. Once disassociated, the mocks can still be used, but the`MockTracker` instance can no longer be
838
+ * used to reset their behavior or
839
+ * otherwise interact with them.
595
840
  *
596
- * After each test completes, this function is called on the test context's `MockTracker`.
597
- * If the global `MockTracker` is used extensively, calling this function manually is recommended.
841
+ * After each test completes, this function is called on the test context's`MockTracker`. If the global `MockTracker` is used extensively, calling this
842
+ * function manually is recommended.
843
+ * @since v19.1.0, v18.13.0
598
844
  */
599
845
  reset(): void;
600
846
  /**
601
- * This function restores the default behavior of all mocks that were previously created by this `MockTracker`.
602
- * Unlike `mock.reset()`, `mock.restoreAll()` does not disassociate the mocks from the `MockTracker` instance.
847
+ * This function restores the default behavior of all mocks that were previously
848
+ * created by this `MockTracker`. Unlike `mock.reset()`, `mock.restoreAll()` does
849
+ * not disassociate the mocks from the `MockTracker` instance.
850
+ * @since v19.1.0, v18.13.0
603
851
  */
604
852
  restoreAll(): void;
605
853
  }
@@ -645,38 +893,103 @@ declare module 'node:test' {
645
893
  */
646
894
  this: unknown;
647
895
  }
648
- interface MockFunctionContext<F extends Function> {
896
+ /**
897
+ * The `MockFunctionContext` class is used to inspect or manipulate the behavior of
898
+ * mocks created via the `MockTracker` APIs.
899
+ * @since v19.1.0, v18.13.0
900
+ */
901
+ class MockFunctionContext<F extends Function> {
649
902
  /**
650
- * A getter that returns a copy of the internal array used to track calls to the mock.
903
+ * A getter that returns a copy of the internal array used to track calls to the
904
+ * mock. Each entry in the array is an object with the following properties.
905
+ * @since v19.1.0, v18.13.0
651
906
  */
652
907
  readonly calls: Array<MockFunctionCall<F>>;
653
908
  /**
654
- * This function returns the number of times that this mock has been invoked.
655
- * This function is more efficient than checking `ctx.calls.length`
656
- * because `ctx.calls` is a getter that creates a copy of the internal call tracking array.
909
+ * This function returns the number of times that this mock has been invoked. This
910
+ * function is more efficient than checking `ctx.calls.length` because `ctx.calls`is a getter that creates a copy of the internal call tracking array.
911
+ * @since v19.1.0, v18.13.0
912
+ * @return The number of times that this mock has been invoked.
657
913
  */
658
914
  callCount(): number;
659
915
  /**
660
916
  * This function is used to change the behavior of an existing mock.
917
+ *
918
+ * The following example creates a mock function using `t.mock.fn()`, calls the
919
+ * mock function, and then changes the mock implementation to a different function.
920
+ *
921
+ * ```js
922
+ * test('changes a mock behavior', (t) => {
923
+ * let cnt = 0;
924
+ *
925
+ * function addOne() {
926
+ * cnt++;
927
+ * return cnt;
928
+ * }
929
+ *
930
+ * function addTwo() {
931
+ * cnt += 2;
932
+ * return cnt;
933
+ * }
934
+ *
935
+ * const fn = t.mock.fn(addOne);
936
+ *
937
+ * assert.strictEqual(fn(), 1);
938
+ * fn.mock.mockImplementation(addTwo);
939
+ * assert.strictEqual(fn(), 3);
940
+ * assert.strictEqual(fn(), 5);
941
+ * });
942
+ * ```
943
+ * @since v19.1.0, v18.13.0
661
944
  * @param implementation The function to be used as the mock's new implementation.
662
945
  */
663
946
  mockImplementation(implementation: Function): void;
664
947
  /**
665
- * This function is used to change the behavior of an existing mock for a single invocation.
666
- * Once invocation `onCall` has occurred, the mock will revert to whatever behavior
667
- * it would have used had `mockImplementationOnce()` not been called.
948
+ * This function is used to change the behavior of an existing mock for a single
949
+ * invocation. Once invocation `onCall` has occurred, the mock will revert to
950
+ * whatever behavior it would have used had `mockImplementationOnce()` not been
951
+ * called.
952
+ *
953
+ * The following example creates a mock function using `t.mock.fn()`, calls the
954
+ * mock function, changes the mock implementation to a different function for the
955
+ * next invocation, and then resumes its previous behavior.
956
+ *
957
+ * ```js
958
+ * test('changes a mock behavior once', (t) => {
959
+ * let cnt = 0;
960
+ *
961
+ * function addOne() {
962
+ * cnt++;
963
+ * return cnt;
964
+ * }
965
+ *
966
+ * function addTwo() {
967
+ * cnt += 2;
968
+ * return cnt;
969
+ * }
970
+ *
971
+ * const fn = t.mock.fn(addOne);
972
+ *
973
+ * assert.strictEqual(fn(), 1);
974
+ * fn.mock.mockImplementationOnce(addTwo);
975
+ * assert.strictEqual(fn(), 3);
976
+ * assert.strictEqual(fn(), 4);
977
+ * });
978
+ * ```
979
+ * @since v19.1.0, v18.13.0
668
980
  * @param implementation The function to be used as the mock's implementation for the invocation number specified by `onCall`.
669
- * @param onCall The invocation number that will use `implementation`.
670
- * If the specified invocation has already occurred then an exception is thrown.
981
+ * @param onCall The invocation number that will use `implementation`. If the specified invocation has already occurred then an exception is thrown.
671
982
  */
672
983
  mockImplementationOnce(implementation: Function, onCall?: number): void;
673
984
  /**
674
985
  * Resets the call history of the mock function.
986
+ * @since v19.3.0, v18.13.0
675
987
  */
676
988
  resetCalls(): void;
677
989
  /**
678
- * Resets the implementation of the mock function to its original behavior.
679
- * The mock can still be used after calling this function.
990
+ * Resets the implementation of the mock function to its original behavior. The
991
+ * mock can still be used after calling this function.
992
+ * @since v19.1.0, v18.13.0
680
993
  */
681
994
  restore(): void;
682
995
  }