@types/node 18.11.5 → 20.2.5

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 (97) hide show
  1. node/README.md +2 -2
  2. node/assert.d.ts +86 -75
  3. node/async_hooks.d.ts +66 -37
  4. node/buffer.d.ts +193 -100
  5. node/child_process.d.ts +82 -56
  6. node/cluster.d.ts +12 -12
  7. node/console.d.ts +5 -5
  8. node/crypto.d.ts +233 -235
  9. node/dgram.d.ts +15 -15
  10. node/diagnostics_channel.d.ts +59 -21
  11. node/dns/promises.d.ts +50 -6
  12. node/dns.d.ts +26 -17
  13. node/domain.d.ts +4 -4
  14. node/events.d.ts +111 -65
  15. node/fs/promises.d.ts +99 -47
  16. node/fs.d.ts +244 -73
  17. node/globals.d.ts +6 -3
  18. node/http.d.ts +265 -148
  19. node/http2.d.ts +42 -47
  20. node/https.d.ts +53 -153
  21. node/index.d.ts +2 -1
  22. node/inspector.d.ts +10 -3
  23. node/module.d.ts +6 -4
  24. node/net.d.ts +32 -15
  25. node/os.d.ts +27 -16
  26. node/package.json +9 -4
  27. node/path.d.ts +4 -4
  28. node/perf_hooks.d.ts +28 -15
  29. node/process.d.ts +49 -46
  30. node/punycode.d.ts +1 -1
  31. node/querystring.d.ts +5 -5
  32. node/readline/promises.d.ts +65 -63
  33. node/readline.d.ts +32 -159
  34. node/repl.d.ts +20 -20
  35. node/stream/consumers.d.ts +1 -1
  36. node/stream.d.ts +950 -869
  37. node/string_decoder.d.ts +6 -6
  38. node/test.d.ts +877 -139
  39. node/timers/promises.d.ts +26 -1
  40. node/timers.d.ts +125 -4
  41. node/tls.d.ts +120 -29
  42. node/trace_events.d.ts +20 -9
  43. node/ts4.8/assert.d.ts +86 -75
  44. node/ts4.8/async_hooks.d.ts +66 -37
  45. node/ts4.8/buffer.d.ts +193 -101
  46. node/ts4.8/child_process.d.ts +82 -56
  47. node/ts4.8/cluster.d.ts +12 -12
  48. node/ts4.8/console.d.ts +5 -5
  49. node/ts4.8/crypto.d.ts +230 -233
  50. node/ts4.8/dgram.d.ts +15 -15
  51. node/ts4.8/diagnostics_channel.d.ts +59 -21
  52. node/ts4.8/dns/promises.d.ts +50 -6
  53. node/ts4.8/dns.d.ts +26 -17
  54. node/ts4.8/domain.d.ts +4 -4
  55. node/ts4.8/events.d.ts +111 -65
  56. node/ts4.8/fs/promises.d.ts +97 -46
  57. node/ts4.8/fs.d.ts +244 -73
  58. node/ts4.8/globals.d.ts +21 -12
  59. node/ts4.8/http.d.ts +265 -148
  60. node/ts4.8/http2.d.ts +42 -47
  61. node/ts4.8/https.d.ts +53 -153
  62. node/ts4.8/inspector.d.ts +10 -3
  63. node/ts4.8/module.d.ts +6 -4
  64. node/ts4.8/net.d.ts +33 -16
  65. node/ts4.8/os.d.ts +27 -16
  66. node/ts4.8/path.d.ts +4 -4
  67. node/ts4.8/perf_hooks.d.ts +28 -15
  68. node/ts4.8/process.d.ts +49 -46
  69. node/ts4.8/punycode.d.ts +1 -1
  70. node/ts4.8/querystring.d.ts +5 -5
  71. node/ts4.8/readline/promises.d.ts +65 -63
  72. node/ts4.8/readline.d.ts +32 -159
  73. node/ts4.8/repl.d.ts +20 -20
  74. node/ts4.8/stream/consumers.d.ts +1 -1
  75. node/ts4.8/stream.d.ts +192 -140
  76. node/ts4.8/string_decoder.d.ts +6 -6
  77. node/ts4.8/test.d.ts +877 -139
  78. node/ts4.8/timers/promises.d.ts +26 -1
  79. node/ts4.8/timers.d.ts +125 -4
  80. node/ts4.8/tls.d.ts +120 -29
  81. node/ts4.8/trace_events.d.ts +20 -9
  82. node/ts4.8/tty.d.ts +4 -5
  83. node/ts4.8/url.d.ts +46 -42
  84. node/ts4.8/util.d.ts +325 -123
  85. node/ts4.8/v8.d.ts +253 -14
  86. node/ts4.8/vm.d.ts +417 -32
  87. node/ts4.8/wasi.d.ts +13 -19
  88. node/ts4.8/worker_threads.d.ts +38 -34
  89. node/ts4.8/zlib.d.ts +11 -11
  90. node/tty.d.ts +4 -5
  91. node/url.d.ts +46 -42
  92. node/util.d.ts +325 -123
  93. node/v8.d.ts +253 -14
  94. node/vm.d.ts +417 -32
  95. node/wasi.d.ts +13 -19
  96. node/worker_threads.d.ts +38 -34
  97. 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.0.0/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.2.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 TapStream} 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
- function run(options?: RunOptions): TapStream;
13
-
95
+ function run(options?: RunOptions): TestsStream;
14
96
  /**
15
- * The `test()` function is the value imported from the test module. Each invocation of this
16
- * function results in the creation of a test point in the TAP output.
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 TAP
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,221 +120,438 @@ 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
-
138
+ namespace test {
139
+ export {
140
+ after,
141
+ afterEach,
142
+ before,
143
+ beforeEach,
144
+ describe,
145
+ it,
146
+ run,
147
+ mock,
148
+ test,
149
+ skip,
150
+ todo,
151
+ only
152
+ };
153
+ }
53
154
  /**
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.
155
+ * The `describe()` function imported from the `node:test` module. Each
156
+ * invocation of this function results in the creation of a Subtest.
157
+ * After invocation of top level `describe` functions,
158
+ * all top level tests and suites will execute.
159
+ * @param [name='The name'] The name of the suite, which is displayed when reporting test results.
160
+ * @param options Configuration options for the suite. supports the same options as `test([name][, options][, fn])`.
161
+ * @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.
162
+ * @return `undefined`.
59
163
  */
60
164
  function describe(name?: string, options?: TestOptions, fn?: SuiteFn): void;
61
165
  function describe(name?: string, fn?: SuiteFn): void;
62
166
  function describe(options?: TestOptions, fn?: SuiteFn): void;
63
167
  function describe(fn?: SuiteFn): void;
64
-
168
+ namespace describe {
169
+ /**
170
+ * Shorthand for skipping a suite, same as `describe([name], { skip: true }[, fn])`.
171
+ */
172
+ function skip(name?: string, options?: TestOptions, fn?: SuiteFn): void;
173
+ function skip(name?: string, fn?: SuiteFn): void;
174
+ function skip(options?: TestOptions, fn?: SuiteFn): void;
175
+ function skip(fn?: SuiteFn): void;
176
+ /**
177
+ * Shorthand for marking a suite as `TODO`, same as `describe([name], { todo: true }[, fn])`.
178
+ */
179
+ function todo(name?: string, options?: TestOptions, fn?: SuiteFn): void;
180
+ function todo(name?: string, fn?: SuiteFn): void;
181
+ function todo(options?: TestOptions, fn?: SuiteFn): void;
182
+ function todo(fn?: SuiteFn): void;
183
+ /**
184
+ * Shorthand for marking a suite as `only`, same as `describe([name], { only: true }[, fn])`.
185
+ * @since v18.15.0
186
+ */
187
+ function only(name?: string, options?: TestOptions, fn?: SuiteFn): void;
188
+ function only(name?: string, fn?: SuiteFn): void;
189
+ function only(options?: TestOptions, fn?: SuiteFn): void;
190
+ function only(fn?: SuiteFn): void;
191
+ }
65
192
  /**
66
- * @since v18.6.0
67
- * @param name The name of the test, which is displayed when reporting test results.
68
- * Default: The `name` property of fn, or `'<anonymous>'` if `fn` does not have a name.
69
- * @param options Configuration options for the test
70
- * @param fn The function under test. If the test uses callbacks, the callback function is
71
- * passed as the second argument. Default: A no-op function.
193
+ * Shorthand for `test()`.
194
+ *
195
+ * The `it()` function is imported from the `node:test` module.
196
+ * @since v18.6.0, v16.17.0
72
197
  */
73
- function it(name?: string, options?: TestOptions, fn?: ItFn): void;
74
- function it(name?: string, fn?: ItFn): void;
75
- function it(options?: TestOptions, fn?: ItFn): void;
76
- function it(fn?: ItFn): void;
77
-
198
+ function it(name?: string, options?: TestOptions, fn?: TestFn): void;
199
+ function it(name?: string, fn?: TestFn): void;
200
+ function it(options?: TestOptions, fn?: TestFn): void;
201
+ function it(fn?: TestFn): void;
202
+ namespace it {
203
+ /**
204
+ * Shorthand for skipping a test, same as `it([name], { skip: true }[, fn])`.
205
+ */
206
+ function skip(name?: string, options?: TestOptions, fn?: TestFn): void;
207
+ function skip(name?: string, fn?: TestFn): void;
208
+ function skip(options?: TestOptions, fn?: TestFn): void;
209
+ function skip(fn?: TestFn): void;
210
+ /**
211
+ * Shorthand for marking a test as `TODO`, same as `it([name], { todo: true }[, fn])`.
212
+ */
213
+ function todo(name?: string, options?: TestOptions, fn?: TestFn): void;
214
+ function todo(name?: string, fn?: TestFn): void;
215
+ function todo(options?: TestOptions, fn?: TestFn): void;
216
+ function todo(fn?: TestFn): void;
217
+ /**
218
+ * Shorthand for marking a test as `only`, same as `it([name], { only: true }[, fn])`.
219
+ * @since v18.15.0
220
+ */
221
+ function only(name?: string, options?: TestOptions, fn?: TestFn): void;
222
+ function only(name?: string, fn?: TestFn): void;
223
+ function only(options?: TestOptions, fn?: TestFn): void;
224
+ function only(fn?: TestFn): void;
225
+ }
226
+ /**
227
+ * Shorthand for skipping a test, same as `test([name], { skip: true }[, fn])`.
228
+ * @since v20.2.0
229
+ */
230
+ function skip(name?: string, options?: TestOptions, fn?: TestFn): void;
231
+ function skip(name?: string, fn?: TestFn): void;
232
+ function skip(options?: TestOptions, fn?: TestFn): void;
233
+ function skip(fn?: TestFn): void;
234
+ /**
235
+ * Shorthand for marking a test as `TODO`, same as `test([name], { todo: true }[, fn])`.
236
+ * @since v20.2.0
237
+ */
238
+ function todo(name?: string, options?: TestOptions, fn?: TestFn): void;
239
+ function todo(name?: string, fn?: TestFn): void;
240
+ function todo(options?: TestOptions, fn?: TestFn): void;
241
+ function todo(fn?: TestFn): void;
242
+ /**
243
+ * Shorthand for marking a test as `only`, same as `test([name], { only: true }[, fn])`.
244
+ * @since v20.2.0
245
+ */
246
+ function only(name?: string, options?: TestOptions, fn?: TestFn): void;
247
+ function only(name?: string, fn?: TestFn): void;
248
+ function only(options?: TestOptions, fn?: TestFn): void;
249
+ function only(fn?: TestFn): void;
78
250
  /**
79
251
  * The type of a function under test. The first argument to this function is a
80
252
  * {@link TestContext} object. If the test uses callbacks, the callback function is passed as
81
253
  * the second argument.
82
254
  */
83
255
  type TestFn = (t: TestContext, done: (result?: any) => void) => any;
84
-
85
256
  /**
86
257
  * The type of a function under Suite.
87
258
  * If the test uses callbacks, the callback function is passed as an argument
88
259
  */
89
260
  type SuiteFn = (done: (result?: any) => void) => void;
90
-
91
- /**
92
- * The type of a function under test.
93
- * If the test uses callbacks, the callback function is passed as an argument
94
- */
95
- type ItFn = (done: (result?: any) => void) => any;
96
-
97
261
  interface RunOptions {
98
262
  /**
99
- * @default false
263
+ * If a number is provided, then that many files would run in parallel.
264
+ * If truthy, it would run (number of cpu cores - 1) files in parallel.
265
+ * If falsy, it would only run one file at a time.
266
+ * If unspecified, subtests inherit this value from their parent.
267
+ * @default true
100
268
  */
101
- concurrency?: number | boolean;
102
-
269
+ concurrency?: number | boolean | undefined;
103
270
  /**
104
- * An array containing the list of files to run. If unspecified, the test runner execution model will be used.
271
+ * An array containing the list of files to run.
272
+ * If unspecified, the test runner execution model will be used.
105
273
  */
106
- files?: readonly string[];
107
-
274
+ files?: readonly string[] | undefined;
108
275
  /**
109
- * Allows aborting an in-progress test.
276
+ * Allows aborting an in-progress test execution.
110
277
  * @default undefined
111
278
  */
112
- signal?: AbortSignal;
113
-
279
+ signal?: AbortSignal | undefined;
114
280
  /**
115
- * A number of milliseconds the test will fail after. If unspecified, subtests inherit this
116
- * value from their parent.
281
+ * A number of milliseconds the test will fail after.
282
+ * If unspecified, subtests inherit this value from their parent.
117
283
  * @default Infinity
118
284
  */
119
- timeout?: number;
285
+ timeout?: number | undefined;
286
+ /**
287
+ * Sets inspector port of test child process.
288
+ * If a nullish value is provided, each process gets its own port,
289
+ * incremented from the primary's `process.debugPort`.
290
+ */
291
+ inspectPort?: number | (() => number) | undefined;
292
+ /**
293
+ * That can be used to only run tests whose name matches the provided pattern.
294
+ * Test name patterns are interpreted as JavaScript regular expressions.
295
+ * For each test that is executed, any corresponding test hooks, such as `beforeEach()`, are also run.
296
+ */
297
+ testNamePatterns?: string | RegExp | string[] | RegExp[];
120
298
  }
121
-
122
299
  /**
123
- * A successful call of the run() method will return a new TapStream object, streaming a TAP output.
124
- * TapStream will emit events in the order of the tests' definitions.
125
- * @since v18.9.0
300
+ * 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
301
+ * order of the tests definition
302
+ * @since v18.9.0, v16.19.0
126
303
  */
127
- interface TapStream extends NodeJS.ReadableStream {
128
- addListener(event: 'test:diagnostic', listener: (message: string) => void): this;
304
+ class TestsStream extends Readable implements NodeJS.ReadableStream {
305
+ addListener(event: 'test:diagnostic', listener: (data: DiagnosticData) => void): this;
129
306
  addListener(event: 'test:fail', listener: (data: TestFail) => void): this;
130
307
  addListener(event: 'test:pass', listener: (data: TestPass) => void): this;
308
+ addListener(event: 'test:plan', listener: (data: TestPlan) => void): this;
309
+ addListener(event: 'test:start', listener: (data: TestStart) => void): this;
131
310
  addListener(event: string, listener: (...args: any[]) => void): this;
132
- emit(event: 'test:diagnostic', message: string): boolean;
311
+ emit(event: 'test:diagnostic', data: DiagnosticData): boolean;
133
312
  emit(event: 'test:fail', data: TestFail): boolean;
134
313
  emit(event: 'test:pass', data: TestPass): boolean;
314
+ emit(event: 'test:plan', data: TestPlan): boolean;
315
+ emit(event: 'test:start', data: TestStart): boolean;
135
316
  emit(event: string | symbol, ...args: any[]): boolean;
136
- on(event: 'test:diagnostic', listener: (message: string) => void): this;
317
+ on(event: 'test:diagnostic', listener: (data: DiagnosticData) => void): this;
137
318
  on(event: 'test:fail', listener: (data: TestFail) => void): this;
138
319
  on(event: 'test:pass', listener: (data: TestPass) => void): this;
320
+ on(event: 'test:plan', listener: (data: TestPlan) => void): this;
321
+ on(event: 'test:start', listener: (data: TestStart) => void): this;
139
322
  on(event: string, listener: (...args: any[]) => void): this;
140
- once(event: 'test:diagnostic', listener: (message: string) => void): this;
323
+ once(event: 'test:diagnostic', listener: (data: DiagnosticData) => void): this;
141
324
  once(event: 'test:fail', listener: (data: TestFail) => void): this;
142
325
  once(event: 'test:pass', listener: (data: TestPass) => void): this;
326
+ once(event: 'test:plan', listener: (data: TestPlan) => void): this;
327
+ once(event: 'test:start', listener: (data: TestStart) => void): this;
143
328
  once(event: string, listener: (...args: any[]) => void): this;
144
- prependListener(event: 'test:diagnostic', listener: (message: string) => void): this;
329
+ prependListener(event: 'test:diagnostic', listener: (data: DiagnosticData) => void): this;
145
330
  prependListener(event: 'test:fail', listener: (data: TestFail) => void): this;
146
331
  prependListener(event: 'test:pass', listener: (data: TestPass) => void): this;
332
+ prependListener(event: 'test:plan', listener: (data: TestPlan) => void): this;
333
+ prependListener(event: 'test:start', listener: (data: TestStart) => void): this;
147
334
  prependListener(event: string, listener: (...args: any[]) => void): this;
148
- prependOnceListener(event: 'test:diagnostic', listener: (message: string) => void): this;
335
+ prependOnceListener(event: 'test:diagnostic', listener: (data: DiagnosticData) => void): this;
149
336
  prependOnceListener(event: 'test:fail', listener: (data: TestFail) => void): this;
150
337
  prependOnceListener(event: 'test:pass', listener: (data: TestPass) => void): this;
338
+ prependOnceListener(event: 'test:plan', listener: (data: TestPlan) => void): this;
339
+ prependOnceListener(event: 'test:start', listener: (data: TestStart) => void): this;
151
340
  prependOnceListener(event: string, listener: (...args: any[]) => void): this;
152
341
  }
153
-
154
- interface TestFail {
342
+ interface DiagnosticData {
155
343
  /**
156
- * The test duration.
344
+ * The diagnostic message.
157
345
  */
158
- duration: number;
159
-
346
+ message: string;
160
347
  /**
161
- * The failure casing test to fail.
348
+ * The nesting level of the test.
162
349
  */
163
- error: Error;
164
-
350
+ nesting: number;
351
+ }
352
+ interface TestFail {
353
+ /**
354
+ * Additional execution metadata.
355
+ */
356
+ details: {
357
+ /**
358
+ * The duration of the test in milliseconds.
359
+ */
360
+ duration: number;
361
+ /**
362
+ * The error thrown by the test.
363
+ */
364
+ error: Error;
365
+ };
165
366
  /**
166
367
  * The test name.
167
368
  */
168
369
  name: string;
169
-
370
+ /**
371
+ * The nesting level of the test.
372
+ */
373
+ nesting: number;
170
374
  /**
171
375
  * The ordinal number of the test.
172
376
  */
173
377
  testNumber: number;
174
-
175
378
  /**
176
379
  * Present if `context.todo` is called.
177
380
  */
178
- todo?: string;
179
-
381
+ todo?: string | boolean;
180
382
  /**
181
383
  * Present if `context.skip` is called.
182
384
  */
183
- skip?: string;
385
+ skip?: string | boolean;
184
386
  }
185
-
186
387
  interface TestPass {
187
388
  /**
188
- * The test duration.
389
+ * Additional execution metadata.
189
390
  */
190
- duration: number;
191
-
391
+ details: {
392
+ /**
393
+ * The duration of the test in milliseconds.
394
+ */
395
+ duration: number;
396
+ };
192
397
  /**
193
398
  * The test name.
194
399
  */
195
400
  name: string;
196
-
401
+ /**
402
+ * The nesting level of the test.
403
+ */
404
+ nesting: number;
197
405
  /**
198
406
  * The ordinal number of the test.
199
407
  */
200
408
  testNumber: number;
201
-
202
409
  /**
203
410
  * Present if `context.todo` is called.
204
411
  */
205
- todo?: string;
206
-
412
+ todo?: string | boolean;
207
413
  /**
208
414
  * Present if `context.skip` is called.
209
415
  */
210
- skip?: string;
416
+ skip?: string | boolean;
417
+ }
418
+ interface TestPlan {
419
+ /**
420
+ * The nesting level of the test.
421
+ */
422
+ nesting: number;
423
+ /**
424
+ * The number of subtests that have ran.
425
+ */
426
+ count: number;
427
+ }
428
+ interface TestStart {
429
+ /**
430
+ * The test name.
431
+ */
432
+ name: string;
433
+ /**
434
+ * The nesting level of the test.
435
+ */
436
+ nesting: number;
211
437
  }
212
-
213
438
  /**
214
- * An instance of `TestContext` is passed to each test function in order to interact with the
215
- * test runner. However, the `TestContext` constructor is not exposed as part of the API.
216
- * @since v18.0.0
439
+ * An instance of `TestContext` is passed to each test function in order to
440
+ * interact with the test runner. However, the `TestContext` constructor is not
441
+ * exposed as part of the API.
442
+ * @since v18.0.0, v16.17.0
217
443
  */
218
- interface TestContext {
444
+ class TestContext {
219
445
  /**
220
- * This function is used to write TAP diagnostics to the output. Any diagnostic information is
221
- * included at the end of the test's results. This function does not return a value.
222
- * @param message Message to be displayed as a TAP diagnostic.
223
- * @since v18.0.0
446
+ * This function is used to create a hook running before subtest of the current test.
447
+ * @param fn The hook function. If the hook uses callbacks, the callback function is passed as
448
+ * the second argument. Default: A no-op function.
449
+ * @param options Configuration options for the hook.
450
+ * @since v20.1.0
451
+ */
452
+ before: typeof before;
453
+ /**
454
+ * This function is used to create a hook running before each subtest of the current test.
455
+ * @param fn The hook function. If the hook uses callbacks, the callback function is passed as
456
+ * the second argument. Default: A no-op function.
457
+ * @param options Configuration options for the hook.
458
+ * @since v18.8.0
459
+ */
460
+ beforeEach: typeof beforeEach;
461
+ /**
462
+ * This function is used to create a hook that runs after the current test finishes.
463
+ * @param fn The hook function. If the hook uses callbacks, the callback function is passed as
464
+ * the second argument. Default: A no-op function.
465
+ * @param options Configuration options for the hook.
466
+ * @since v18.13.0
467
+ */
468
+ after: typeof after;
469
+ /**
470
+ * This function is used to create a hook running after each subtest of the current test.
471
+ * @param fn The hook function. If the hook uses callbacks, the callback function is passed as
472
+ * the second argument. Default: A no-op function.
473
+ * @param options Configuration options for the hook.
474
+ * @since v18.8.0
475
+ */
476
+ afterEach: typeof afterEach;
477
+ /**
478
+ * This function is used to write diagnostics to the output. Any diagnostic
479
+ * information is included at the end of the test's results. This function does
480
+ * not return a value.
481
+ *
482
+ * ```js
483
+ * test('top level test', (t) => {
484
+ * t.diagnostic('A diagnostic message');
485
+ * });
486
+ * ```
487
+ * @since v18.0.0, v16.17.0
488
+ * @param message Message to be reported.
224
489
  */
225
490
  diagnostic(message: string): void;
226
-
227
491
  /**
228
- * If `shouldRunOnlyTests` is truthy, the test context will only run tests that have the `only`
229
- * option set. Otherwise, all tests are run. If Node.js was not started with the `--test-only`
230
- * command-line option, this function is a no-op.
492
+ * The name of the test.
493
+ * @since v18.8.0, v16.18.0
494
+ */
495
+ readonly name: string;
496
+ /**
497
+ * If `shouldRunOnlyTests` is truthy, the test context will only run tests that
498
+ * have the `only` option set. Otherwise, all tests are run. If Node.js was not
499
+ * started with the `--test-only` command-line option, this function is a
500
+ * no-op.
501
+ *
502
+ * ```js
503
+ * test('top level test', (t) => {
504
+ * // The test context can be set to run subtests with the 'only' option.
505
+ * t.runOnly(true);
506
+ * return Promise.all([
507
+ * t.test('this subtest is now skipped'),
508
+ * t.test('this subtest is run', { only: true }),
509
+ * ]);
510
+ * });
511
+ * ```
512
+ * @since v18.0.0, v16.17.0
231
513
  * @param shouldRunOnlyTests Whether or not to run `only` tests.
232
- * @since v18.0.0
233
514
  */
234
515
  runOnly(shouldRunOnlyTests: boolean): void;
235
-
236
516
  /**
237
- * This function causes the test's output to indicate the test as skipped. If `message` is
238
- * provided, it is included in the TAP output. Calling `skip()` does not terminate execution of
239
- * the test function. This function does not return a value.
240
- * @param message Optional skip message to be displayed in TAP output.
241
- * @since v18.0.0
517
+ * ```js
518
+ * test('top level test', async (t) => {
519
+ * await fetch('some/uri', { signal: t.signal });
520
+ * });
521
+ * ```
522
+ * @since v18.7.0, v16.17.0
523
+ */
524
+ readonly signal: AbortSignal;
525
+ /**
526
+ * 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
527
+ * not terminate execution of the test function. This function does not return a
528
+ * value.
529
+ *
530
+ * ```js
531
+ * test('top level test', (t) => {
532
+ * // Make sure to return here as well if the test contains additional logic.
533
+ * t.skip('this is skipped');
534
+ * });
535
+ * ```
536
+ * @since v18.0.0, v16.17.0
537
+ * @param message Optional skip message.
242
538
  */
243
539
  skip(message?: string): void;
244
-
245
540
  /**
246
- * This function adds a `TODO` directive to the test's output. If `message` is provided, it is
247
- * included in the TAP output. Calling `todo()` does not terminate execution of the test
248
- * function. This function does not return a value.
249
- * @param message Optional `TODO` message to be displayed in TAP output.
250
- * @since v18.0.0
541
+ * This function adds a `TODO` directive to the test's output. If `message` is
542
+ * provided, it is included in the output. Calling `todo()` does not terminate
543
+ * execution of the test function. This function does not return a value.
544
+ *
545
+ * ```js
546
+ * test('top level test', (t) => {
547
+ * // This test is marked as `TODO`
548
+ * t.todo('this is a todo');
549
+ * });
550
+ * ```
551
+ * @since v18.0.0, v16.17.0
552
+ * @param message Optional `TODO` message.
251
553
  */
252
554
  todo(message?: string): void;
253
-
254
555
  /**
255
556
  * This function is used to create subtests under the current test. This function behaves in
256
557
  * the same fashion as the top level {@link test} function.
@@ -264,51 +565,488 @@ declare module 'node:test' {
264
565
  * @returns A {@link Promise} resolved with `undefined` once the test completes.
265
566
  */
266
567
  test: typeof test;
568
+ /**
569
+ * Each test provides its own MockTracker instance.
570
+ */
571
+ readonly mock: MockTracker;
267
572
  }
268
-
269
573
  interface TestOptions {
270
574
  /**
271
- * The number of tests that can be run at the same time. If unspecified, subtests inherit this
272
- * value from their parent.
273
- * @default 1
575
+ * If a number is provided, then that many tests would run in parallel.
576
+ * If truthy, it would run (number of cpu cores - 1) tests in parallel.
577
+ * For subtests, it will be `Infinity` tests in parallel.
578
+ * If falsy, it would only run one test at a time.
579
+ * If unspecified, subtests inherit this value from their parent.
580
+ * @default false
274
581
  */
275
- concurrency?: number;
276
-
582
+ concurrency?: number | boolean | undefined;
277
583
  /**
278
584
  * If truthy, and the test context is configured to run `only` tests, then this test will be
279
585
  * run. Otherwise, the test is skipped.
280
586
  * @default false
281
587
  */
282
- only?: boolean;
283
-
588
+ only?: boolean | undefined;
284
589
  /**
285
590
  * Allows aborting an in-progress test.
286
591
  * @since v18.8.0
287
592
  */
288
- signal?: AbortSignal;
289
-
593
+ signal?: AbortSignal | undefined;
290
594
  /**
291
595
  * If truthy, the test is skipped. If a string is provided, that string is displayed in the
292
596
  * test results as the reason for skipping the test.
293
597
  * @default false
294
598
  */
295
- skip?: boolean | string;
296
-
599
+ skip?: boolean | string | undefined;
297
600
  /**
298
601
  * A number of milliseconds the test will fail after. If unspecified, subtests inherit this
299
602
  * value from their parent.
300
603
  * @default Infinity
301
604
  * @since v18.7.0
302
605
  */
303
- timeout?: number;
304
-
606
+ timeout?: number | undefined;
305
607
  /**
306
608
  * If truthy, the test marked as `TODO`. If a string is provided, that string is displayed in
307
609
  * the test results as the reason why the test is `TODO`.
308
610
  * @default false
309
611
  */
310
- todo?: boolean | string;
612
+ todo?: boolean | string | undefined;
613
+ }
614
+ /**
615
+ * This function is used to create a hook running before running a suite.
616
+ *
617
+ * ```js
618
+ * describe('tests', async () => {
619
+ * before(() => console.log('about to run some test'));
620
+ * it('is a subtest', () => {
621
+ * assert.ok('some relevant assertion here');
622
+ * });
623
+ * });
624
+ * ```
625
+ * @since v18.8.0, v16.18.0
626
+ * @param [fn='A no-op function'] The hook function. If the hook uses callbacks, the callback function is passed as the second argument.
627
+ * @param options Configuration options for the hook. The following properties are supported:
628
+ */
629
+ function before(fn?: HookFn, options?: HookOptions): void;
630
+ /**
631
+ * This function is used to create a hook running after running a suite.
632
+ *
633
+ * ```js
634
+ * describe('tests', async () => {
635
+ * after(() => console.log('finished running tests'));
636
+ * it('is a subtest', () => {
637
+ * assert.ok('some relevant assertion here');
638
+ * });
639
+ * });
640
+ * ```
641
+ * @since v18.8.0, v16.18.0
642
+ * @param [fn='A no-op function'] The hook function. If the hook uses callbacks, the callback function is passed as the second argument.
643
+ * @param options Configuration options for the hook. The following properties are supported:
644
+ */
645
+ function after(fn?: HookFn, options?: HookOptions): void;
646
+ /**
647
+ * This function is used to create a hook running
648
+ * before each subtest of the current suite.
649
+ *
650
+ * ```js
651
+ * describe('tests', async () => {
652
+ * beforeEach(() => console.log('about to run a test'));
653
+ * it('is a subtest', () => {
654
+ * assert.ok('some relevant assertion here');
655
+ * });
656
+ * });
657
+ * ```
658
+ * @since v18.8.0, v16.18.0
659
+ * @param [fn='A no-op function'] The hook function. If the hook uses callbacks, the callback function is passed as the second argument.
660
+ * @param options Configuration options for the hook. The following properties are supported:
661
+ */
662
+ function beforeEach(fn?: HookFn, options?: HookOptions): void;
663
+ /**
664
+ * This function is used to create a hook running
665
+ * after each subtest of the current test.
666
+ *
667
+ * ```js
668
+ * describe('tests', async () => {
669
+ * afterEach(() => console.log('finished running a test'));
670
+ * it('is a subtest', () => {
671
+ * assert.ok('some relevant assertion here');
672
+ * });
673
+ * });
674
+ * ```
675
+ * @since v18.8.0, v16.18.0
676
+ * @param [fn='A no-op function'] The hook function. If the hook uses callbacks, the callback function is passed as the second argument.
677
+ * @param options Configuration options for the hook. The following properties are supported:
678
+ */
679
+ function afterEach(fn?: HookFn, options?: HookOptions): void;
680
+ /**
681
+ * The hook function. If the hook uses callbacks, the callback function is passed as the
682
+ * second argument.
683
+ */
684
+ type HookFn = (done: (result?: any) => void) => any;
685
+ /**
686
+ * Configuration options for hooks.
687
+ * @since v18.8.0
688
+ */
689
+ interface HookOptions {
690
+ /**
691
+ * Allows aborting an in-progress hook.
692
+ */
693
+ signal?: AbortSignal | undefined;
694
+ /**
695
+ * A number of milliseconds the hook will fail after. If unspecified, subtests inherit this
696
+ * value from their parent.
697
+ * @default Infinity
698
+ */
699
+ timeout?: number | undefined;
700
+ }
701
+ interface MockFunctionOptions {
702
+ /**
703
+ * The number of times that the mock will use the behavior of `implementation`.
704
+ * Once the mock function has been called `times` times,
705
+ * it will automatically restore the behavior of `original`.
706
+ * This value must be an integer greater than zero.
707
+ * @default Infinity
708
+ */
709
+ times?: number | undefined;
311
710
  }
711
+ interface MockMethodOptions extends MockFunctionOptions {
712
+ /**
713
+ * If `true`, `object[methodName]` is treated as a getter.
714
+ * This option cannot be used with the `setter` option.
715
+ */
716
+ getter?: boolean | undefined;
717
+ /**
718
+ * If `true`, `object[methodName]` is treated as a setter.
719
+ * This option cannot be used with the `getter` option.
720
+ */
721
+ setter?: boolean | undefined;
722
+ }
723
+ type Mock<F extends Function> = F & {
724
+ mock: MockFunctionContext<F>;
725
+ };
726
+ type NoOpFunction = (...args: any[]) => undefined;
727
+ type FunctionPropertyNames<T> = {
728
+ [K in keyof T]: T[K] extends Function ? K : never;
729
+ }[keyof T];
730
+ /**
731
+ * The `MockTracker` class is used to manage mocking functionality. The test runner
732
+ * module provides a top level `mock` export which is a `MockTracker` instance.
733
+ * Each test also provides its own `MockTracker` instance via the test context's`mock` property.
734
+ * @since v19.1.0, v18.13.0
735
+ */
736
+ class MockTracker {
737
+ /**
738
+ * This function is used to create a mock function.
739
+ *
740
+ * The following example creates a mock function that increments a counter by one
741
+ * on each invocation. The `times` option is used to modify the mock behavior such
742
+ * that the first two invocations add two to the counter instead of one.
743
+ *
744
+ * ```js
745
+ * test('mocks a counting function', (t) => {
746
+ * let cnt = 0;
747
+ *
748
+ * function addOne() {
749
+ * cnt++;
750
+ * return cnt;
751
+ * }
752
+ *
753
+ * function addTwo() {
754
+ * cnt += 2;
755
+ * return cnt;
756
+ * }
757
+ *
758
+ * const fn = t.mock.fn(addOne, addTwo, { times: 2 });
759
+ *
760
+ * assert.strictEqual(fn(), 2);
761
+ * assert.strictEqual(fn(), 4);
762
+ * assert.strictEqual(fn(), 5);
763
+ * assert.strictEqual(fn(), 6);
764
+ * });
765
+ * ```
766
+ * @since v19.1.0, v18.13.0
767
+ * @param [original='A no-op function'] An optional function to create a mock on.
768
+ * @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
769
+ * then restore the behavior of `original`.
770
+ * @param options Optional configuration options for the mock function. The following properties are supported:
771
+ * @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
772
+ * behavior of the mocked function.
773
+ */
774
+ fn<F extends Function = NoOpFunction>(original?: F, options?: MockFunctionOptions): Mock<F>;
775
+ fn<F extends Function = NoOpFunction, Implementation extends Function = F>(original?: F, implementation?: Implementation, options?: MockFunctionOptions): Mock<F | Implementation>;
776
+ /**
777
+ * This function is used to create a mock on an existing object method. The
778
+ * following example demonstrates how a mock is created on an existing object
779
+ * method.
780
+ *
781
+ * ```js
782
+ * test('spies on an object method', (t) => {
783
+ * const number = {
784
+ * value: 5,
785
+ * subtract(a) {
786
+ * return this.value - a;
787
+ * },
788
+ * };
789
+ *
790
+ * t.mock.method(number, 'subtract');
791
+ * assert.strictEqual(number.subtract.mock.calls.length, 0);
792
+ * assert.strictEqual(number.subtract(3), 2);
793
+ * assert.strictEqual(number.subtract.mock.calls.length, 1);
794
+ *
795
+ * const call = number.subtract.mock.calls[0];
796
+ *
797
+ * assert.deepStrictEqual(call.arguments, [3]);
798
+ * assert.strictEqual(call.result, 2);
799
+ * assert.strictEqual(call.error, undefined);
800
+ * assert.strictEqual(call.target, undefined);
801
+ * assert.strictEqual(call.this, number);
802
+ * });
803
+ * ```
804
+ * @since v19.1.0, v18.13.0
805
+ * @param object The object whose method is being mocked.
806
+ * @param methodName The identifier of the method on `object` to mock. If `object[methodName]` is not a function, an error is thrown.
807
+ * @param implementation An optional function used as the mock implementation for `object[methodName]`.
808
+ * @param options Optional configuration options for the mock method. The following properties are supported:
809
+ * @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
810
+ * behavior of the mocked method.
811
+ */
812
+ method<
813
+ MockedObject extends object,
814
+ MethodName extends FunctionPropertyNames<MockedObject>,
815
+ >(
816
+ object: MockedObject,
817
+ methodName: MethodName,
818
+ options?: MockFunctionOptions,
819
+ ): MockedObject[MethodName] extends Function
820
+ ? Mock<MockedObject[MethodName]>
821
+ : never;
822
+ method<
823
+ MockedObject extends object,
824
+ MethodName extends FunctionPropertyNames<MockedObject>,
825
+ Implementation extends Function,
826
+ >(
827
+ object: MockedObject,
828
+ methodName: MethodName,
829
+ implementation: Implementation,
830
+ options?: MockFunctionOptions,
831
+ ): MockedObject[MethodName] extends Function
832
+ ? Mock<MockedObject[MethodName] | Implementation>
833
+ : never;
834
+ method<MockedObject extends object>(
835
+ object: MockedObject,
836
+ methodName: keyof MockedObject,
837
+ options: MockMethodOptions,
838
+ ): Mock<Function>;
839
+ method<MockedObject extends object>(
840
+ object: MockedObject,
841
+ methodName: keyof MockedObject,
842
+ implementation: Function,
843
+ options: MockMethodOptions,
844
+ ): Mock<Function>;
312
845
 
313
- export { test as default, run, test, describe, it };
846
+ /**
847
+ * This function is syntax sugar for `MockTracker.method` with `options.getter`set to `true`.
848
+ * @since v19.3.0, v18.13.0
849
+ */
850
+ getter<
851
+ MockedObject extends object,
852
+ MethodName extends keyof MockedObject,
853
+ >(
854
+ object: MockedObject,
855
+ methodName: MethodName,
856
+ options?: MockFunctionOptions,
857
+ ): Mock<() => MockedObject[MethodName]>;
858
+ getter<
859
+ MockedObject extends object,
860
+ MethodName extends keyof MockedObject,
861
+ Implementation extends Function,
862
+ >(
863
+ object: MockedObject,
864
+ methodName: MethodName,
865
+ implementation?: Implementation,
866
+ options?: MockFunctionOptions,
867
+ ): Mock<(() => MockedObject[MethodName]) | Implementation>;
868
+ /**
869
+ * This function is syntax sugar for `MockTracker.method` with `options.setter`set to `true`.
870
+ * @since v19.3.0, v18.13.0
871
+ */
872
+ setter<
873
+ MockedObject extends object,
874
+ MethodName extends keyof MockedObject,
875
+ >(
876
+ object: MockedObject,
877
+ methodName: MethodName,
878
+ options?: MockFunctionOptions,
879
+ ): Mock<(value: MockedObject[MethodName]) => void>;
880
+ setter<
881
+ MockedObject extends object,
882
+ MethodName extends keyof MockedObject,
883
+ Implementation extends Function,
884
+ >(
885
+ object: MockedObject,
886
+ methodName: MethodName,
887
+ implementation?: Implementation,
888
+ options?: MockFunctionOptions,
889
+ ): Mock<((value: MockedObject[MethodName]) => void) | Implementation>;
890
+ /**
891
+ * This function restores the default behavior of all mocks that were previously
892
+ * 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
893
+ * used to reset their behavior or
894
+ * otherwise interact with them.
895
+ *
896
+ * After each test completes, this function is called on the test context's`MockTracker`. If the global `MockTracker` is used extensively, calling this
897
+ * function manually is recommended.
898
+ * @since v19.1.0, v18.13.0
899
+ */
900
+ reset(): void;
901
+ /**
902
+ * This function restores the default behavior of all mocks that were previously
903
+ * created by this `MockTracker`. Unlike `mock.reset()`, `mock.restoreAll()` does
904
+ * not disassociate the mocks from the `MockTracker` instance.
905
+ * @since v19.1.0, v18.13.0
906
+ */
907
+ restoreAll(): void;
908
+ }
909
+ const mock: MockTracker;
910
+ interface MockFunctionCall<
911
+ F extends Function,
912
+ ReturnType = F extends (...args: any) => infer T
913
+ ? T
914
+ : F extends abstract new (...args: any) => infer T
915
+ ? T
916
+ : unknown,
917
+ Args = F extends (...args: infer Y) => any
918
+ ? Y
919
+ : F extends abstract new (...args: infer Y) => any
920
+ ? Y
921
+ : unknown[],
922
+ > {
923
+ /**
924
+ * An array of the arguments passed to the mock function.
925
+ */
926
+ arguments: Args;
927
+ /**
928
+ * If the mocked function threw then this property contains the thrown value.
929
+ */
930
+ error: unknown | undefined;
931
+ /**
932
+ * The value returned by the mocked function.
933
+ *
934
+ * If the mocked function threw, it will be `undefined`.
935
+ */
936
+ result: ReturnType | undefined;
937
+ /**
938
+ * An `Error` object whose stack can be used to determine the callsite of the mocked function invocation.
939
+ */
940
+ stack: Error;
941
+ /**
942
+ * If the mocked function is a constructor, this field contains the class being constructed.
943
+ * Otherwise this will be `undefined`.
944
+ */
945
+ target: F extends abstract new (...args: any) => any ? F : undefined;
946
+ /**
947
+ * The mocked function's `this` value.
948
+ */
949
+ this: unknown;
950
+ }
951
+ /**
952
+ * The `MockFunctionContext` class is used to inspect or manipulate the behavior of
953
+ * mocks created via the `MockTracker` APIs.
954
+ * @since v19.1.0, v18.13.0
955
+ */
956
+ class MockFunctionContext<F extends Function> {
957
+ /**
958
+ * A getter that returns a copy of the internal array used to track calls to the
959
+ * mock. Each entry in the array is an object with the following properties.
960
+ * @since v19.1.0, v18.13.0
961
+ */
962
+ readonly calls: Array<MockFunctionCall<F>>;
963
+ /**
964
+ * This function returns the number of times that this mock has been invoked. This
965
+ * 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.
966
+ * @since v19.1.0, v18.13.0
967
+ * @return The number of times that this mock has been invoked.
968
+ */
969
+ callCount(): number;
970
+ /**
971
+ * This function is used to change the behavior of an existing mock.
972
+ *
973
+ * The following example creates a mock function using `t.mock.fn()`, calls the
974
+ * mock function, and then changes the mock implementation to a different function.
975
+ *
976
+ * ```js
977
+ * test('changes a mock behavior', (t) => {
978
+ * let cnt = 0;
979
+ *
980
+ * function addOne() {
981
+ * cnt++;
982
+ * return cnt;
983
+ * }
984
+ *
985
+ * function addTwo() {
986
+ * cnt += 2;
987
+ * return cnt;
988
+ * }
989
+ *
990
+ * const fn = t.mock.fn(addOne);
991
+ *
992
+ * assert.strictEqual(fn(), 1);
993
+ * fn.mock.mockImplementation(addTwo);
994
+ * assert.strictEqual(fn(), 3);
995
+ * assert.strictEqual(fn(), 5);
996
+ * });
997
+ * ```
998
+ * @since v19.1.0, v18.13.0
999
+ * @param implementation The function to be used as the mock's new implementation.
1000
+ */
1001
+ mockImplementation(implementation: Function): void;
1002
+ /**
1003
+ * This function is used to change the behavior of an existing mock for a single
1004
+ * invocation. Once invocation `onCall` has occurred, the mock will revert to
1005
+ * whatever behavior it would have used had `mockImplementationOnce()` not been
1006
+ * called.
1007
+ *
1008
+ * The following example creates a mock function using `t.mock.fn()`, calls the
1009
+ * mock function, changes the mock implementation to a different function for the
1010
+ * next invocation, and then resumes its previous behavior.
1011
+ *
1012
+ * ```js
1013
+ * test('changes a mock behavior once', (t) => {
1014
+ * let cnt = 0;
1015
+ *
1016
+ * function addOne() {
1017
+ * cnt++;
1018
+ * return cnt;
1019
+ * }
1020
+ *
1021
+ * function addTwo() {
1022
+ * cnt += 2;
1023
+ * return cnt;
1024
+ * }
1025
+ *
1026
+ * const fn = t.mock.fn(addOne);
1027
+ *
1028
+ * assert.strictEqual(fn(), 1);
1029
+ * fn.mock.mockImplementationOnce(addTwo);
1030
+ * assert.strictEqual(fn(), 3);
1031
+ * assert.strictEqual(fn(), 4);
1032
+ * });
1033
+ * ```
1034
+ * @since v19.1.0, v18.13.0
1035
+ * @param implementation The function to be used as the mock's implementation for the invocation number specified by `onCall`.
1036
+ * @param onCall The invocation number that will use `implementation`. If the specified invocation has already occurred then an exception is thrown.
1037
+ */
1038
+ mockImplementationOnce(implementation: Function, onCall?: number): void;
1039
+ /**
1040
+ * Resets the call history of the mock function.
1041
+ * @since v19.3.0, v18.13.0
1042
+ */
1043
+ resetCalls(): void;
1044
+ /**
1045
+ * Resets the implementation of the mock function to its original behavior. The
1046
+ * mock can still be used after calling this function.
1047
+ * @since v19.1.0, v18.13.0
1048
+ */
1049
+ restore(): void;
1050
+ }
1051
+ export { test as default, run, test, describe, it, before, after, beforeEach, afterEach, mock, skip, only, todo };
314
1052
  }