@types/node 18.16.3 → 20.1.0

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 (96) hide show
  1. node/README.md +1 -1
  2. node/assert.d.ts +71 -75
  3. node/async_hooks.d.ts +62 -42
  4. node/buffer.d.ts +127 -99
  5. node/child_process.d.ts +50 -54
  6. node/cluster.d.ts +12 -12
  7. node/console.d.ts +5 -5
  8. node/crypto.d.ts +209 -220
  9. node/dgram.d.ts +15 -15
  10. node/diagnostics_channel.d.ts +25 -26
  11. node/dns/promises.d.ts +6 -6
  12. node/dns.d.ts +24 -16
  13. node/domain.d.ts +4 -4
  14. node/events.d.ts +60 -60
  15. node/fs/promises.d.ts +78 -48
  16. node/fs.d.ts +117 -81
  17. node/http.d.ts +158 -147
  18. node/http2.d.ts +42 -46
  19. node/https.d.ts +52 -153
  20. node/index.d.ts +1 -1
  21. node/inspector.d.ts +10 -3
  22. node/module.d.ts +5 -4
  23. node/net.d.ts +21 -18
  24. node/os.d.ts +22 -18
  25. node/package.json +2 -2
  26. node/path.d.ts +4 -4
  27. node/perf_hooks.d.ts +28 -15
  28. node/process.d.ts +43 -46
  29. node/punycode.d.ts +1 -1
  30. node/querystring.d.ts +5 -5
  31. node/readline/promises.d.ts +6 -4
  32. node/readline.d.ts +15 -15
  33. node/repl.d.ts +9 -9
  34. node/stream/consumers.d.ts +1 -1
  35. node/stream.d.ts +88 -136
  36. node/string_decoder.d.ts +6 -6
  37. node/test.d.ts +423 -186
  38. node/timers/promises.d.ts +3 -3
  39. node/timers.d.ts +2 -2
  40. node/tls.d.ts +24 -16
  41. node/trace_events.d.ts +20 -9
  42. node/ts4.8/assert.d.ts +71 -75
  43. node/ts4.8/async_hooks.d.ts +62 -42
  44. node/ts4.8/buffer.d.ts +127 -99
  45. node/ts4.8/child_process.d.ts +50 -54
  46. node/ts4.8/cluster.d.ts +12 -12
  47. node/ts4.8/console.d.ts +5 -5
  48. node/ts4.8/crypto.d.ts +209 -220
  49. node/ts4.8/dgram.d.ts +15 -15
  50. node/ts4.8/diagnostics_channel.d.ts +25 -26
  51. node/ts4.8/dns/promises.d.ts +6 -6
  52. node/ts4.8/dns.d.ts +24 -16
  53. node/ts4.8/domain.d.ts +4 -4
  54. node/ts4.8/events.d.ts +60 -60
  55. node/ts4.8/fs/promises.d.ts +96 -45
  56. node/ts4.8/fs.d.ts +203 -67
  57. node/ts4.8/globals.d.ts +29 -28
  58. node/ts4.8/http.d.ts +198 -126
  59. node/ts4.8/http2.d.ts +42 -46
  60. node/ts4.8/https.d.ts +52 -153
  61. node/ts4.8/inspector.d.ts +10 -3
  62. node/ts4.8/module.d.ts +5 -4
  63. node/ts4.8/net.d.ts +24 -21
  64. node/ts4.8/os.d.ts +22 -18
  65. node/ts4.8/path.d.ts +4 -4
  66. node/ts4.8/perf_hooks.d.ts +28 -15
  67. node/ts4.8/process.d.ts +43 -46
  68. node/ts4.8/punycode.d.ts +1 -1
  69. node/ts4.8/querystring.d.ts +5 -5
  70. node/ts4.8/readline/promises.d.ts +6 -4
  71. node/ts4.8/readline.d.ts +15 -15
  72. node/ts4.8/repl.d.ts +9 -9
  73. node/ts4.8/stream/consumers.d.ts +1 -1
  74. node/ts4.8/stream.d.ts +91 -139
  75. node/ts4.8/string_decoder.d.ts +6 -6
  76. node/ts4.8/test.d.ts +423 -186
  77. node/ts4.8/timers/promises.d.ts +3 -3
  78. node/ts4.8/timers.d.ts +9 -2
  79. node/ts4.8/tls.d.ts +24 -16
  80. node/ts4.8/trace_events.d.ts +20 -9
  81. node/ts4.8/tty.d.ts +4 -5
  82. node/ts4.8/url.d.ts +26 -36
  83. node/ts4.8/util.d.ts +143 -116
  84. node/ts4.8/v8.d.ts +110 -16
  85. node/ts4.8/vm.d.ts +292 -42
  86. node/ts4.8/wasi.d.ts +13 -19
  87. node/ts4.8/worker_threads.d.ts +32 -34
  88. node/ts4.8/zlib.d.ts +11 -11
  89. node/tty.d.ts +4 -5
  90. node/url.d.ts +26 -36
  91. node/util.d.ts +146 -111
  92. node/v8.d.ts +110 -16
  93. node/vm.d.ts +292 -42
  94. node/wasi.d.ts +13 -19
  95. node/worker_threads.d.ts +32 -34
  96. node/zlib.d.ts +11 -11
node/test.d.ts CHANGED
@@ -1,28 +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
-
14
96
  /**
15
- * The `test()` function is the value imported from the test module. Each invocation of this
16
- * 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`.
17
99
  *
18
- * The {@link TestContext} object passed to the fn argument can be used to perform actions
19
- * related to the current test. Examples include skipping the test, adding additional
20
- * 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.
21
103
  *
22
- * `test()` returns a {@link Promise} that resolves once the test completes. The return value
23
- * can usually be discarded for top level tests. However, the return value from subtests should
24
- * be used to prevent the parent test from finishing first and cancelling the subtest as shown
25
- * 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.
26
110
  *
27
111
  * ```js
28
112
  * test('top level test', async (t) => {
@@ -36,52 +120,56 @@ declare module 'node:test' {
36
120
  * });
37
121
  * });
38
122
  * ```
39
- * @since v18.0.0
40
- * @param name The name of the test, which is displayed when reporting test results.
41
- * Default: The `name` property of fn, or `'<anonymous>'` if `fn` does not have a name.
42
- * @param options Configuration options for the test
43
- * @param fn The function under test. The first argument to this function is a
44
- * {@link TestContext} object. If the test uses callbacks, the callback function is
45
- * passed as the second argument. Default: A no-op function.
46
- * @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}.
47
133
  */
48
134
  function test(name?: string, fn?: TestFn): Promise<void>;
49
135
  function test(name?: string, options?: TestOptions, fn?: TestFn): Promise<void>;
50
136
  function test(options?: TestOptions, fn?: TestFn): Promise<void>;
51
137
  function test(fn?: TestFn): Promise<void>;
52
-
53
138
  /**
54
- * @since v18.6.0
55
- * @param name The name of the suite, which is displayed when reporting suite results.
56
- * Default: The `name` property of fn, or `'<anonymous>'` if `fn` does not have a name.
57
- * @param options Configuration options for the suite
58
- * @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`.
59
147
  */
60
148
  function describe(name?: string, options?: TestOptions, fn?: SuiteFn): void;
61
149
  function describe(name?: string, fn?: SuiteFn): void;
62
150
  function describe(options?: TestOptions, fn?: SuiteFn): void;
63
151
  function describe(fn?: SuiteFn): void;
64
152
  namespace describe {
65
- // 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
+ */
66
156
  function skip(name?: string, options?: TestOptions, fn?: SuiteFn): void;
67
157
  function skip(name?: string, fn?: SuiteFn): void;
68
158
  function skip(options?: TestOptions, fn?: SuiteFn): void;
69
159
  function skip(fn?: SuiteFn): void;
70
-
71
- // 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
+ */
72
163
  function todo(name?: string, options?: TestOptions, fn?: SuiteFn): void;
73
164
  function todo(name?: string, fn?: SuiteFn): void;
74
165
  function todo(options?: TestOptions, fn?: SuiteFn): void;
75
166
  function todo(fn?: SuiteFn): void;
76
167
  }
77
-
78
168
  /**
79
- * @since v18.6.0
80
- * @param name The name of the test, which is displayed when reporting test results.
81
- * Default: The `name` property of fn, or `'<anonymous>'` if `fn` does not have a name.
82
- * @param options Configuration options for the test
83
- * @param fn The function under test. If the test uses callbacks, the callback function is
84
- * 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
85
173
  */
86
174
  function it(name?: string, options?: TestOptions, fn?: ItFn): void;
87
175
  function it(name?: string, fn?: ItFn): void;
@@ -93,33 +181,28 @@ declare module 'node:test' {
93
181
  function skip(name?: string, fn?: ItFn): void;
94
182
  function skip(options?: TestOptions, fn?: ItFn): void;
95
183
  function skip(fn?: ItFn): void;
96
-
97
184
  // Shorthand for marking a test as `TODO`, same as `it([name], { todo: true }[, fn])`.
98
185
  function todo(name?: string, options?: TestOptions, fn?: ItFn): void;
99
186
  function todo(name?: string, fn?: ItFn): void;
100
187
  function todo(options?: TestOptions, fn?: ItFn): void;
101
188
  function todo(fn?: ItFn): void;
102
189
  }
103
-
104
190
  /**
105
191
  * The type of a function under test. The first argument to this function is a
106
192
  * {@link TestContext} object. If the test uses callbacks, the callback function is passed as
107
193
  * the second argument.
108
194
  */
109
195
  type TestFn = (t: TestContext, done: (result?: any) => void) => any;
110
-
111
196
  /**
112
197
  * The type of a function under Suite.
113
198
  * If the test uses callbacks, the callback function is passed as an argument
114
199
  */
115
200
  type SuiteFn = (done: (result?: any) => void) => void;
116
-
117
201
  /**
118
202
  * The type of a function under test.
119
203
  * If the test uses callbacks, the callback function is passed as an argument
120
204
  */
121
205
  type ItFn = (done: (result?: any) => void) => any;
122
-
123
206
  interface RunOptions {
124
207
  /**
125
208
  * If a number is provided, then that many files would run in parallel.
@@ -129,41 +212,41 @@ declare module 'node:test' {
129
212
  * @default true
130
213
  */
131
214
  concurrency?: number | boolean | undefined;
132
-
133
215
  /**
134
216
  * An array containing the list of files to run.
135
217
  * If unspecified, the test runner execution model will be used.
136
218
  */
137
219
  files?: readonly string[] | undefined;
138
-
139
220
  /**
140
221
  * Allows aborting an in-progress test execution.
141
222
  * @default undefined
142
223
  */
143
224
  signal?: AbortSignal | undefined;
144
-
145
225
  /**
146
226
  * A number of milliseconds the test will fail after.
147
227
  * If unspecified, subtests inherit this value from their parent.
148
228
  * @default Infinity
149
229
  */
150
230
  timeout?: number | undefined;
151
-
152
231
  /**
153
232
  * Sets inspector port of test child process.
154
233
  * If a nullish value is provided, each process gets its own port,
155
234
  * incremented from the primary's `process.debugPort`.
156
235
  */
157
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[];
158
243
  }
159
-
160
244
  /**
161
- * A successful call of the `run()` method will return a new `TestsStream` object,
162
- * streaming a series of events representing the execution of the tests.
163
- * `TestsStream` will emit events in the order of the tests' definitions.
164
- * @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
165
248
  */
166
- interface TestsStream extends NodeJS.ReadableStream {
249
+ class TestsStream extends Readable implements NodeJS.ReadableStream {
167
250
  addListener(event: 'test:diagnostic', listener: (data: DiagnosticData) => void): this;
168
251
  addListener(event: 'test:fail', listener: (data: TestFail) => void): this;
169
252
  addListener(event: 'test:pass', listener: (data: TestPass) => void): this;
@@ -201,19 +284,16 @@ declare module 'node:test' {
201
284
  prependOnceListener(event: 'test:start', listener: (data: TestStart) => void): this;
202
285
  prependOnceListener(event: string, listener: (...args: any[]) => void): this;
203
286
  }
204
-
205
287
  interface DiagnosticData {
206
288
  /**
207
289
  * The diagnostic message.
208
290
  */
209
291
  message: string;
210
-
211
292
  /**
212
293
  * The nesting level of the test.
213
294
  */
214
295
  nesting: number;
215
296
  }
216
-
217
297
  interface TestFail {
218
298
  /**
219
299
  * Additional execution metadata.
@@ -223,39 +303,32 @@ declare module 'node:test' {
223
303
  * The duration of the test in milliseconds.
224
304
  */
225
305
  duration: number;
226
-
227
306
  /**
228
307
  * The error thrown by the test.
229
308
  */
230
309
  error: Error;
231
310
  };
232
-
233
311
  /**
234
312
  * The test name.
235
313
  */
236
314
  name: string;
237
-
238
315
  /**
239
316
  * The nesting level of the test.
240
317
  */
241
318
  nesting: number;
242
-
243
319
  /**
244
320
  * The ordinal number of the test.
245
321
  */
246
322
  testNumber: number;
247
-
248
323
  /**
249
324
  * Present if `context.todo` is called.
250
325
  */
251
326
  todo?: string | boolean;
252
-
253
327
  /**
254
328
  * Present if `context.skip` is called.
255
329
  */
256
330
  skip?: string | boolean;
257
331
  }
258
-
259
332
  interface TestPass {
260
333
  /**
261
334
  * Additional execution metadata.
@@ -266,63 +339,62 @@ declare module 'node:test' {
266
339
  */
267
340
  duration: number;
268
341
  };
269
-
270
342
  /**
271
343
  * The test name.
272
344
  */
273
345
  name: string;
274
-
275
346
  /**
276
347
  * The nesting level of the test.
277
348
  */
278
349
  nesting: number;
279
-
280
350
  /**
281
351
  * The ordinal number of the test.
282
352
  */
283
353
  testNumber: number;
284
-
285
354
  /**
286
355
  * Present if `context.todo` is called.
287
356
  */
288
357
  todo?: string | boolean;
289
-
290
358
  /**
291
359
  * Present if `context.skip` is called.
292
360
  */
293
361
  skip?: string | boolean;
294
362
  }
295
-
296
363
  interface TestPlan {
297
364
  /**
298
365
  * The nesting level of the test.
299
366
  */
300
367
  nesting: number;
301
-
302
368
  /**
303
369
  * The number of subtests that have ran.
304
370
  */
305
371
  count: number;
306
372
  }
307
-
308
373
  interface TestStart {
309
374
  /**
310
375
  * The test name.
311
376
  */
312
377
  name: string;
313
-
314
378
  /**
315
379
  * The nesting level of the test.
316
380
  */
317
381
  nesting: number;
318
382
  }
319
-
320
383
  /**
321
- * An instance of `TestContext` is passed to each test function in order to interact with the
322
- * test runner. However, the `TestContext` constructor is not exposed as part of the API.
323
- * @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
324
388
  */
325
- 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;
326
398
  /**
327
399
  * This function is used to create a hook running before each subtest of the current test.
328
400
  * @param fn The hook function. If the hook uses callbacks, the callback function is passed as
@@ -331,7 +403,6 @@ declare module 'node:test' {
331
403
  * @since v18.8.0
332
404
  */
333
405
  beforeEach: typeof beforeEach;
334
-
335
406
  /**
336
407
  * This function is used to create a hook that runs after the current test finishes.
337
408
  * @param fn The hook function. If the hook uses callbacks, the callback function is passed as
@@ -340,7 +411,6 @@ declare module 'node:test' {
340
411
  * @since v18.13.0
341
412
  */
342
413
  after: typeof after;
343
-
344
414
  /**
345
415
  * This function is used to create a hook running after each subtest of the current test.
346
416
  * @param fn The hook function. If the hook uses callbacks, the callback function is passed as
@@ -349,54 +419,84 @@ declare module 'node:test' {
349
419
  * @since v18.8.0
350
420
  */
351
421
  afterEach: typeof afterEach;
352
-
353
422
  /**
354
- * This function is used to write diagnostics to the output. Any diagnostic information is
355
- * 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
356
433
  * @param message Message to be reported.
357
- * @since v18.0.0
358
434
  */
359
435
  diagnostic(message: string): void;
360
-
361
436
  /**
362
437
  * The name of the test.
363
- * @since v18.8.0
438
+ * @since v18.8.0, v16.18.0
364
439
  */
365
440
  readonly name: string;
366
-
367
441
  /**
368
- * If `shouldRunOnlyTests` is truthy, the test context will only run tests that have the `only`
369
- * option set. Otherwise, all tests are run. If Node.js was not started with the `--test-only`
370
- * 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
371
458
  * @param shouldRunOnlyTests Whether or not to run `only` tests.
372
- * @since v18.0.0
373
459
  */
374
460
  runOnly(shouldRunOnlyTests: boolean): void;
375
-
376
461
  /**
377
- * Can be used to abort test subtasks when the test has been aborted.
378
- * @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
379
468
  */
380
469
  readonly signal: AbortSignal;
381
-
382
470
  /**
383
- * This function causes the test's output to indicate the test as skipped. If `message` is
384
- * provided, it is included in the output. Calling `skip()` does not terminate execution of
385
- * 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
386
482
  * @param message Optional skip message.
387
- * @since v18.0.0
388
483
  */
389
484
  skip(message?: string): void;
390
-
391
485
  /**
392
- * This function adds a `TODO` directive to the test's output. If `message` is provided, it is
393
- * included in the output. Calling `todo()` does not terminate execution of the test
394
- * 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
395
497
  * @param message Optional `TODO` message.
396
- * @since v18.0.0
397
498
  */
398
499
  todo(message?: string): void;
399
-
400
500
  /**
401
501
  * This function is used to create subtests under the current test. This function behaves in
402
502
  * the same fashion as the top level {@link test} function.
@@ -415,7 +515,6 @@ declare module 'node:test' {
415
515
  */
416
516
  readonly mock: MockTracker;
417
517
  }
418
-
419
518
  interface TestOptions {
420
519
  /**
421
520
  * If a number is provided, then that many tests would run in parallel.
@@ -426,27 +525,23 @@ declare module 'node:test' {
426
525
  * @default false
427
526
  */
428
527
  concurrency?: number | boolean | undefined;
429
-
430
528
  /**
431
529
  * If truthy, and the test context is configured to run `only` tests, then this test will be
432
530
  * run. Otherwise, the test is skipped.
433
531
  * @default false
434
532
  */
435
533
  only?: boolean | undefined;
436
-
437
534
  /**
438
535
  * Allows aborting an in-progress test.
439
536
  * @since v18.8.0
440
537
  */
441
538
  signal?: AbortSignal | undefined;
442
-
443
539
  /**
444
540
  * If truthy, the test is skipped. If a string is provided, that string is displayed in the
445
541
  * test results as the reason for skipping the test.
446
542
  * @default false
447
543
  */
448
544
  skip?: boolean | string | undefined;
449
-
450
545
  /**
451
546
  * A number of milliseconds the test will fail after. If unspecified, subtests inherit this
452
547
  * value from their parent.
@@ -454,7 +549,6 @@ declare module 'node:test' {
454
549
  * @since v18.7.0
455
550
  */
456
551
  timeout?: number | undefined;
457
-
458
552
  /**
459
553
  * If truthy, the test marked as `TODO`. If a string is provided, that string is displayed in
460
554
  * the test results as the reason why the test is `TODO`.
@@ -462,49 +556,77 @@ declare module 'node:test' {
462
556
  */
463
557
  todo?: boolean | string | undefined;
464
558
  }
465
-
466
559
  /**
467
560
  * This function is used to create a hook running before running a suite.
468
- * @param fn The hook function. If the hook uses callbacks, the callback function is passed as
469
- * the second argument. Default: A no-op function.
470
- * @param options Configuration options for the hook.
471
- * @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:
472
573
  */
473
574
  function before(fn?: HookFn, options?: HookOptions): void;
474
-
475
575
  /**
476
- * This function is used to create a hook running after running a suite.
477
- * @param fn The hook function. If the hook uses callbacks, the callback function is passed as
478
- * the second argument. Default: A no-op function.
479
- * @param options Configuration options for the hook.
480
- * @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:
481
589
  */
482
590
  function after(fn?: HookFn, options?: HookOptions): void;
483
-
484
591
  /**
485
- * This function is used to create a hook running before each subtest of the current suite.
486
- * @param fn The hook function. If the hook uses callbacks, the callback function is passed as
487
- * the second argument. Default: A no-op function.
488
- * @param options Configuration options for the hook.
489
- * @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:
490
606
  */
491
607
  function beforeEach(fn?: HookFn, options?: HookOptions): void;
492
-
493
608
  /**
494
- * This function is used to create a hook running after each subtest of the current test.
495
- * @param fn The hook function. If the hook uses callbacks, the callback function is passed as
496
- * the second argument. Default: A no-op function.
497
- * @param options Configuration options for the hook.
498
- * @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:
499
623
  */
500
624
  function afterEach(fn?: HookFn, options?: HookOptions): void;
501
-
502
625
  /**
503
626
  * The hook function. If the hook uses callbacks, the callback function is passed as the
504
627
  * second argument.
505
628
  */
506
629
  type HookFn = (done: (result?: any) => void) => any;
507
-
508
630
  /**
509
631
  * Configuration options for hooks.
510
632
  * @since v18.8.0
@@ -514,7 +636,6 @@ declare module 'node:test' {
514
636
  * Allows aborting an in-progress hook.
515
637
  */
516
638
  signal?: AbortSignal | undefined;
517
-
518
639
  /**
519
640
  * A number of milliseconds the hook will fail after. If unspecified, subtests inherit this
520
641
  * value from their parent.
@@ -522,7 +643,6 @@ declare module 'node:test' {
522
643
  */
523
644
  timeout?: number | undefined;
524
645
  }
525
-
526
646
  interface MockFunctionOptions {
527
647
  /**
528
648
  * The number of times that the mock will use the behavior of `implementation`.
@@ -533,48 +653,106 @@ declare module 'node:test' {
533
653
  */
534
654
  times?: number | undefined;
535
655
  }
536
-
537
656
  interface MockMethodOptions extends MockFunctionOptions {
538
657
  /**
539
658
  * If `true`, `object[methodName]` is treated as a getter.
540
659
  * This option cannot be used with the `setter` option.
541
660
  */
542
661
  getter?: boolean | undefined;
543
-
544
662
  /**
545
663
  * If `true`, `object[methodName]` is treated as a setter.
546
664
  * This option cannot be used with the `getter` option.
547
665
  */
548
666
  setter?: boolean | undefined;
549
667
  }
550
-
551
668
  type Mock<F extends Function> = F & {
552
669
  mock: MockFunctionContext<F>;
553
670
  };
554
-
555
671
  type NoOpFunction = (...args: any[]) => undefined;
556
-
557
672
  type FunctionPropertyNames<T> = {
558
673
  [K in keyof T]: T[K] extends Function ? K : never;
559
674
  }[keyof T];
560
-
561
- 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 {
562
682
  /**
563
683
  * This function is used to create a mock function.
564
- * @param original An optional function to create a mock on.
565
- * @param implementation An optional function used as the mock implementation for `original`.
566
- * This is useful for creating mocks that exhibit one behavior for a specified number of calls and then restore the behavior of `original`.
567
- * @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.
568
718
  */
569
719
  fn<F extends Function = NoOpFunction>(original?: F, options?: MockFunctionOptions): Mock<F>;
570
720
  fn<F extends Function = NoOpFunction, Implementation extends Function = F>(original?: F, implementation?: Implementation, options?: MockFunctionOptions): Mock<F | Implementation>;
571
-
572
721
  /**
573
- * 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
574
750
  * @param object The object whose method is being mocked.
575
751
  * @param methodName The identifier of the method on `object` to mock. If `object[methodName]` is not a function, an error is thrown.
576
752
  * @param implementation An optional function used as the mock implementation for `object[methodName]`.
577
- * @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.
578
756
  */
579
757
  method<
580
758
  MockedObject extends object,
@@ -611,7 +789,8 @@ declare module 'node:test' {
611
789
  ): Mock<Function>;
612
790
 
613
791
  /**
614
- * 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
615
794
  */
616
795
  getter<
617
796
  MockedObject extends object,
@@ -631,9 +810,9 @@ declare module 'node:test' {
631
810
  implementation?: Implementation,
632
811
  options?: MockFunctionOptions,
633
812
  ): Mock<(() => MockedObject[MethodName]) | Implementation>;
634
-
635
813
  /**
636
- * 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
637
816
  */
638
817
  setter<
639
818
  MockedObject extends object,
@@ -653,26 +832,26 @@ declare module 'node:test' {
653
832
  implementation?: Implementation,
654
833
  options?: MockFunctionOptions,
655
834
  ): Mock<((value: MockedObject[MethodName]) => void) | Implementation>;
656
-
657
835
  /**
658
- * This function restores the default behavior of all mocks that were previously created by this `MockTracker`
659
- * and disassociates the mocks from the `MockTracker` instance. Once disassociated, the mocks can still be used,
660
- * 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.
661
840
  *
662
- * After each test completes, this function is called on the test context's `MockTracker`.
663
- * 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
664
844
  */
665
845
  reset(): void;
666
-
667
846
  /**
668
- * This function restores the default behavior of all mocks that were previously created by this `MockTracker`.
669
- * 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
670
851
  */
671
852
  restoreAll(): void;
672
853
  }
673
-
674
854
  const mock: MockTracker;
675
-
676
855
  interface MockFunctionCall<
677
856
  F extends Function,
678
857
  ReturnType = F extends (...args: any) => infer T
@@ -714,47 +893,105 @@ declare module 'node:test' {
714
893
  */
715
894
  this: unknown;
716
895
  }
717
-
718
- 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> {
719
902
  /**
720
- * 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
721
906
  */
722
907
  readonly calls: Array<MockFunctionCall<F>>;
723
-
724
908
  /**
725
- * This function returns the number of times that this mock has been invoked.
726
- * This function is more efficient than checking `ctx.calls.length`
727
- * 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.
728
913
  */
729
914
  callCount(): number;
730
-
731
915
  /**
732
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
733
944
  * @param implementation The function to be used as the mock's new implementation.
734
945
  */
735
946
  mockImplementation(implementation: Function): void;
736
-
737
947
  /**
738
- * This function is used to change the behavior of an existing mock for a single invocation.
739
- * Once invocation `onCall` has occurred, the mock will revert to whatever behavior
740
- * 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
741
980
  * @param implementation The function to be used as the mock's implementation for the invocation number specified by `onCall`.
742
- * @param onCall The invocation number that will use `implementation`.
743
- * 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.
744
982
  */
745
983
  mockImplementationOnce(implementation: Function, onCall?: number): void;
746
-
747
984
  /**
748
985
  * Resets the call history of the mock function.
986
+ * @since v19.3.0, v18.13.0
749
987
  */
750
988
  resetCalls(): void;
751
-
752
989
  /**
753
- * Resets the implementation of the mock function to its original behavior.
754
- * 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
755
993
  */
756
994
  restore(): void;
757
995
  }
758
-
759
996
  export { test as default, run, test, describe, it, before, after, beforeEach, afterEach, mock };
760
997
  }