@types/node 20.19.2 → 20.19.4
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.
- node v20.19/README.md +1 -1
- node v20.19/buffer.d.ts +2 -0
- node v20.19/package.json +2 -2
- node v20.19/test.d.ts +1525 -1610
node v20.19/test.d.ts
CHANGED
@@ -80,26 +80,8 @@
|
|
80
80
|
*/
|
81
81
|
declare module "node:test" {
|
82
82
|
import { Readable } from "node:stream";
|
83
|
-
|
84
|
-
|
85
|
-
* machines or processes, ideal for large-scale executions across varied
|
86
|
-
* environments. It's incompatible with `watch` mode, tailored for rapid
|
87
|
-
* code iteration by automatically rerunning tests on file changes.
|
88
|
-
*
|
89
|
-
* ```js
|
90
|
-
* import { tap } from 'node:test/reporters';
|
91
|
-
* import { run } from 'node:test';
|
92
|
-
* import process from 'node:process';
|
93
|
-
* import path from 'node:path';
|
94
|
-
*
|
95
|
-
* run({ files: [path.resolve('./tests/test.js')] })
|
96
|
-
* .compose(tap)
|
97
|
-
* .pipe(process.stdout);
|
98
|
-
* ```
|
99
|
-
* @since v18.9.0, v16.19.0
|
100
|
-
* @param options Configuration options for running tests.
|
101
|
-
*/
|
102
|
-
function run(options?: RunOptions): TestsStream;
|
83
|
+
import TestFn = test.TestFn;
|
84
|
+
import TestOptions = test.TestOptions;
|
103
85
|
/**
|
104
86
|
* The `test()` function is the value imported from the `test` module. Each
|
105
87
|
* invocation of this function results in reporting the test to the `TestsStream`.
|
@@ -144,1679 +126,1611 @@ declare module "node:test" {
|
|
144
126
|
function test(options?: TestOptions, fn?: TestFn): Promise<void>;
|
145
127
|
function test(fn?: TestFn): Promise<void>;
|
146
128
|
namespace test {
|
147
|
-
export {
|
129
|
+
export { test };
|
130
|
+
export { suite as describe, test as it };
|
148
131
|
}
|
149
|
-
|
150
|
-
* The `suite()` function is imported from the `node:test` module.
|
151
|
-
* @param name The name of the suite, which is displayed when reporting test results.
|
152
|
-
* Defaults to the `name` property of `fn`, or `'<anonymous>'` if `fn` does not have a name.
|
153
|
-
* @param options Configuration options for the suite. This supports the same options as {@link test}.
|
154
|
-
* @param fn The suite function declaring nested tests and suites. The first argument to this function is a {@link SuiteContext} object.
|
155
|
-
* @return Immediately fulfilled with `undefined`.
|
156
|
-
* @since v20.13.0
|
157
|
-
*/
|
158
|
-
function suite(name?: string, options?: TestOptions, fn?: SuiteFn): Promise<void>;
|
159
|
-
function suite(name?: string, fn?: SuiteFn): Promise<void>;
|
160
|
-
function suite(options?: TestOptions, fn?: SuiteFn): Promise<void>;
|
161
|
-
function suite(fn?: SuiteFn): Promise<void>;
|
162
|
-
namespace suite {
|
163
|
-
/**
|
164
|
-
* Shorthand for skipping a suite. This is the same as calling {@link suite} with `options.skip` set to `true`.
|
165
|
-
* @since v20.13.0
|
166
|
-
*/
|
167
|
-
function skip(name?: string, options?: TestOptions, fn?: SuiteFn): Promise<void>;
|
168
|
-
function skip(name?: string, fn?: SuiteFn): Promise<void>;
|
169
|
-
function skip(options?: TestOptions, fn?: SuiteFn): Promise<void>;
|
170
|
-
function skip(fn?: SuiteFn): Promise<void>;
|
132
|
+
namespace test {
|
171
133
|
/**
|
172
|
-
*
|
173
|
-
*
|
134
|
+
* **Note:** `shard` is used to horizontally parallelize test running across
|
135
|
+
* machines or processes, ideal for large-scale executions across varied
|
136
|
+
* environments. It's incompatible with `watch` mode, tailored for rapid
|
137
|
+
* code iteration by automatically rerunning tests on file changes.
|
138
|
+
*
|
139
|
+
* ```js
|
140
|
+
* import { tap } from 'node:test/reporters';
|
141
|
+
* import { run } from 'node:test';
|
142
|
+
* import process from 'node:process';
|
143
|
+
* import path from 'node:path';
|
144
|
+
*
|
145
|
+
* run({ files: [path.resolve('./tests/test.js')] })
|
146
|
+
* .compose(tap)
|
147
|
+
* .pipe(process.stdout);
|
148
|
+
* ```
|
149
|
+
* @since v18.9.0, v16.19.0
|
150
|
+
* @param options Configuration options for running tests.
|
174
151
|
*/
|
175
|
-
function
|
176
|
-
function todo(name?: string, fn?: SuiteFn): Promise<void>;
|
177
|
-
function todo(options?: TestOptions, fn?: SuiteFn): Promise<void>;
|
178
|
-
function todo(fn?: SuiteFn): Promise<void>;
|
152
|
+
function run(options?: RunOptions): TestsStream;
|
179
153
|
/**
|
180
|
-
*
|
154
|
+
* The `suite()` function is imported from the `node:test` module.
|
155
|
+
* @param name The name of the suite, which is displayed when reporting test results.
|
156
|
+
* Defaults to the `name` property of `fn`, or `'<anonymous>'` if `fn` does not have a name.
|
157
|
+
* @param options Configuration options for the suite. This supports the same options as {@link test}.
|
158
|
+
* @param fn The suite function declaring nested tests and suites. The first argument to this function is a {@link SuiteContext} object.
|
159
|
+
* @return Immediately fulfilled with `undefined`.
|
181
160
|
* @since v20.13.0
|
182
161
|
*/
|
183
|
-
function
|
184
|
-
function
|
185
|
-
function
|
186
|
-
function
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
function todo(fn?: SuiteFn): Promise<void>;
|
214
|
-
/**
|
215
|
-
* Shorthand for marking a suite as `only`. This is the same as calling {@link describe} with `options.only` set to `true`.
|
216
|
-
* @since v18.15.0
|
217
|
-
*/
|
218
|
-
function only(name?: string, options?: TestOptions, fn?: SuiteFn): Promise<void>;
|
219
|
-
function only(name?: string, fn?: SuiteFn): Promise<void>;
|
220
|
-
function only(options?: TestOptions, fn?: SuiteFn): Promise<void>;
|
221
|
-
function only(fn?: SuiteFn): Promise<void>;
|
222
|
-
}
|
223
|
-
/**
|
224
|
-
* Alias for {@link test}.
|
225
|
-
*
|
226
|
-
* The `it()` function is imported from the `node:test` module.
|
227
|
-
* @since v18.6.0, v16.17.0
|
228
|
-
*/
|
229
|
-
function it(name?: string, options?: TestOptions, fn?: TestFn): Promise<void>;
|
230
|
-
function it(name?: string, fn?: TestFn): Promise<void>;
|
231
|
-
function it(options?: TestOptions, fn?: TestFn): Promise<void>;
|
232
|
-
function it(fn?: TestFn): Promise<void>;
|
233
|
-
namespace it {
|
162
|
+
function suite(name?: string, options?: TestOptions, fn?: SuiteFn): Promise<void>;
|
163
|
+
function suite(name?: string, fn?: SuiteFn): Promise<void>;
|
164
|
+
function suite(options?: TestOptions, fn?: SuiteFn): Promise<void>;
|
165
|
+
function suite(fn?: SuiteFn): Promise<void>;
|
166
|
+
namespace suite {
|
167
|
+
/**
|
168
|
+
* Shorthand for skipping a suite. This is the same as calling {@link suite} with `options.skip` set to `true`.
|
169
|
+
* @since v20.13.0
|
170
|
+
*/
|
171
|
+
function skip(name?: string, options?: TestOptions, fn?: SuiteFn): Promise<void>;
|
172
|
+
function skip(name?: string, fn?: SuiteFn): Promise<void>;
|
173
|
+
function skip(options?: TestOptions, fn?: SuiteFn): Promise<void>;
|
174
|
+
function skip(fn?: SuiteFn): Promise<void>;
|
175
|
+
/**
|
176
|
+
* Shorthand for marking a suite as `TODO`. This is the same as calling {@link suite} with `options.todo` set to `true`.
|
177
|
+
* @since v20.13.0
|
178
|
+
*/
|
179
|
+
function todo(name?: string, options?: TestOptions, fn?: SuiteFn): Promise<void>;
|
180
|
+
function todo(name?: string, fn?: SuiteFn): Promise<void>;
|
181
|
+
function todo(options?: TestOptions, fn?: SuiteFn): Promise<void>;
|
182
|
+
function todo(fn?: SuiteFn): Promise<void>;
|
183
|
+
/**
|
184
|
+
* Shorthand for marking a suite as `only`. This is the same as calling {@link suite} with `options.only` set to `true`.
|
185
|
+
* @since v20.13.0
|
186
|
+
*/
|
187
|
+
function only(name?: string, options?: TestOptions, fn?: SuiteFn): Promise<void>;
|
188
|
+
function only(name?: string, fn?: SuiteFn): Promise<void>;
|
189
|
+
function only(options?: TestOptions, fn?: SuiteFn): Promise<void>;
|
190
|
+
function only(fn?: SuiteFn): Promise<void>;
|
191
|
+
}
|
234
192
|
/**
|
235
|
-
* Shorthand for skipping a test. This is the same as calling {@link
|
193
|
+
* Shorthand for skipping a test. This is the same as calling {@link test} with `options.skip` set to `true`.
|
194
|
+
* @since v20.2.0
|
236
195
|
*/
|
237
196
|
function skip(name?: string, options?: TestOptions, fn?: TestFn): Promise<void>;
|
238
197
|
function skip(name?: string, fn?: TestFn): Promise<void>;
|
239
198
|
function skip(options?: TestOptions, fn?: TestFn): Promise<void>;
|
240
199
|
function skip(fn?: TestFn): Promise<void>;
|
241
200
|
/**
|
242
|
-
* Shorthand for marking a test as `TODO`. This is the same as calling {@link
|
201
|
+
* Shorthand for marking a test as `TODO`. This is the same as calling {@link test} with `options.todo` set to `true`.
|
202
|
+
* @since v20.2.0
|
243
203
|
*/
|
244
204
|
function todo(name?: string, options?: TestOptions, fn?: TestFn): Promise<void>;
|
245
205
|
function todo(name?: string, fn?: TestFn): Promise<void>;
|
246
206
|
function todo(options?: TestOptions, fn?: TestFn): Promise<void>;
|
247
207
|
function todo(fn?: TestFn): Promise<void>;
|
248
208
|
/**
|
249
|
-
* Shorthand for marking a test as `only`. This is the same as calling {@link
|
250
|
-
* @since
|
209
|
+
* Shorthand for marking a test as `only`. This is the same as calling {@link test} with `options.only` set to `true`.
|
210
|
+
* @since v20.2.0
|
251
211
|
*/
|
252
212
|
function only(name?: string, options?: TestOptions, fn?: TestFn): Promise<void>;
|
253
213
|
function only(name?: string, fn?: TestFn): Promise<void>;
|
254
214
|
function only(options?: TestOptions, fn?: TestFn): Promise<void>;
|
255
215
|
function only(fn?: TestFn): Promise<void>;
|
256
|
-
}
|
257
|
-
/**
|
258
|
-
* Shorthand for skipping a test. This is the same as calling {@link test} with `options.skip` set to `true`.
|
259
|
-
* @since v20.2.0
|
260
|
-
*/
|
261
|
-
function skip(name?: string, options?: TestOptions, fn?: TestFn): Promise<void>;
|
262
|
-
function skip(name?: string, fn?: TestFn): Promise<void>;
|
263
|
-
function skip(options?: TestOptions, fn?: TestFn): Promise<void>;
|
264
|
-
function skip(fn?: TestFn): Promise<void>;
|
265
|
-
/**
|
266
|
-
* Shorthand for marking a test as `TODO`. This is the same as calling {@link test} with `options.todo` set to `true`.
|
267
|
-
* @since v20.2.0
|
268
|
-
*/
|
269
|
-
function todo(name?: string, options?: TestOptions, fn?: TestFn): Promise<void>;
|
270
|
-
function todo(name?: string, fn?: TestFn): Promise<void>;
|
271
|
-
function todo(options?: TestOptions, fn?: TestFn): Promise<void>;
|
272
|
-
function todo(fn?: TestFn): Promise<void>;
|
273
|
-
/**
|
274
|
-
* Shorthand for marking a test as `only`. This is the same as calling {@link test} with `options.only` set to `true`.
|
275
|
-
* @since v20.2.0
|
276
|
-
*/
|
277
|
-
function only(name?: string, options?: TestOptions, fn?: TestFn): Promise<void>;
|
278
|
-
function only(name?: string, fn?: TestFn): Promise<void>;
|
279
|
-
function only(options?: TestOptions, fn?: TestFn): Promise<void>;
|
280
|
-
function only(fn?: TestFn): Promise<void>;
|
281
|
-
/**
|
282
|
-
* The type of a function passed to {@link test}. The first argument to this function is a {@link TestContext} object.
|
283
|
-
* If the test uses callbacks, the callback function is passed as the second argument.
|
284
|
-
*/
|
285
|
-
type TestFn = (t: TestContext, done: (result?: any) => void) => void | Promise<void>;
|
286
|
-
/**
|
287
|
-
* The type of a suite test function. The argument to this function is a {@link SuiteContext} object.
|
288
|
-
*/
|
289
|
-
type SuiteFn = (s: SuiteContext) => void | Promise<void>;
|
290
|
-
interface TestShard {
|
291
|
-
/**
|
292
|
-
* A positive integer between 1 and `total` that specifies the index of the shard to run.
|
293
|
-
*/
|
294
|
-
index: number;
|
295
216
|
/**
|
296
|
-
*
|
297
|
-
|
298
|
-
total: number;
|
299
|
-
}
|
300
|
-
interface RunOptions {
|
301
|
-
/**
|
302
|
-
* If a number is provided, then that many test processes would run in parallel, where each process corresponds to one test file.
|
303
|
-
* If `true`, it would run `os.availableParallelism() - 1` test files in parallel. If `false`, it would only run one test file at a time.
|
304
|
-
* @default false
|
217
|
+
* The type of a function passed to {@link test}. The first argument to this function is a {@link TestContext} object.
|
218
|
+
* If the test uses callbacks, the callback function is passed as the second argument.
|
305
219
|
*/
|
306
|
-
|
220
|
+
type TestFn = (t: TestContext, done: (result?: any) => void) => void | Promise<void>;
|
307
221
|
/**
|
308
|
-
*
|
309
|
-
* [test runner execution model](https://nodejs.org/docs/latest-v20.x/api/test.html#test-runner-execution-model).
|
222
|
+
* The type of a suite test function. The argument to this function is a {@link SuiteContext} object.
|
310
223
|
*/
|
311
|
-
|
224
|
+
type SuiteFn = (s: SuiteContext) => void | Promise<void>;
|
225
|
+
interface TestShard {
|
226
|
+
/**
|
227
|
+
* A positive integer between 1 and `total` that specifies the index of the shard to run.
|
228
|
+
*/
|
229
|
+
index: number;
|
230
|
+
/**
|
231
|
+
* A positive integer that specifies the total number of shards to split the test files to.
|
232
|
+
*/
|
233
|
+
total: number;
|
234
|
+
}
|
235
|
+
interface RunOptions {
|
236
|
+
/**
|
237
|
+
* If a number is provided, then that many test processes would run in parallel, where each process corresponds to one test file.
|
238
|
+
* If `true`, it would run `os.availableParallelism() - 1` test files in parallel. If `false`, it would only run one test file at a time.
|
239
|
+
* @default false
|
240
|
+
*/
|
241
|
+
concurrency?: number | boolean | undefined;
|
242
|
+
/**
|
243
|
+
* An array containing the list of files to run. If omitted, files are run according to the
|
244
|
+
* [test runner execution model](https://nodejs.org/docs/latest-v20.x/api/test.html#test-runner-execution-model).
|
245
|
+
*/
|
246
|
+
files?: readonly string[] | undefined;
|
247
|
+
/**
|
248
|
+
* Configures the test runner to exit the process once all known
|
249
|
+
* tests have finished executing even if the event loop would
|
250
|
+
* otherwise remain active.
|
251
|
+
* @default false
|
252
|
+
*/
|
253
|
+
forceExit?: boolean | undefined;
|
254
|
+
/**
|
255
|
+
* Sets inspector port of test child process.
|
256
|
+
* If a nullish value is provided, each process gets its own port,
|
257
|
+
* incremented from the primary's `process.debugPort`.
|
258
|
+
* @default undefined
|
259
|
+
*/
|
260
|
+
inspectPort?: number | (() => number) | undefined;
|
261
|
+
/**
|
262
|
+
* If truthy, the test context will only run tests that have the `only` option set
|
263
|
+
*/
|
264
|
+
only?: boolean | undefined;
|
265
|
+
/**
|
266
|
+
* A function that accepts the `TestsStream` instance and can be used to setup listeners before any tests are run.
|
267
|
+
* @default undefined
|
268
|
+
*/
|
269
|
+
setup?: ((reporter: TestsStream) => void | Promise<void>) | undefined;
|
270
|
+
/**
|
271
|
+
* Allows aborting an in-progress test execution.
|
272
|
+
*/
|
273
|
+
signal?: AbortSignal | undefined;
|
274
|
+
/**
|
275
|
+
* If provided, only run tests whose name matches the provided pattern.
|
276
|
+
* Strings are interpreted as JavaScript regular expressions.
|
277
|
+
* @default undefined
|
278
|
+
*/
|
279
|
+
testNamePatterns?: string | RegExp | ReadonlyArray<string | RegExp> | undefined;
|
280
|
+
/**
|
281
|
+
* The number of milliseconds after which the test execution will fail.
|
282
|
+
* If unspecified, subtests inherit this value from their parent.
|
283
|
+
* @default Infinity
|
284
|
+
*/
|
285
|
+
timeout?: number | undefined;
|
286
|
+
/**
|
287
|
+
* Whether to run in watch mode or not.
|
288
|
+
* @default false
|
289
|
+
*/
|
290
|
+
watch?: boolean | undefined;
|
291
|
+
/**
|
292
|
+
* Running tests in a specific shard.
|
293
|
+
* @default undefined
|
294
|
+
*/
|
295
|
+
shard?: TestShard | undefined;
|
296
|
+
}
|
297
|
+
/**
|
298
|
+
* A successful call to `run()` will return a new `TestsStream` object, streaming a series of events representing the execution of the tests.
|
299
|
+
*
|
300
|
+
* Some of the events are guaranteed to be emitted in the same order as the tests are defined, while others are emitted in the order that the tests execute.
|
301
|
+
* @since v18.9.0, v16.19.0
|
302
|
+
*/
|
303
|
+
interface TestsStream extends Readable {
|
304
|
+
addListener(event: "test:coverage", listener: (data: EventData.TestCoverage) => void): this;
|
305
|
+
addListener(event: "test:complete", listener: (data: EventData.TestComplete) => void): this;
|
306
|
+
addListener(event: "test:dequeue", listener: (data: EventData.TestDequeue) => void): this;
|
307
|
+
addListener(event: "test:diagnostic", listener: (data: EventData.TestDiagnostic) => void): this;
|
308
|
+
addListener(event: "test:enqueue", listener: (data: EventData.TestEnqueue) => void): this;
|
309
|
+
addListener(event: "test:fail", listener: (data: EventData.TestFail) => void): this;
|
310
|
+
addListener(event: "test:pass", listener: (data: EventData.TestPass) => void): this;
|
311
|
+
addListener(event: "test:plan", listener: (data: EventData.TestPlan) => void): this;
|
312
|
+
addListener(event: "test:start", listener: (data: EventData.TestStart) => void): this;
|
313
|
+
addListener(event: "test:stderr", listener: (data: EventData.TestStderr) => void): this;
|
314
|
+
addListener(event: "test:stdout", listener: (data: EventData.TestStdout) => void): this;
|
315
|
+
addListener(event: "test:watch:drained", listener: () => void): this;
|
316
|
+
addListener(event: string, listener: (...args: any[]) => void): this;
|
317
|
+
emit(event: "test:coverage", data: EventData.TestCoverage): boolean;
|
318
|
+
emit(event: "test:complete", data: EventData.TestComplete): boolean;
|
319
|
+
emit(event: "test:dequeue", data: EventData.TestDequeue): boolean;
|
320
|
+
emit(event: "test:diagnostic", data: EventData.TestDiagnostic): boolean;
|
321
|
+
emit(event: "test:enqueue", data: EventData.TestEnqueue): boolean;
|
322
|
+
emit(event: "test:fail", data: EventData.TestFail): boolean;
|
323
|
+
emit(event: "test:pass", data: EventData.TestPass): boolean;
|
324
|
+
emit(event: "test:plan", data: EventData.TestPlan): boolean;
|
325
|
+
emit(event: "test:start", data: EventData.TestStart): boolean;
|
326
|
+
emit(event: "test:stderr", data: EventData.TestStderr): boolean;
|
327
|
+
emit(event: "test:stdout", data: EventData.TestStdout): boolean;
|
328
|
+
emit(event: "test:watch:drained"): boolean;
|
329
|
+
emit(event: string | symbol, ...args: any[]): boolean;
|
330
|
+
on(event: "test:coverage", listener: (data: EventData.TestCoverage) => void): this;
|
331
|
+
on(event: "test:complete", listener: (data: EventData.TestComplete) => void): this;
|
332
|
+
on(event: "test:dequeue", listener: (data: EventData.TestDequeue) => void): this;
|
333
|
+
on(event: "test:diagnostic", listener: (data: EventData.TestDiagnostic) => void): this;
|
334
|
+
on(event: "test:enqueue", listener: (data: EventData.TestEnqueue) => void): this;
|
335
|
+
on(event: "test:fail", listener: (data: EventData.TestFail) => void): this;
|
336
|
+
on(event: "test:pass", listener: (data: EventData.TestPass) => void): this;
|
337
|
+
on(event: "test:plan", listener: (data: EventData.TestPlan) => void): this;
|
338
|
+
on(event: "test:start", listener: (data: EventData.TestStart) => void): this;
|
339
|
+
on(event: "test:stderr", listener: (data: EventData.TestStderr) => void): this;
|
340
|
+
on(event: "test:stdout", listener: (data: EventData.TestStdout) => void): this;
|
341
|
+
on(event: "test:watch:drained", listener: () => void): this;
|
342
|
+
on(event: string, listener: (...args: any[]) => void): this;
|
343
|
+
once(event: "test:coverage", listener: (data: EventData.TestCoverage) => void): this;
|
344
|
+
once(event: "test:complete", listener: (data: EventData.TestComplete) => void): this;
|
345
|
+
once(event: "test:dequeue", listener: (data: EventData.TestDequeue) => void): this;
|
346
|
+
once(event: "test:diagnostic", listener: (data: EventData.TestDiagnostic) => void): this;
|
347
|
+
once(event: "test:enqueue", listener: (data: EventData.TestEnqueue) => void): this;
|
348
|
+
once(event: "test:fail", listener: (data: EventData.TestFail) => void): this;
|
349
|
+
once(event: "test:pass", listener: (data: EventData.TestPass) => void): this;
|
350
|
+
once(event: "test:plan", listener: (data: EventData.TestPlan) => void): this;
|
351
|
+
once(event: "test:start", listener: (data: EventData.TestStart) => void): this;
|
352
|
+
once(event: "test:stderr", listener: (data: EventData.TestStderr) => void): this;
|
353
|
+
once(event: "test:stdout", listener: (data: EventData.TestStdout) => void): this;
|
354
|
+
once(event: "test:watch:drained", listener: () => void): this;
|
355
|
+
once(event: string, listener: (...args: any[]) => void): this;
|
356
|
+
prependListener(event: "test:coverage", listener: (data: EventData.TestCoverage) => void): this;
|
357
|
+
prependListener(event: "test:complete", listener: (data: EventData.TestComplete) => void): this;
|
358
|
+
prependListener(event: "test:dequeue", listener: (data: EventData.TestDequeue) => void): this;
|
359
|
+
prependListener(event: "test:diagnostic", listener: (data: EventData.TestDiagnostic) => void): this;
|
360
|
+
prependListener(event: "test:enqueue", listener: (data: EventData.TestEnqueue) => void): this;
|
361
|
+
prependListener(event: "test:fail", listener: (data: EventData.TestFail) => void): this;
|
362
|
+
prependListener(event: "test:pass", listener: (data: EventData.TestPass) => void): this;
|
363
|
+
prependListener(event: "test:plan", listener: (data: EventData.TestPlan) => void): this;
|
364
|
+
prependListener(event: "test:start", listener: (data: EventData.TestStart) => void): this;
|
365
|
+
prependListener(event: "test:stderr", listener: (data: EventData.TestStderr) => void): this;
|
366
|
+
prependListener(event: "test:stdout", listener: (data: EventData.TestStdout) => void): this;
|
367
|
+
prependListener(event: "test:watch:drained", listener: () => void): this;
|
368
|
+
prependListener(event: string, listener: (...args: any[]) => void): this;
|
369
|
+
prependOnceListener(event: "test:coverage", listener: (data: EventData.TestCoverage) => void): this;
|
370
|
+
prependOnceListener(event: "test:complete", listener: (data: EventData.TestComplete) => void): this;
|
371
|
+
prependOnceListener(event: "test:dequeue", listener: (data: EventData.TestDequeue) => void): this;
|
372
|
+
prependOnceListener(event: "test:diagnostic", listener: (data: EventData.TestDiagnostic) => void): this;
|
373
|
+
prependOnceListener(event: "test:enqueue", listener: (data: EventData.TestEnqueue) => void): this;
|
374
|
+
prependOnceListener(event: "test:fail", listener: (data: EventData.TestFail) => void): this;
|
375
|
+
prependOnceListener(event: "test:pass", listener: (data: EventData.TestPass) => void): this;
|
376
|
+
prependOnceListener(event: "test:plan", listener: (data: EventData.TestPlan) => void): this;
|
377
|
+
prependOnceListener(event: "test:start", listener: (data: EventData.TestStart) => void): this;
|
378
|
+
prependOnceListener(event: "test:stderr", listener: (data: EventData.TestStderr) => void): this;
|
379
|
+
prependOnceListener(event: "test:stdout", listener: (data: EventData.TestStdout) => void): this;
|
380
|
+
prependOnceListener(event: "test:watch:drained", listener: () => void): this;
|
381
|
+
prependOnceListener(event: string, listener: (...args: any[]) => void): this;
|
382
|
+
}
|
383
|
+
namespace EventData {
|
384
|
+
interface Error extends globalThis.Error {
|
385
|
+
cause: globalThis.Error;
|
386
|
+
}
|
387
|
+
interface LocationInfo {
|
388
|
+
/**
|
389
|
+
* The column number where the test is defined, or
|
390
|
+
* `undefined` if the test was run through the REPL.
|
391
|
+
*/
|
392
|
+
column?: number;
|
393
|
+
/**
|
394
|
+
* The path of the test file, `undefined` if test was run through the REPL.
|
395
|
+
*/
|
396
|
+
file?: string;
|
397
|
+
/**
|
398
|
+
* The line number where the test is defined, or `undefined` if the test was run through the REPL.
|
399
|
+
*/
|
400
|
+
line?: number;
|
401
|
+
}
|
402
|
+
interface TestDiagnostic extends LocationInfo {
|
403
|
+
/**
|
404
|
+
* The diagnostic message.
|
405
|
+
*/
|
406
|
+
message: string;
|
407
|
+
/**
|
408
|
+
* The nesting level of the test.
|
409
|
+
*/
|
410
|
+
nesting: number;
|
411
|
+
}
|
412
|
+
interface TestCoverage {
|
413
|
+
/**
|
414
|
+
* An object containing the coverage report.
|
415
|
+
*/
|
416
|
+
summary: {
|
417
|
+
/**
|
418
|
+
* An array of coverage reports for individual files.
|
419
|
+
*/
|
420
|
+
files: Array<{
|
421
|
+
/**
|
422
|
+
* The absolute path of the file.
|
423
|
+
*/
|
424
|
+
path: string;
|
425
|
+
/**
|
426
|
+
* The total number of lines.
|
427
|
+
*/
|
428
|
+
totalLineCount: number;
|
429
|
+
/**
|
430
|
+
* The total number of branches.
|
431
|
+
*/
|
432
|
+
totalBranchCount: number;
|
433
|
+
/**
|
434
|
+
* The total number of functions.
|
435
|
+
*/
|
436
|
+
totalFunctionCount: number;
|
437
|
+
/**
|
438
|
+
* The number of covered lines.
|
439
|
+
*/
|
440
|
+
coveredLineCount: number;
|
441
|
+
/**
|
442
|
+
* The number of covered branches.
|
443
|
+
*/
|
444
|
+
coveredBranchCount: number;
|
445
|
+
/**
|
446
|
+
* The number of covered functions.
|
447
|
+
*/
|
448
|
+
coveredFunctionCount: number;
|
449
|
+
/**
|
450
|
+
* The percentage of lines covered.
|
451
|
+
*/
|
452
|
+
coveredLinePercent: number;
|
453
|
+
/**
|
454
|
+
* The percentage of branches covered.
|
455
|
+
*/
|
456
|
+
coveredBranchPercent: number;
|
457
|
+
/**
|
458
|
+
* The percentage of functions covered.
|
459
|
+
*/
|
460
|
+
coveredFunctionPercent: number;
|
461
|
+
/**
|
462
|
+
* An array of functions representing function coverage.
|
463
|
+
*/
|
464
|
+
functions: Array<{
|
465
|
+
/**
|
466
|
+
* The name of the function.
|
467
|
+
*/
|
468
|
+
name: string;
|
469
|
+
/**
|
470
|
+
* The line number where the function is defined.
|
471
|
+
*/
|
472
|
+
line: number;
|
473
|
+
/**
|
474
|
+
* The number of times the function was called.
|
475
|
+
*/
|
476
|
+
count: number;
|
477
|
+
}>;
|
478
|
+
/**
|
479
|
+
* An array of branches representing branch coverage.
|
480
|
+
*/
|
481
|
+
branches: Array<{
|
482
|
+
/**
|
483
|
+
* The line number where the branch is defined.
|
484
|
+
*/
|
485
|
+
line: number;
|
486
|
+
/**
|
487
|
+
* The number of times the branch was taken.
|
488
|
+
*/
|
489
|
+
count: number;
|
490
|
+
}>;
|
491
|
+
/**
|
492
|
+
* An array of lines representing line numbers and the number of times they were covered.
|
493
|
+
*/
|
494
|
+
lines: Array<{
|
495
|
+
/**
|
496
|
+
* The line number.
|
497
|
+
*/
|
498
|
+
line: number;
|
499
|
+
/**
|
500
|
+
* The number of times the line was covered.
|
501
|
+
*/
|
502
|
+
count: number;
|
503
|
+
}>;
|
504
|
+
}>;
|
505
|
+
/**
|
506
|
+
* An object containing a summary of coverage for all files.
|
507
|
+
*/
|
508
|
+
totals: {
|
509
|
+
/**
|
510
|
+
* The total number of lines.
|
511
|
+
*/
|
512
|
+
totalLineCount: number;
|
513
|
+
/**
|
514
|
+
* The total number of branches.
|
515
|
+
*/
|
516
|
+
totalBranchCount: number;
|
517
|
+
/**
|
518
|
+
* The total number of functions.
|
519
|
+
*/
|
520
|
+
totalFunctionCount: number;
|
521
|
+
/**
|
522
|
+
* The number of covered lines.
|
523
|
+
*/
|
524
|
+
coveredLineCount: number;
|
525
|
+
/**
|
526
|
+
* The number of covered branches.
|
527
|
+
*/
|
528
|
+
coveredBranchCount: number;
|
529
|
+
/**
|
530
|
+
* The number of covered functions.
|
531
|
+
*/
|
532
|
+
coveredFunctionCount: number;
|
533
|
+
/**
|
534
|
+
* The percentage of lines covered.
|
535
|
+
*/
|
536
|
+
coveredLinePercent: number;
|
537
|
+
/**
|
538
|
+
* The percentage of branches covered.
|
539
|
+
*/
|
540
|
+
coveredBranchPercent: number;
|
541
|
+
/**
|
542
|
+
* The percentage of functions covered.
|
543
|
+
*/
|
544
|
+
coveredFunctionPercent: number;
|
545
|
+
};
|
546
|
+
/**
|
547
|
+
* The working directory when code coverage began. This
|
548
|
+
* is useful for displaying relative path names in case
|
549
|
+
* the tests changed the working directory of the Node.js process.
|
550
|
+
*/
|
551
|
+
workingDirectory: string;
|
552
|
+
};
|
553
|
+
/**
|
554
|
+
* The nesting level of the test.
|
555
|
+
*/
|
556
|
+
nesting: number;
|
557
|
+
}
|
558
|
+
interface TestComplete extends LocationInfo {
|
559
|
+
/**
|
560
|
+
* Additional execution metadata.
|
561
|
+
*/
|
562
|
+
details: {
|
563
|
+
/**
|
564
|
+
* Whether the test passed or not.
|
565
|
+
*/
|
566
|
+
passed: boolean;
|
567
|
+
/**
|
568
|
+
* The duration of the test in milliseconds.
|
569
|
+
*/
|
570
|
+
duration_ms: number;
|
571
|
+
/**
|
572
|
+
* An error wrapping the error thrown by the test if it did not pass.
|
573
|
+
*/
|
574
|
+
error?: Error;
|
575
|
+
/**
|
576
|
+
* The type of the test, used to denote whether this is a suite.
|
577
|
+
*/
|
578
|
+
type?: "suite";
|
579
|
+
};
|
580
|
+
/**
|
581
|
+
* The test name.
|
582
|
+
*/
|
583
|
+
name: string;
|
584
|
+
/**
|
585
|
+
* The nesting level of the test.
|
586
|
+
*/
|
587
|
+
nesting: number;
|
588
|
+
/**
|
589
|
+
* The ordinal number of the test.
|
590
|
+
*/
|
591
|
+
testNumber: number;
|
592
|
+
/**
|
593
|
+
* Present if `context.todo` is called.
|
594
|
+
*/
|
595
|
+
todo?: string | boolean;
|
596
|
+
/**
|
597
|
+
* Present if `context.skip` is called.
|
598
|
+
*/
|
599
|
+
skip?: string | boolean;
|
600
|
+
}
|
601
|
+
interface TestDequeue extends LocationInfo {
|
602
|
+
/**
|
603
|
+
* The test name.
|
604
|
+
*/
|
605
|
+
name: string;
|
606
|
+
/**
|
607
|
+
* The nesting level of the test.
|
608
|
+
*/
|
609
|
+
nesting: number;
|
610
|
+
}
|
611
|
+
interface TestEnqueue extends LocationInfo {
|
612
|
+
/**
|
613
|
+
* The test name.
|
614
|
+
*/
|
615
|
+
name: string;
|
616
|
+
/**
|
617
|
+
* The nesting level of the test.
|
618
|
+
*/
|
619
|
+
nesting: number;
|
620
|
+
}
|
621
|
+
interface TestFail extends LocationInfo {
|
622
|
+
/**
|
623
|
+
* Additional execution metadata.
|
624
|
+
*/
|
625
|
+
details: {
|
626
|
+
/**
|
627
|
+
* The duration of the test in milliseconds.
|
628
|
+
*/
|
629
|
+
duration_ms: number;
|
630
|
+
/**
|
631
|
+
* An error wrapping the error thrown by the test.
|
632
|
+
*/
|
633
|
+
error: Error;
|
634
|
+
/**
|
635
|
+
* The type of the test, used to denote whether this is a suite.
|
636
|
+
* @since v20.0.0, v19.9.0, v18.17.0
|
637
|
+
*/
|
638
|
+
type?: "suite";
|
639
|
+
};
|
640
|
+
/**
|
641
|
+
* The test name.
|
642
|
+
*/
|
643
|
+
name: string;
|
644
|
+
/**
|
645
|
+
* The nesting level of the test.
|
646
|
+
*/
|
647
|
+
nesting: number;
|
648
|
+
/**
|
649
|
+
* The ordinal number of the test.
|
650
|
+
*/
|
651
|
+
testNumber: number;
|
652
|
+
/**
|
653
|
+
* Present if `context.todo` is called.
|
654
|
+
*/
|
655
|
+
todo?: string | boolean;
|
656
|
+
/**
|
657
|
+
* Present if `context.skip` is called.
|
658
|
+
*/
|
659
|
+
skip?: string | boolean;
|
660
|
+
}
|
661
|
+
interface TestPass extends LocationInfo {
|
662
|
+
/**
|
663
|
+
* Additional execution metadata.
|
664
|
+
*/
|
665
|
+
details: {
|
666
|
+
/**
|
667
|
+
* The duration of the test in milliseconds.
|
668
|
+
*/
|
669
|
+
duration_ms: number;
|
670
|
+
/**
|
671
|
+
* The type of the test, used to denote whether this is a suite.
|
672
|
+
* @since 20.0.0, 19.9.0, 18.17.0
|
673
|
+
*/
|
674
|
+
type?: "suite";
|
675
|
+
};
|
676
|
+
/**
|
677
|
+
* The test name.
|
678
|
+
*/
|
679
|
+
name: string;
|
680
|
+
/**
|
681
|
+
* The nesting level of the test.
|
682
|
+
*/
|
683
|
+
nesting: number;
|
684
|
+
/**
|
685
|
+
* The ordinal number of the test.
|
686
|
+
*/
|
687
|
+
testNumber: number;
|
688
|
+
/**
|
689
|
+
* Present if `context.todo` is called.
|
690
|
+
*/
|
691
|
+
todo?: string | boolean;
|
692
|
+
/**
|
693
|
+
* Present if `context.skip` is called.
|
694
|
+
*/
|
695
|
+
skip?: string | boolean;
|
696
|
+
}
|
697
|
+
interface TestPlan extends LocationInfo {
|
698
|
+
/**
|
699
|
+
* The nesting level of the test.
|
700
|
+
*/
|
701
|
+
nesting: number;
|
702
|
+
/**
|
703
|
+
* The number of subtests that have ran.
|
704
|
+
*/
|
705
|
+
count: number;
|
706
|
+
}
|
707
|
+
interface TestStart extends LocationInfo {
|
708
|
+
/**
|
709
|
+
* The test name.
|
710
|
+
*/
|
711
|
+
name: string;
|
712
|
+
/**
|
713
|
+
* The nesting level of the test.
|
714
|
+
*/
|
715
|
+
nesting: number;
|
716
|
+
}
|
717
|
+
interface TestStderr {
|
718
|
+
/**
|
719
|
+
* The path of the test file.
|
720
|
+
*/
|
721
|
+
file: string;
|
722
|
+
/**
|
723
|
+
* The message written to `stderr`.
|
724
|
+
*/
|
725
|
+
message: string;
|
726
|
+
}
|
727
|
+
interface TestStdout {
|
728
|
+
/**
|
729
|
+
* The path of the test file.
|
730
|
+
*/
|
731
|
+
file: string;
|
732
|
+
/**
|
733
|
+
* The message written to `stdout`.
|
734
|
+
*/
|
735
|
+
message: string;
|
736
|
+
}
|
737
|
+
}
|
312
738
|
/**
|
313
|
-
*
|
314
|
-
*
|
315
|
-
*
|
316
|
-
* @
|
739
|
+
* An instance of `TestContext` is passed to each test function in order to
|
740
|
+
* interact with the test runner. However, the `TestContext` constructor is not
|
741
|
+
* exposed as part of the API.
|
742
|
+
* @since v18.0.0, v16.17.0
|
317
743
|
*/
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
744
|
+
interface TestContext {
|
745
|
+
/**
|
746
|
+
* An object containing assertion methods bound to the test context.
|
747
|
+
* The top-level functions from the `node:assert` module are exposed here for the purpose of creating test plans.
|
748
|
+
*
|
749
|
+
* **Note:** Some of the functions from `node:assert` contain type assertions. If these are called via the
|
750
|
+
* TestContext `assert` object, then the context parameter in the test's function signature **must be explicitly typed**
|
751
|
+
* (ie. the parameter must have a type annotation), otherwise an error will be raised by the TypeScript compiler:
|
752
|
+
* ```ts
|
753
|
+
* import { test, type TestContext } from 'node:test';
|
754
|
+
*
|
755
|
+
* // The test function's context parameter must have a type annotation.
|
756
|
+
* test('example', (t: TestContext) => {
|
757
|
+
* t.assert.deepStrictEqual(actual, expected);
|
758
|
+
* });
|
759
|
+
*
|
760
|
+
* // Omitting the type annotation will result in a compilation error.
|
761
|
+
* test('example', t => {
|
762
|
+
* t.assert.deepStrictEqual(actual, expected); // Error: 't' needs an explicit type annotation.
|
763
|
+
* });
|
764
|
+
* ```
|
765
|
+
* @since v20.15.0
|
766
|
+
*/
|
767
|
+
readonly assert: TestContextAssert;
|
768
|
+
/**
|
769
|
+
* This function is used to create a hook running before subtest of the current test.
|
770
|
+
* @param fn The hook function. The first argument to this function is a `TestContext` object.
|
771
|
+
* If the hook uses callbacks, the callback function is passed as the second argument.
|
772
|
+
* @param options Configuration options for the hook.
|
773
|
+
* @since v20.1.0, v18.17.0
|
774
|
+
*/
|
775
|
+
before(fn?: TestContextHookFn, options?: HookOptions): void;
|
776
|
+
/**
|
777
|
+
* This function is used to create a hook running before each subtest of the current test.
|
778
|
+
* @param fn The hook function. The first argument to this function is a `TestContext` object.
|
779
|
+
* If the hook uses callbacks, the callback function is passed as the second argument.
|
780
|
+
* @param options Configuration options for the hook.
|
781
|
+
* @since v18.8.0
|
782
|
+
*/
|
783
|
+
beforeEach(fn?: TestContextHookFn, options?: HookOptions): void;
|
784
|
+
/**
|
785
|
+
* This function is used to create a hook that runs after the current test finishes.
|
786
|
+
* @param fn The hook function. The first argument to this function is a `TestContext` object.
|
787
|
+
* If the hook uses callbacks, the callback function is passed as the second argument.
|
788
|
+
* @param options Configuration options for the hook.
|
789
|
+
* @since v18.13.0
|
790
|
+
*/
|
791
|
+
after(fn?: TestContextHookFn, options?: HookOptions): void;
|
792
|
+
/**
|
793
|
+
* This function is used to create a hook running after each subtest of the current test.
|
794
|
+
* @param fn The hook function. The first argument to this function is a `TestContext` object.
|
795
|
+
* If the hook uses callbacks, the callback function is passed as the second argument.
|
796
|
+
* @param options Configuration options for the hook.
|
797
|
+
* @since v18.8.0
|
798
|
+
*/
|
799
|
+
afterEach(fn?: TestContextHookFn, options?: HookOptions): void;
|
800
|
+
/**
|
801
|
+
* This function is used to write diagnostics to the output. Any diagnostic
|
802
|
+
* information is included at the end of the test's results. This function does
|
803
|
+
* not return a value.
|
804
|
+
*
|
805
|
+
* ```js
|
806
|
+
* test('top level test', (t) => {
|
807
|
+
* t.diagnostic('A diagnostic message');
|
808
|
+
* });
|
809
|
+
* ```
|
810
|
+
* @since v18.0.0, v16.17.0
|
811
|
+
* @param message Message to be reported.
|
812
|
+
*/
|
813
|
+
diagnostic(message: string): void;
|
814
|
+
/**
|
815
|
+
* The name of the test and each of its ancestors, separated by `>`.
|
816
|
+
* @since v20.16.0
|
817
|
+
*/
|
818
|
+
readonly fullName: string;
|
819
|
+
/**
|
820
|
+
* The name of the test.
|
821
|
+
* @since v18.8.0, v16.18.0
|
822
|
+
*/
|
823
|
+
readonly name: string;
|
824
|
+
/**
|
825
|
+
* Used to set the number of assertions and subtests that are expected to run within the test.
|
826
|
+
* If the number of assertions and subtests that run does not match the expected count, the test will fail.
|
827
|
+
*
|
828
|
+
* To make sure assertions are tracked, the assert functions on `context.assert` must be used,
|
829
|
+
* instead of importing from the `node:assert` module.
|
830
|
+
* ```js
|
831
|
+
* test('top level test', (t) => {
|
832
|
+
* t.plan(2);
|
833
|
+
* t.assert.ok('some relevant assertion here');
|
834
|
+
* t.test('subtest', () => {});
|
835
|
+
* });
|
836
|
+
* ```
|
837
|
+
*
|
838
|
+
* When working with asynchronous code, the `plan` function can be used to ensure that the correct number of assertions are run:
|
839
|
+
* ```js
|
840
|
+
* test('planning with streams', (t, done) => {
|
841
|
+
* function* generate() {
|
842
|
+
* yield 'a';
|
843
|
+
* yield 'b';
|
844
|
+
* yield 'c';
|
845
|
+
* }
|
846
|
+
* const expected = ['a', 'b', 'c'];
|
847
|
+
* t.plan(expected.length);
|
848
|
+
* const stream = Readable.from(generate());
|
849
|
+
* stream.on('data', (chunk) => {
|
850
|
+
* t.assert.strictEqual(chunk, expected.shift());
|
851
|
+
* });
|
852
|
+
* stream.on('end', () => {
|
853
|
+
* done();
|
854
|
+
* });
|
855
|
+
* });
|
856
|
+
* ```
|
857
|
+
* @since v20.15.0
|
858
|
+
*/
|
859
|
+
plan(count: number): void;
|
860
|
+
/**
|
861
|
+
* If `shouldRunOnlyTests` is truthy, the test context will only run tests that
|
862
|
+
* have the `only` option set. Otherwise, all tests are run. If Node.js was not
|
863
|
+
* started with the `--test-only` command-line option, this function is a
|
864
|
+
* no-op.
|
865
|
+
*
|
866
|
+
* ```js
|
867
|
+
* test('top level test', (t) => {
|
868
|
+
* // The test context can be set to run subtests with the 'only' option.
|
869
|
+
* t.runOnly(true);
|
870
|
+
* return Promise.all([
|
871
|
+
* t.test('this subtest is now skipped'),
|
872
|
+
* t.test('this subtest is run', { only: true }),
|
873
|
+
* ]);
|
874
|
+
* });
|
875
|
+
* ```
|
876
|
+
* @since v18.0.0, v16.17.0
|
877
|
+
* @param shouldRunOnlyTests Whether or not to run `only` tests.
|
878
|
+
*/
|
879
|
+
runOnly(shouldRunOnlyTests: boolean): void;
|
880
|
+
/**
|
881
|
+
* ```js
|
882
|
+
* test('top level test', async (t) => {
|
883
|
+
* await fetch('some/uri', { signal: t.signal });
|
884
|
+
* });
|
885
|
+
* ```
|
886
|
+
* @since v18.7.0, v16.17.0
|
887
|
+
*/
|
888
|
+
readonly signal: AbortSignal;
|
889
|
+
/**
|
890
|
+
* 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
|
891
|
+
* not terminate execution of the test function. This function does not return a
|
892
|
+
* value.
|
893
|
+
*
|
894
|
+
* ```js
|
895
|
+
* test('top level test', (t) => {
|
896
|
+
* // Make sure to return here as well if the test contains additional logic.
|
897
|
+
* t.skip('this is skipped');
|
898
|
+
* });
|
899
|
+
* ```
|
900
|
+
* @since v18.0.0, v16.17.0
|
901
|
+
* @param message Optional skip message.
|
902
|
+
*/
|
903
|
+
skip(message?: string): void;
|
904
|
+
/**
|
905
|
+
* This function adds a `TODO` directive to the test's output. If `message` is
|
906
|
+
* provided, it is included in the output. Calling `todo()` does not terminate
|
907
|
+
* execution of the test function. This function does not return a value.
|
908
|
+
*
|
909
|
+
* ```js
|
910
|
+
* test('top level test', (t) => {
|
911
|
+
* // This test is marked as `TODO`
|
912
|
+
* t.todo('this is a todo');
|
913
|
+
* });
|
914
|
+
* ```
|
915
|
+
* @since v18.0.0, v16.17.0
|
916
|
+
* @param message Optional `TODO` message.
|
917
|
+
*/
|
918
|
+
todo(message?: string): void;
|
919
|
+
/**
|
920
|
+
* This function is used to create subtests under the current test. This function behaves in
|
921
|
+
* the same fashion as the top level {@link test} function.
|
922
|
+
* @since v18.0.0
|
923
|
+
* @param name The name of the test, which is displayed when reporting test results.
|
924
|
+
* Defaults to the `name` property of `fn`, or `'<anonymous>'` if `fn` does not have a name.
|
925
|
+
* @param options Configuration options for the test.
|
926
|
+
* @param fn The function under test. This first argument to this function is a {@link TestContext} object.
|
927
|
+
* If the test uses callbacks, the callback function is passed as the second argument.
|
928
|
+
* @returns A {@link Promise} resolved with `undefined` once the test completes.
|
929
|
+
*/
|
930
|
+
test: typeof test;
|
931
|
+
/**
|
932
|
+
* Each test provides its own MockTracker instance.
|
933
|
+
*/
|
934
|
+
readonly mock: MockTracker;
|
935
|
+
}
|
936
|
+
interface TestContextAssert extends
|
937
|
+
Pick<
|
938
|
+
typeof import("assert"),
|
939
|
+
| "deepEqual"
|
940
|
+
| "deepStrictEqual"
|
941
|
+
| "doesNotMatch"
|
942
|
+
| "doesNotReject"
|
943
|
+
| "doesNotThrow"
|
944
|
+
| "equal"
|
945
|
+
| "fail"
|
946
|
+
| "ifError"
|
947
|
+
| "match"
|
948
|
+
| "notDeepEqual"
|
949
|
+
| "notDeepStrictEqual"
|
950
|
+
| "notEqual"
|
951
|
+
| "notStrictEqual"
|
952
|
+
| "ok"
|
953
|
+
| "rejects"
|
954
|
+
| "strictEqual"
|
955
|
+
| "throws"
|
956
|
+
>
|
957
|
+
{}
|
958
|
+
/**
|
959
|
+
* An instance of `SuiteContext` is passed to each suite function in order to
|
960
|
+
* interact with the test runner. However, the `SuiteContext` constructor is not
|
961
|
+
* exposed as part of the API.
|
962
|
+
* @since v18.7.0, v16.17.0
|
324
963
|
*/
|
325
|
-
|
964
|
+
interface SuiteContext {
|
965
|
+
/**
|
966
|
+
* The name of the suite.
|
967
|
+
* @since v18.8.0, v16.18.0
|
968
|
+
*/
|
969
|
+
readonly name: string;
|
970
|
+
/**
|
971
|
+
* Can be used to abort test subtasks when the test has been aborted.
|
972
|
+
* @since v18.7.0, v16.17.0
|
973
|
+
*/
|
974
|
+
readonly signal: AbortSignal;
|
975
|
+
}
|
976
|
+
interface TestOptions {
|
977
|
+
/**
|
978
|
+
* If a number is provided, then that many tests would run in parallel.
|
979
|
+
* If truthy, it would run (number of cpu cores - 1) tests in parallel.
|
980
|
+
* For subtests, it will be `Infinity` tests in parallel.
|
981
|
+
* If falsy, it would only run one test at a time.
|
982
|
+
* If unspecified, subtests inherit this value from their parent.
|
983
|
+
* @default false
|
984
|
+
*/
|
985
|
+
concurrency?: number | boolean | undefined;
|
986
|
+
/**
|
987
|
+
* If truthy, and the test context is configured to run `only` tests, then this test will be
|
988
|
+
* run. Otherwise, the test is skipped.
|
989
|
+
* @default false
|
990
|
+
*/
|
991
|
+
only?: boolean | undefined;
|
992
|
+
/**
|
993
|
+
* Allows aborting an in-progress test.
|
994
|
+
* @since v18.8.0
|
995
|
+
*/
|
996
|
+
signal?: AbortSignal | undefined;
|
997
|
+
/**
|
998
|
+
* If truthy, the test is skipped. If a string is provided, that string is displayed in the
|
999
|
+
* test results as the reason for skipping the test.
|
1000
|
+
* @default false
|
1001
|
+
*/
|
1002
|
+
skip?: boolean | string | undefined;
|
1003
|
+
/**
|
1004
|
+
* A number of milliseconds the test will fail after. If unspecified, subtests inherit this
|
1005
|
+
* value from their parent.
|
1006
|
+
* @default Infinity
|
1007
|
+
* @since v18.7.0
|
1008
|
+
*/
|
1009
|
+
timeout?: number | undefined;
|
1010
|
+
/**
|
1011
|
+
* If truthy, the test marked as `TODO`. If a string is provided, that string is displayed in
|
1012
|
+
* the test results as the reason why the test is `TODO`.
|
1013
|
+
* @default false
|
1014
|
+
*/
|
1015
|
+
todo?: boolean | string | undefined;
|
1016
|
+
/**
|
1017
|
+
* The number of assertions and subtests expected to be run in the test.
|
1018
|
+
* If the number of assertions run in the test does not match the number
|
1019
|
+
* specified in the plan, the test will fail.
|
1020
|
+
* @default undefined
|
1021
|
+
* @since v20.15.0
|
1022
|
+
*/
|
1023
|
+
plan?: number | undefined;
|
1024
|
+
}
|
326
1025
|
/**
|
327
|
-
*
|
1026
|
+
* This function creates a hook that runs before executing a suite.
|
1027
|
+
*
|
1028
|
+
* ```js
|
1029
|
+
* describe('tests', async () => {
|
1030
|
+
* before(() => console.log('about to run some test'));
|
1031
|
+
* it('is a subtest', () => {
|
1032
|
+
* assert.ok('some relevant assertion here');
|
1033
|
+
* });
|
1034
|
+
* });
|
1035
|
+
* ```
|
1036
|
+
* @since v18.8.0, v16.18.0
|
1037
|
+
* @param fn The hook function. If the hook uses callbacks, the callback function is passed as the second argument.
|
1038
|
+
* @param options Configuration options for the hook.
|
328
1039
|
*/
|
329
|
-
|
1040
|
+
function before(fn?: HookFn, options?: HookOptions): void;
|
330
1041
|
/**
|
331
|
-
*
|
332
|
-
*
|
1042
|
+
* This function creates a hook that runs after executing a suite.
|
1043
|
+
*
|
1044
|
+
* ```js
|
1045
|
+
* describe('tests', async () => {
|
1046
|
+
* after(() => console.log('finished running tests'));
|
1047
|
+
* it('is a subtest', () => {
|
1048
|
+
* assert.ok('some relevant assertion here');
|
1049
|
+
* });
|
1050
|
+
* });
|
1051
|
+
* ```
|
1052
|
+
* @since v18.8.0, v16.18.0
|
1053
|
+
* @param fn The hook function. If the hook uses callbacks, the callback function is passed as the second argument.
|
1054
|
+
* @param options Configuration options for the hook.
|
333
1055
|
*/
|
334
|
-
|
1056
|
+
function after(fn?: HookFn, options?: HookOptions): void;
|
335
1057
|
/**
|
336
|
-
*
|
1058
|
+
* This function creates a hook that runs before each test in the current suite.
|
1059
|
+
*
|
1060
|
+
* ```js
|
1061
|
+
* describe('tests', async () => {
|
1062
|
+
* beforeEach(() => console.log('about to run a test'));
|
1063
|
+
* it('is a subtest', () => {
|
1064
|
+
* assert.ok('some relevant assertion here');
|
1065
|
+
* });
|
1066
|
+
* });
|
1067
|
+
* ```
|
1068
|
+
* @since v18.8.0, v16.18.0
|
1069
|
+
* @param fn The hook function. If the hook uses callbacks, the callback function is passed as the second argument.
|
1070
|
+
* @param options Configuration options for the hook.
|
337
1071
|
*/
|
338
|
-
|
1072
|
+
function beforeEach(fn?: HookFn, options?: HookOptions): void;
|
339
1073
|
/**
|
340
|
-
*
|
341
|
-
*
|
342
|
-
*
|
1074
|
+
* This function creates a hook that runs after each test in the current suite.
|
1075
|
+
* The `afterEach()` hook is run even if the test fails.
|
1076
|
+
*
|
1077
|
+
* ```js
|
1078
|
+
* describe('tests', async () => {
|
1079
|
+
* afterEach(() => console.log('finished running a test'));
|
1080
|
+
* it('is a subtest', () => {
|
1081
|
+
* assert.ok('some relevant assertion here');
|
1082
|
+
* });
|
1083
|
+
* });
|
1084
|
+
* ```
|
1085
|
+
* @since v18.8.0, v16.18.0
|
1086
|
+
* @param fn The hook function. If the hook uses callbacks, the callback function is passed as the second argument.
|
1087
|
+
* @param options Configuration options for the hook.
|
343
1088
|
*/
|
344
|
-
|
1089
|
+
function afterEach(fn?: HookFn, options?: HookOptions): void;
|
345
1090
|
/**
|
346
|
-
* The
|
347
|
-
* If
|
348
|
-
* @default Infinity
|
1091
|
+
* The hook function. The first argument is the context in which the hook is called.
|
1092
|
+
* If the hook uses callbacks, the callback function is passed as the second argument.
|
349
1093
|
*/
|
350
|
-
|
1094
|
+
type HookFn = (c: TestContext | SuiteContext, done: (result?: any) => void) => any;
|
351
1095
|
/**
|
352
|
-
*
|
353
|
-
*
|
1096
|
+
* The hook function. The first argument is a `TestContext` object.
|
1097
|
+
* If the hook uses callbacks, the callback function is passed as the second argument.
|
354
1098
|
*/
|
355
|
-
|
1099
|
+
type TestContextHookFn = (t: TestContext, done: (result?: any) => void) => any;
|
356
1100
|
/**
|
357
|
-
*
|
358
|
-
* @
|
1101
|
+
* Configuration options for hooks.
|
1102
|
+
* @since v18.8.0
|
359
1103
|
*/
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
prependListener(event: "test:coverage", listener: (data: TestCoverage) => void): this;
|
422
|
-
prependListener(event: "test:complete", listener: (data: TestComplete) => void): this;
|
423
|
-
prependListener(event: "test:dequeue", listener: (data: TestDequeue) => void): this;
|
424
|
-
prependListener(event: "test:diagnostic", listener: (data: DiagnosticData) => void): this;
|
425
|
-
prependListener(event: "test:enqueue", listener: (data: TestEnqueue) => void): this;
|
426
|
-
prependListener(event: "test:fail", listener: (data: TestFail) => void): this;
|
427
|
-
prependListener(event: "test:pass", listener: (data: TestPass) => void): this;
|
428
|
-
prependListener(event: "test:plan", listener: (data: TestPlan) => void): this;
|
429
|
-
prependListener(event: "test:start", listener: (data: TestStart) => void): this;
|
430
|
-
prependListener(event: "test:stderr", listener: (data: TestStderr) => void): this;
|
431
|
-
prependListener(event: "test:stdout", listener: (data: TestStdout) => void): this;
|
432
|
-
prependListener(event: "test:watch:drained", listener: () => void): this;
|
433
|
-
prependListener(event: string, listener: (...args: any[]) => void): this;
|
434
|
-
prependOnceListener(event: "test:coverage", listener: (data: TestCoverage) => void): this;
|
435
|
-
prependOnceListener(event: "test:complete", listener: (data: TestComplete) => void): this;
|
436
|
-
prependOnceListener(event: "test:dequeue", listener: (data: TestDequeue) => void): this;
|
437
|
-
prependOnceListener(event: "test:diagnostic", listener: (data: DiagnosticData) => void): this;
|
438
|
-
prependOnceListener(event: "test:enqueue", listener: (data: TestEnqueue) => void): this;
|
439
|
-
prependOnceListener(event: "test:fail", listener: (data: TestFail) => void): this;
|
440
|
-
prependOnceListener(event: "test:pass", listener: (data: TestPass) => void): this;
|
441
|
-
prependOnceListener(event: "test:plan", listener: (data: TestPlan) => void): this;
|
442
|
-
prependOnceListener(event: "test:start", listener: (data: TestStart) => void): this;
|
443
|
-
prependOnceListener(event: "test:stderr", listener: (data: TestStderr) => void): this;
|
444
|
-
prependOnceListener(event: "test:stdout", listener: (data: TestStdout) => void): this;
|
445
|
-
prependOnceListener(event: "test:watch:drained", listener: () => void): this;
|
446
|
-
prependOnceListener(event: string, listener: (...args: any[]) => void): this;
|
447
|
-
}
|
448
|
-
/**
|
449
|
-
* An instance of `TestContext` is passed to each test function in order to
|
450
|
-
* interact with the test runner. However, the `TestContext` constructor is not
|
451
|
-
* exposed as part of the API.
|
452
|
-
* @since v18.0.0, v16.17.0
|
453
|
-
*/
|
454
|
-
class TestContext {
|
455
|
-
/**
|
456
|
-
* An object containing assertion methods bound to the test context.
|
457
|
-
* The top-level functions from the `node:assert` module are exposed here for the purpose of creating test plans.
|
458
|
-
*
|
459
|
-
* **Note:** Some of the functions from `node:assert` contain type assertions. If these are called via the
|
460
|
-
* TestContext `assert` object, then the context parameter in the test's function signature **must be explicitly typed**
|
461
|
-
* (ie. the parameter must have a type annotation), otherwise an error will be raised by the TypeScript compiler:
|
462
|
-
* ```ts
|
463
|
-
* import { test, type TestContext } from 'node:test';
|
464
|
-
*
|
465
|
-
* // The test function's context parameter must have a type annotation.
|
466
|
-
* test('example', (t: TestContext) => {
|
467
|
-
* t.assert.deepStrictEqual(actual, expected);
|
468
|
-
* });
|
469
|
-
*
|
470
|
-
* // Omitting the type annotation will result in a compilation error.
|
471
|
-
* test('example', t => {
|
472
|
-
* t.assert.deepStrictEqual(actual, expected); // Error: 't' needs an explicit type annotation.
|
473
|
-
* });
|
474
|
-
* ```
|
475
|
-
* @since v20.15.0
|
476
|
-
*/
|
477
|
-
readonly assert: TestContextAssert;
|
478
|
-
/**
|
479
|
-
* This function is used to create a hook running before subtest of the current test.
|
480
|
-
* @param fn The hook function. The first argument to this function is a `TestContext` object.
|
481
|
-
* If the hook uses callbacks, the callback function is passed as the second argument.
|
482
|
-
* @param options Configuration options for the hook.
|
483
|
-
* @since v20.1.0, v18.17.0
|
484
|
-
*/
|
485
|
-
before(fn?: TestContextHookFn, options?: HookOptions): void;
|
486
|
-
/**
|
487
|
-
* This function is used to create a hook running before each subtest of the current test.
|
488
|
-
* @param fn The hook function. The first argument to this function is a `TestContext` object.
|
489
|
-
* If the hook uses callbacks, the callback function is passed as the second argument.
|
490
|
-
* @param options Configuration options for the hook.
|
491
|
-
* @since v18.8.0
|
492
|
-
*/
|
493
|
-
beforeEach(fn?: TestContextHookFn, options?: HookOptions): void;
|
494
|
-
/**
|
495
|
-
* This function is used to create a hook that runs after the current test finishes.
|
496
|
-
* @param fn The hook function. The first argument to this function is a `TestContext` object.
|
497
|
-
* If the hook uses callbacks, the callback function is passed as the second argument.
|
498
|
-
* @param options Configuration options for the hook.
|
499
|
-
* @since v18.13.0
|
500
|
-
*/
|
501
|
-
after(fn?: TestContextHookFn, options?: HookOptions): void;
|
502
|
-
/**
|
503
|
-
* This function is used to create a hook running after each subtest of the current test.
|
504
|
-
* @param fn The hook function. The first argument to this function is a `TestContext` object.
|
505
|
-
* If the hook uses callbacks, the callback function is passed as the second argument.
|
506
|
-
* @param options Configuration options for the hook.
|
507
|
-
* @since v18.8.0
|
508
|
-
*/
|
509
|
-
afterEach(fn?: TestContextHookFn, options?: HookOptions): void;
|
510
|
-
/**
|
511
|
-
* This function is used to write diagnostics to the output. Any diagnostic
|
512
|
-
* information is included at the end of the test's results. This function does
|
513
|
-
* not return a value.
|
514
|
-
*
|
515
|
-
* ```js
|
516
|
-
* test('top level test', (t) => {
|
517
|
-
* t.diagnostic('A diagnostic message');
|
518
|
-
* });
|
519
|
-
* ```
|
520
|
-
* @since v18.0.0, v16.17.0
|
521
|
-
* @param message Message to be reported.
|
522
|
-
*/
|
523
|
-
diagnostic(message: string): void;
|
524
|
-
/**
|
525
|
-
* The name of the test and each of its ancestors, separated by `>`.
|
526
|
-
* @since v20.16.0
|
527
|
-
*/
|
528
|
-
readonly fullName: string;
|
529
|
-
/**
|
530
|
-
* The name of the test.
|
531
|
-
* @since v18.8.0, v16.18.0
|
532
|
-
*/
|
533
|
-
readonly name: string;
|
534
|
-
/**
|
535
|
-
* Used to set the number of assertions and subtests that are expected to run within the test.
|
536
|
-
* If the number of assertions and subtests that run does not match the expected count, the test will fail.
|
537
|
-
*
|
538
|
-
* To make sure assertions are tracked, the assert functions on `context.assert` must be used,
|
539
|
-
* instead of importing from the `node:assert` module.
|
540
|
-
* ```js
|
541
|
-
* test('top level test', (t) => {
|
542
|
-
* t.plan(2);
|
543
|
-
* t.assert.ok('some relevant assertion here');
|
544
|
-
* t.test('subtest', () => {});
|
545
|
-
* });
|
546
|
-
* ```
|
547
|
-
*
|
548
|
-
* When working with asynchronous code, the `plan` function can be used to ensure that the correct number of assertions are run:
|
549
|
-
* ```js
|
550
|
-
* test('planning with streams', (t, done) => {
|
551
|
-
* function* generate() {
|
552
|
-
* yield 'a';
|
553
|
-
* yield 'b';
|
554
|
-
* yield 'c';
|
555
|
-
* }
|
556
|
-
* const expected = ['a', 'b', 'c'];
|
557
|
-
* t.plan(expected.length);
|
558
|
-
* const stream = Readable.from(generate());
|
559
|
-
* stream.on('data', (chunk) => {
|
560
|
-
* t.assert.strictEqual(chunk, expected.shift());
|
561
|
-
* });
|
562
|
-
* stream.on('end', () => {
|
563
|
-
* done();
|
564
|
-
* });
|
565
|
-
* });
|
566
|
-
* ```
|
567
|
-
* @since v20.15.0
|
568
|
-
*/
|
569
|
-
plan(count: number): void;
|
570
|
-
/**
|
571
|
-
* If `shouldRunOnlyTests` is truthy, the test context will only run tests that
|
572
|
-
* have the `only` option set. Otherwise, all tests are run. If Node.js was not
|
573
|
-
* started with the `--test-only` command-line option, this function is a
|
574
|
-
* no-op.
|
575
|
-
*
|
576
|
-
* ```js
|
577
|
-
* test('top level test', (t) => {
|
578
|
-
* // The test context can be set to run subtests with the 'only' option.
|
579
|
-
* t.runOnly(true);
|
580
|
-
* return Promise.all([
|
581
|
-
* t.test('this subtest is now skipped'),
|
582
|
-
* t.test('this subtest is run', { only: true }),
|
583
|
-
* ]);
|
584
|
-
* });
|
585
|
-
* ```
|
586
|
-
* @since v18.0.0, v16.17.0
|
587
|
-
* @param shouldRunOnlyTests Whether or not to run `only` tests.
|
588
|
-
*/
|
589
|
-
runOnly(shouldRunOnlyTests: boolean): void;
|
590
|
-
/**
|
591
|
-
* ```js
|
592
|
-
* test('top level test', async (t) => {
|
593
|
-
* await fetch('some/uri', { signal: t.signal });
|
594
|
-
* });
|
595
|
-
* ```
|
596
|
-
* @since v18.7.0, v16.17.0
|
597
|
-
*/
|
598
|
-
readonly signal: AbortSignal;
|
599
|
-
/**
|
600
|
-
* 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
|
601
|
-
* not terminate execution of the test function. This function does not return a
|
602
|
-
* value.
|
603
|
-
*
|
604
|
-
* ```js
|
605
|
-
* test('top level test', (t) => {
|
606
|
-
* // Make sure to return here as well if the test contains additional logic.
|
607
|
-
* t.skip('this is skipped');
|
608
|
-
* });
|
609
|
-
* ```
|
610
|
-
* @since v18.0.0, v16.17.0
|
611
|
-
* @param message Optional skip message.
|
612
|
-
*/
|
613
|
-
skip(message?: string): void;
|
614
|
-
/**
|
615
|
-
* This function adds a `TODO` directive to the test's output. If `message` is
|
616
|
-
* provided, it is included in the output. Calling `todo()` does not terminate
|
617
|
-
* execution of the test function. This function does not return a value.
|
618
|
-
*
|
619
|
-
* ```js
|
620
|
-
* test('top level test', (t) => {
|
621
|
-
* // This test is marked as `TODO`
|
622
|
-
* t.todo('this is a todo');
|
623
|
-
* });
|
624
|
-
* ```
|
625
|
-
* @since v18.0.0, v16.17.0
|
626
|
-
* @param message Optional `TODO` message.
|
627
|
-
*/
|
628
|
-
todo(message?: string): void;
|
629
|
-
/**
|
630
|
-
* This function is used to create subtests under the current test. This function behaves in
|
631
|
-
* the same fashion as the top level {@link test} function.
|
632
|
-
* @since v18.0.0
|
633
|
-
* @param name The name of the test, which is displayed when reporting test results.
|
634
|
-
* Defaults to the `name` property of `fn`, or `'<anonymous>'` if `fn` does not have a name.
|
635
|
-
* @param options Configuration options for the test.
|
636
|
-
* @param fn The function under test. This first argument to this function is a {@link TestContext} object.
|
637
|
-
* If the test uses callbacks, the callback function is passed as the second argument.
|
638
|
-
* @returns A {@link Promise} resolved with `undefined` once the test completes.
|
639
|
-
*/
|
640
|
-
test: typeof test;
|
641
|
-
/**
|
642
|
-
* Each test provides its own MockTracker instance.
|
643
|
-
*/
|
644
|
-
readonly mock: MockTracker;
|
645
|
-
}
|
646
|
-
interface TestContextAssert extends
|
647
|
-
Pick<
|
648
|
-
typeof import("assert"),
|
649
|
-
| "deepEqual"
|
650
|
-
| "deepStrictEqual"
|
651
|
-
| "doesNotMatch"
|
652
|
-
| "doesNotReject"
|
653
|
-
| "doesNotThrow"
|
654
|
-
| "equal"
|
655
|
-
| "fail"
|
656
|
-
| "ifError"
|
657
|
-
| "match"
|
658
|
-
| "notDeepEqual"
|
659
|
-
| "notDeepStrictEqual"
|
660
|
-
| "notEqual"
|
661
|
-
| "notStrictEqual"
|
662
|
-
| "ok"
|
663
|
-
| "rejects"
|
664
|
-
| "strictEqual"
|
665
|
-
| "throws"
|
666
|
-
>
|
667
|
-
{}
|
668
|
-
|
669
|
-
/**
|
670
|
-
* An instance of `SuiteContext` is passed to each suite function in order to
|
671
|
-
* interact with the test runner. However, the `SuiteContext` constructor is not
|
672
|
-
* exposed as part of the API.
|
673
|
-
* @since v18.7.0, v16.17.0
|
674
|
-
*/
|
675
|
-
class SuiteContext {
|
676
|
-
/**
|
677
|
-
* The name of the suite.
|
678
|
-
* @since v18.8.0, v16.18.0
|
679
|
-
*/
|
680
|
-
readonly name: string;
|
681
|
-
/**
|
682
|
-
* Can be used to abort test subtasks when the test has been aborted.
|
683
|
-
* @since v18.7.0, v16.17.0
|
684
|
-
*/
|
685
|
-
readonly signal: AbortSignal;
|
686
|
-
}
|
687
|
-
interface TestOptions {
|
688
|
-
/**
|
689
|
-
* If a number is provided, then that many tests would run in parallel.
|
690
|
-
* If truthy, it would run (number of cpu cores - 1) tests in parallel.
|
691
|
-
* For subtests, it will be `Infinity` tests in parallel.
|
692
|
-
* If falsy, it would only run one test at a time.
|
693
|
-
* If unspecified, subtests inherit this value from their parent.
|
694
|
-
* @default false
|
695
|
-
*/
|
696
|
-
concurrency?: number | boolean | undefined;
|
697
|
-
/**
|
698
|
-
* If truthy, and the test context is configured to run `only` tests, then this test will be
|
699
|
-
* run. Otherwise, the test is skipped.
|
700
|
-
* @default false
|
701
|
-
*/
|
702
|
-
only?: boolean | undefined;
|
703
|
-
/**
|
704
|
-
* Allows aborting an in-progress test.
|
705
|
-
* @since v18.8.0
|
706
|
-
*/
|
707
|
-
signal?: AbortSignal | undefined;
|
708
|
-
/**
|
709
|
-
* If truthy, the test is skipped. If a string is provided, that string is displayed in the
|
710
|
-
* test results as the reason for skipping the test.
|
711
|
-
* @default false
|
712
|
-
*/
|
713
|
-
skip?: boolean | string | undefined;
|
714
|
-
/**
|
715
|
-
* A number of milliseconds the test will fail after. If unspecified, subtests inherit this
|
716
|
-
* value from their parent.
|
717
|
-
* @default Infinity
|
718
|
-
* @since v18.7.0
|
719
|
-
*/
|
720
|
-
timeout?: number | undefined;
|
721
|
-
/**
|
722
|
-
* If truthy, the test marked as `TODO`. If a string is provided, that string is displayed in
|
723
|
-
* the test results as the reason why the test is `TODO`.
|
724
|
-
* @default false
|
725
|
-
*/
|
726
|
-
todo?: boolean | string | undefined;
|
727
|
-
/**
|
728
|
-
* The number of assertions and subtests expected to be run in the test.
|
729
|
-
* If the number of assertions run in the test does not match the number
|
730
|
-
* specified in the plan, the test will fail.
|
731
|
-
* @default undefined
|
732
|
-
* @since v20.15.0
|
733
|
-
*/
|
734
|
-
plan?: number | undefined;
|
735
|
-
}
|
736
|
-
/**
|
737
|
-
* This function creates a hook that runs before executing a suite.
|
738
|
-
*
|
739
|
-
* ```js
|
740
|
-
* describe('tests', async () => {
|
741
|
-
* before(() => console.log('about to run some test'));
|
742
|
-
* it('is a subtest', () => {
|
743
|
-
* assert.ok('some relevant assertion here');
|
744
|
-
* });
|
745
|
-
* });
|
746
|
-
* ```
|
747
|
-
* @since v18.8.0, v16.18.0
|
748
|
-
* @param fn The hook function. If the hook uses callbacks, the callback function is passed as the second argument.
|
749
|
-
* @param options Configuration options for the hook.
|
750
|
-
*/
|
751
|
-
function before(fn?: HookFn, options?: HookOptions): void;
|
752
|
-
/**
|
753
|
-
* This function creates a hook that runs after executing a suite.
|
754
|
-
*
|
755
|
-
* ```js
|
756
|
-
* describe('tests', async () => {
|
757
|
-
* after(() => console.log('finished running tests'));
|
758
|
-
* it('is a subtest', () => {
|
759
|
-
* assert.ok('some relevant assertion here');
|
760
|
-
* });
|
761
|
-
* });
|
762
|
-
* ```
|
763
|
-
* @since v18.8.0, v16.18.0
|
764
|
-
* @param fn The hook function. If the hook uses callbacks, the callback function is passed as the second argument.
|
765
|
-
* @param options Configuration options for the hook.
|
766
|
-
*/
|
767
|
-
function after(fn?: HookFn, options?: HookOptions): void;
|
768
|
-
/**
|
769
|
-
* This function creates a hook that runs before each test in the current suite.
|
770
|
-
*
|
771
|
-
* ```js
|
772
|
-
* describe('tests', async () => {
|
773
|
-
* beforeEach(() => console.log('about to run a test'));
|
774
|
-
* it('is a subtest', () => {
|
775
|
-
* assert.ok('some relevant assertion here');
|
776
|
-
* });
|
777
|
-
* });
|
778
|
-
* ```
|
779
|
-
* @since v18.8.0, v16.18.0
|
780
|
-
* @param fn The hook function. If the hook uses callbacks, the callback function is passed as the second argument.
|
781
|
-
* @param options Configuration options for the hook.
|
782
|
-
*/
|
783
|
-
function beforeEach(fn?: HookFn, options?: HookOptions): void;
|
784
|
-
/**
|
785
|
-
* This function creates a hook that runs after each test in the current suite.
|
786
|
-
* The `afterEach()` hook is run even if the test fails.
|
787
|
-
*
|
788
|
-
* ```js
|
789
|
-
* describe('tests', async () => {
|
790
|
-
* afterEach(() => console.log('finished running a test'));
|
791
|
-
* it('is a subtest', () => {
|
792
|
-
* assert.ok('some relevant assertion here');
|
793
|
-
* });
|
794
|
-
* });
|
795
|
-
* ```
|
796
|
-
* @since v18.8.0, v16.18.0
|
797
|
-
* @param fn The hook function. If the hook uses callbacks, the callback function is passed as the second argument.
|
798
|
-
* @param options Configuration options for the hook.
|
799
|
-
*/
|
800
|
-
function afterEach(fn?: HookFn, options?: HookOptions): void;
|
801
|
-
/**
|
802
|
-
* The hook function. The first argument is the context in which the hook is called.
|
803
|
-
* If the hook uses callbacks, the callback function is passed as the second argument.
|
804
|
-
*/
|
805
|
-
type HookFn = (c: TestContext | SuiteContext, done: (result?: any) => void) => any;
|
806
|
-
/**
|
807
|
-
* The hook function. The first argument is a `TestContext` object.
|
808
|
-
* If the hook uses callbacks, the callback function is passed as the second argument.
|
809
|
-
*/
|
810
|
-
type TestContextHookFn = (t: TestContext, done: (result?: any) => void) => any;
|
811
|
-
/**
|
812
|
-
* Configuration options for hooks.
|
813
|
-
* @since v18.8.0
|
814
|
-
*/
|
815
|
-
interface HookOptions {
|
816
|
-
/**
|
817
|
-
* Allows aborting an in-progress hook.
|
818
|
-
*/
|
819
|
-
signal?: AbortSignal | undefined;
|
820
|
-
/**
|
821
|
-
* A number of milliseconds the hook will fail after. If unspecified, subtests inherit this
|
822
|
-
* value from their parent.
|
823
|
-
* @default Infinity
|
824
|
-
*/
|
825
|
-
timeout?: number | undefined;
|
826
|
-
}
|
827
|
-
interface MockFunctionOptions {
|
828
|
-
/**
|
829
|
-
* The number of times that the mock will use the behavior of `implementation`.
|
830
|
-
* Once the mock function has been called `times` times,
|
831
|
-
* it will automatically restore the behavior of `original`.
|
832
|
-
* This value must be an integer greater than zero.
|
833
|
-
* @default Infinity
|
834
|
-
*/
|
835
|
-
times?: number | undefined;
|
836
|
-
}
|
837
|
-
interface MockMethodOptions extends MockFunctionOptions {
|
838
|
-
/**
|
839
|
-
* If `true`, `object[methodName]` is treated as a getter.
|
840
|
-
* This option cannot be used with the `setter` option.
|
841
|
-
*/
|
842
|
-
getter?: boolean | undefined;
|
843
|
-
/**
|
844
|
-
* If `true`, `object[methodName]` is treated as a setter.
|
845
|
-
* This option cannot be used with the `getter` option.
|
846
|
-
*/
|
847
|
-
setter?: boolean | undefined;
|
848
|
-
}
|
849
|
-
type Mock<F extends Function> = F & {
|
850
|
-
mock: MockFunctionContext<F>;
|
851
|
-
};
|
852
|
-
type NoOpFunction = (...args: any[]) => undefined;
|
853
|
-
type FunctionPropertyNames<T> = {
|
854
|
-
[K in keyof T]: T[K] extends Function ? K : never;
|
855
|
-
}[keyof T];
|
856
|
-
interface MockModuleOptions {
|
857
|
-
/**
|
858
|
-
* If false, each call to `require()` or `import()` generates a new mock module.
|
859
|
-
* If true, subsequent calls will return the same module mock, and the mock module is inserted into the CommonJS cache.
|
860
|
-
* @default false
|
861
|
-
*/
|
862
|
-
cache?: boolean | undefined;
|
863
|
-
/**
|
864
|
-
* The value to use as the mocked module's default export.
|
865
|
-
*
|
866
|
-
* If this value is not provided, ESM mocks do not include a default export.
|
867
|
-
* If the mock is a CommonJS or builtin module, this setting is used as the value of `module.exports`.
|
868
|
-
* If this value is not provided, CJS and builtin mocks use an empty object as the value of `module.exports`.
|
869
|
-
*/
|
870
|
-
defaultExport?: any;
|
871
|
-
/**
|
872
|
-
* An object whose keys and values are used to create the named exports of the mock module.
|
873
|
-
*
|
874
|
-
* If the mock is a CommonJS or builtin module, these values are copied onto `module.exports`.
|
875
|
-
* Therefore, if a mock is created with both named exports and a non-object default export,
|
876
|
-
* the mock will throw an exception when used as a CJS or builtin module.
|
877
|
-
*/
|
878
|
-
namedExports?: object | undefined;
|
879
|
-
}
|
880
|
-
/**
|
881
|
-
* The `MockTracker` class is used to manage mocking functionality. The test runner
|
882
|
-
* module provides a top level `mock` export which is a `MockTracker` instance.
|
883
|
-
* Each test also provides its own `MockTracker` instance via the test context's `mock` property.
|
884
|
-
* @since v19.1.0, v18.13.0
|
885
|
-
*/
|
886
|
-
class MockTracker {
|
887
|
-
/**
|
888
|
-
* This function is used to create a mock function.
|
889
|
-
*
|
890
|
-
* The following example creates a mock function that increments a counter by one
|
891
|
-
* on each invocation. The `times` option is used to modify the mock behavior such
|
892
|
-
* that the first two invocations add two to the counter instead of one.
|
893
|
-
*
|
894
|
-
* ```js
|
895
|
-
* test('mocks a counting function', (t) => {
|
896
|
-
* let cnt = 0;
|
897
|
-
*
|
898
|
-
* function addOne() {
|
899
|
-
* cnt++;
|
900
|
-
* return cnt;
|
901
|
-
* }
|
902
|
-
*
|
903
|
-
* function addTwo() {
|
904
|
-
* cnt += 2;
|
905
|
-
* return cnt;
|
906
|
-
* }
|
907
|
-
*
|
908
|
-
* const fn = t.mock.fn(addOne, addTwo, { times: 2 });
|
909
|
-
*
|
910
|
-
* assert.strictEqual(fn(), 2);
|
911
|
-
* assert.strictEqual(fn(), 4);
|
912
|
-
* assert.strictEqual(fn(), 5);
|
913
|
-
* assert.strictEqual(fn(), 6);
|
914
|
-
* });
|
915
|
-
* ```
|
916
|
-
* @since v19.1.0, v18.13.0
|
917
|
-
* @param original An optional function to create a mock on.
|
918
|
-
* @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
|
919
|
-
* then restore the behavior of `original`.
|
920
|
-
* @param options Optional configuration options for the mock function.
|
921
|
-
* @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
|
922
|
-
* behavior of the mocked function.
|
923
|
-
*/
|
924
|
-
fn<F extends Function = NoOpFunction>(original?: F, options?: MockFunctionOptions): Mock<F>;
|
925
|
-
fn<F extends Function = NoOpFunction, Implementation extends Function = F>(
|
926
|
-
original?: F,
|
927
|
-
implementation?: Implementation,
|
928
|
-
options?: MockFunctionOptions,
|
929
|
-
): Mock<F | Implementation>;
|
930
|
-
/**
|
931
|
-
* This function is used to create a mock on an existing object method. The
|
932
|
-
* following example demonstrates how a mock is created on an existing object
|
933
|
-
* method.
|
934
|
-
*
|
935
|
-
* ```js
|
936
|
-
* test('spies on an object method', (t) => {
|
937
|
-
* const number = {
|
938
|
-
* value: 5,
|
939
|
-
* subtract(a) {
|
940
|
-
* return this.value - a;
|
941
|
-
* },
|
942
|
-
* };
|
943
|
-
*
|
944
|
-
* t.mock.method(number, 'subtract');
|
945
|
-
* assert.strictEqual(number.subtract.mock.calls.length, 0);
|
946
|
-
* assert.strictEqual(number.subtract(3), 2);
|
947
|
-
* assert.strictEqual(number.subtract.mock.calls.length, 1);
|
948
|
-
*
|
949
|
-
* const call = number.subtract.mock.calls[0];
|
950
|
-
*
|
951
|
-
* assert.deepStrictEqual(call.arguments, [3]);
|
952
|
-
* assert.strictEqual(call.result, 2);
|
953
|
-
* assert.strictEqual(call.error, undefined);
|
954
|
-
* assert.strictEqual(call.target, undefined);
|
955
|
-
* assert.strictEqual(call.this, number);
|
956
|
-
* });
|
957
|
-
* ```
|
958
|
-
* @since v19.1.0, v18.13.0
|
959
|
-
* @param object The object whose method is being mocked.
|
960
|
-
* @param methodName The identifier of the method on `object` to mock. If `object[methodName]` is not a function, an error is thrown.
|
961
|
-
* @param implementation An optional function used as the mock implementation for `object[methodName]`.
|
962
|
-
* @param options Optional configuration options for the mock method.
|
963
|
-
* @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
|
964
|
-
* behavior of the mocked method.
|
965
|
-
*/
|
966
|
-
method<
|
967
|
-
MockedObject extends object,
|
968
|
-
MethodName extends FunctionPropertyNames<MockedObject>,
|
969
|
-
>(
|
970
|
-
object: MockedObject,
|
971
|
-
methodName: MethodName,
|
972
|
-
options?: MockFunctionOptions,
|
973
|
-
): MockedObject[MethodName] extends Function ? Mock<MockedObject[MethodName]>
|
974
|
-
: never;
|
975
|
-
method<
|
976
|
-
MockedObject extends object,
|
977
|
-
MethodName extends FunctionPropertyNames<MockedObject>,
|
978
|
-
Implementation extends Function,
|
979
|
-
>(
|
980
|
-
object: MockedObject,
|
981
|
-
methodName: MethodName,
|
982
|
-
implementation: Implementation,
|
983
|
-
options?: MockFunctionOptions,
|
984
|
-
): MockedObject[MethodName] extends Function ? Mock<MockedObject[MethodName] | Implementation>
|
985
|
-
: never;
|
986
|
-
method<MockedObject extends object>(
|
987
|
-
object: MockedObject,
|
988
|
-
methodName: keyof MockedObject,
|
989
|
-
options: MockMethodOptions,
|
990
|
-
): Mock<Function>;
|
991
|
-
method<MockedObject extends object>(
|
992
|
-
object: MockedObject,
|
993
|
-
methodName: keyof MockedObject,
|
994
|
-
implementation: Function,
|
995
|
-
options: MockMethodOptions,
|
996
|
-
): Mock<Function>;
|
997
|
-
|
998
|
-
/**
|
999
|
-
* This function is syntax sugar for `MockTracker.method` with `options.getter` set to `true`.
|
1000
|
-
* @since v19.3.0, v18.13.0
|
1001
|
-
*/
|
1002
|
-
getter<
|
1003
|
-
MockedObject extends object,
|
1004
|
-
MethodName extends keyof MockedObject,
|
1005
|
-
>(
|
1006
|
-
object: MockedObject,
|
1007
|
-
methodName: MethodName,
|
1008
|
-
options?: MockFunctionOptions,
|
1009
|
-
): Mock<() => MockedObject[MethodName]>;
|
1010
|
-
getter<
|
1011
|
-
MockedObject extends object,
|
1012
|
-
MethodName extends keyof MockedObject,
|
1013
|
-
Implementation extends Function,
|
1014
|
-
>(
|
1015
|
-
object: MockedObject,
|
1016
|
-
methodName: MethodName,
|
1017
|
-
implementation?: Implementation,
|
1018
|
-
options?: MockFunctionOptions,
|
1019
|
-
): Mock<(() => MockedObject[MethodName]) | Implementation>;
|
1020
|
-
/**
|
1021
|
-
* This function is syntax sugar for `MockTracker.method` with `options.setter` set to `true`.
|
1022
|
-
* @since v19.3.0, v18.13.0
|
1023
|
-
*/
|
1024
|
-
setter<
|
1025
|
-
MockedObject extends object,
|
1026
|
-
MethodName extends keyof MockedObject,
|
1027
|
-
>(
|
1028
|
-
object: MockedObject,
|
1029
|
-
methodName: MethodName,
|
1030
|
-
options?: MockFunctionOptions,
|
1031
|
-
): Mock<(value: MockedObject[MethodName]) => void>;
|
1032
|
-
setter<
|
1033
|
-
MockedObject extends object,
|
1034
|
-
MethodName extends keyof MockedObject,
|
1035
|
-
Implementation extends Function,
|
1036
|
-
>(
|
1037
|
-
object: MockedObject,
|
1038
|
-
methodName: MethodName,
|
1039
|
-
implementation?: Implementation,
|
1040
|
-
options?: MockFunctionOptions,
|
1041
|
-
): Mock<((value: MockedObject[MethodName]) => void) | Implementation>;
|
1042
|
-
|
1043
|
-
/**
|
1044
|
-
* This function is used to mock the exports of ECMAScript modules, CommonJS modules, and Node.js builtin modules.
|
1045
|
-
* Any references to the original module prior to mocking are not impacted.
|
1046
|
-
*
|
1047
|
-
* Only available through the [--experimental-test-module-mocks](https://nodejs.org/api/cli.html#--experimental-test-module-mocks) flag.
|
1048
|
-
* @since v20.18.0
|
1049
|
-
* @experimental
|
1050
|
-
* @param specifier A string identifying the module to mock.
|
1051
|
-
* @param options Optional configuration options for the mock module.
|
1052
|
-
*/
|
1053
|
-
module(specifier: string, options?: MockModuleOptions): MockModuleContext;
|
1054
|
-
|
1055
|
-
/**
|
1056
|
-
* This function restores the default behavior of all mocks that were previously
|
1057
|
-
* 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
|
1058
|
-
* used to reset their behavior or
|
1059
|
-
* otherwise interact with them.
|
1060
|
-
*
|
1061
|
-
* After each test completes, this function is called on the test context's `MockTracker`. If the global `MockTracker` is used extensively, calling this
|
1062
|
-
* function manually is recommended.
|
1063
|
-
* @since v19.1.0, v18.13.0
|
1064
|
-
*/
|
1065
|
-
reset(): void;
|
1066
|
-
/**
|
1067
|
-
* This function restores the default behavior of all mocks that were previously
|
1068
|
-
* created by this `MockTracker`. Unlike `mock.reset()`, `mock.restoreAll()` does
|
1069
|
-
* not disassociate the mocks from the `MockTracker` instance.
|
1070
|
-
* @since v19.1.0, v18.13.0
|
1071
|
-
*/
|
1072
|
-
restoreAll(): void;
|
1073
|
-
timers: MockTimers;
|
1074
|
-
}
|
1075
|
-
const mock: MockTracker;
|
1076
|
-
interface MockFunctionCall<
|
1077
|
-
F extends Function,
|
1078
|
-
ReturnType = F extends (...args: any) => infer T ? T
|
1079
|
-
: F extends abstract new(...args: any) => infer T ? T
|
1080
|
-
: unknown,
|
1081
|
-
Args = F extends (...args: infer Y) => any ? Y
|
1082
|
-
: F extends abstract new(...args: infer Y) => any ? Y
|
1083
|
-
: unknown[],
|
1084
|
-
> {
|
1085
|
-
/**
|
1086
|
-
* An array of the arguments passed to the mock function.
|
1087
|
-
*/
|
1088
|
-
arguments: Args;
|
1089
|
-
/**
|
1090
|
-
* If the mocked function threw then this property contains the thrown value.
|
1091
|
-
*/
|
1092
|
-
error: unknown | undefined;
|
1093
|
-
/**
|
1094
|
-
* The value returned by the mocked function.
|
1095
|
-
*
|
1096
|
-
* If the mocked function threw, it will be `undefined`.
|
1097
|
-
*/
|
1098
|
-
result: ReturnType | undefined;
|
1099
|
-
/**
|
1100
|
-
* An `Error` object whose stack can be used to determine the callsite of the mocked function invocation.
|
1101
|
-
*/
|
1102
|
-
stack: Error;
|
1103
|
-
/**
|
1104
|
-
* If the mocked function is a constructor, this field contains the class being constructed.
|
1105
|
-
* Otherwise this will be `undefined`.
|
1106
|
-
*/
|
1107
|
-
target: F extends abstract new(...args: any) => any ? F : undefined;
|
1108
|
-
/**
|
1109
|
-
* The mocked function's `this` value.
|
1110
|
-
*/
|
1111
|
-
this: unknown;
|
1112
|
-
}
|
1113
|
-
/**
|
1114
|
-
* The `MockFunctionContext` class is used to inspect or manipulate the behavior of
|
1115
|
-
* mocks created via the `MockTracker` APIs.
|
1116
|
-
* @since v19.1.0, v18.13.0
|
1117
|
-
*/
|
1118
|
-
class MockFunctionContext<F extends Function> {
|
1119
|
-
/**
|
1120
|
-
* A getter that returns a copy of the internal array used to track calls to the
|
1121
|
-
* mock. Each entry in the array is an object with the following properties.
|
1122
|
-
* @since v19.1.0, v18.13.0
|
1123
|
-
*/
|
1124
|
-
readonly calls: Array<MockFunctionCall<F>>;
|
1125
|
-
/**
|
1126
|
-
* This function returns the number of times that this mock has been invoked. This
|
1127
|
-
* 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.
|
1128
|
-
* @since v19.1.0, v18.13.0
|
1129
|
-
* @return The number of times that this mock has been invoked.
|
1130
|
-
*/
|
1131
|
-
callCount(): number;
|
1132
|
-
/**
|
1133
|
-
* This function is used to change the behavior of an existing mock.
|
1134
|
-
*
|
1135
|
-
* The following example creates a mock function using `t.mock.fn()`, calls the
|
1136
|
-
* mock function, and then changes the mock implementation to a different function.
|
1137
|
-
*
|
1138
|
-
* ```js
|
1139
|
-
* test('changes a mock behavior', (t) => {
|
1140
|
-
* let cnt = 0;
|
1141
|
-
*
|
1142
|
-
* function addOne() {
|
1143
|
-
* cnt++;
|
1144
|
-
* return cnt;
|
1145
|
-
* }
|
1146
|
-
*
|
1147
|
-
* function addTwo() {
|
1148
|
-
* cnt += 2;
|
1149
|
-
* return cnt;
|
1150
|
-
* }
|
1151
|
-
*
|
1152
|
-
* const fn = t.mock.fn(addOne);
|
1153
|
-
*
|
1154
|
-
* assert.strictEqual(fn(), 1);
|
1155
|
-
* fn.mock.mockImplementation(addTwo);
|
1156
|
-
* assert.strictEqual(fn(), 3);
|
1157
|
-
* assert.strictEqual(fn(), 5);
|
1158
|
-
* });
|
1159
|
-
* ```
|
1160
|
-
* @since v19.1.0, v18.13.0
|
1161
|
-
* @param implementation The function to be used as the mock's new implementation.
|
1162
|
-
*/
|
1163
|
-
mockImplementation(implementation: F): void;
|
1164
|
-
/**
|
1165
|
-
* This function is used to change the behavior of an existing mock for a single
|
1166
|
-
* invocation. Once invocation `onCall` has occurred, the mock will revert to
|
1167
|
-
* whatever behavior it would have used had `mockImplementationOnce()` not been
|
1168
|
-
* called.
|
1169
|
-
*
|
1170
|
-
* The following example creates a mock function using `t.mock.fn()`, calls the
|
1171
|
-
* mock function, changes the mock implementation to a different function for the
|
1172
|
-
* next invocation, and then resumes its previous behavior.
|
1173
|
-
*
|
1174
|
-
* ```js
|
1175
|
-
* test('changes a mock behavior once', (t) => {
|
1176
|
-
* let cnt = 0;
|
1177
|
-
*
|
1178
|
-
* function addOne() {
|
1179
|
-
* cnt++;
|
1180
|
-
* return cnt;
|
1181
|
-
* }
|
1182
|
-
*
|
1183
|
-
* function addTwo() {
|
1184
|
-
* cnt += 2;
|
1185
|
-
* return cnt;
|
1186
|
-
* }
|
1187
|
-
*
|
1188
|
-
* const fn = t.mock.fn(addOne);
|
1189
|
-
*
|
1190
|
-
* assert.strictEqual(fn(), 1);
|
1191
|
-
* fn.mock.mockImplementationOnce(addTwo);
|
1192
|
-
* assert.strictEqual(fn(), 3);
|
1193
|
-
* assert.strictEqual(fn(), 4);
|
1194
|
-
* });
|
1195
|
-
* ```
|
1196
|
-
* @since v19.1.0, v18.13.0
|
1197
|
-
* @param implementation The function to be used as the mock's implementation for the invocation number specified by `onCall`.
|
1198
|
-
* @param onCall The invocation number that will use `implementation`. If the specified invocation has already occurred then an exception is thrown.
|
1199
|
-
*/
|
1200
|
-
mockImplementationOnce(implementation: F, onCall?: number): void;
|
1201
|
-
/**
|
1202
|
-
* Resets the call history of the mock function.
|
1203
|
-
* @since v19.3.0, v18.13.0
|
1204
|
-
*/
|
1205
|
-
resetCalls(): void;
|
1104
|
+
interface HookOptions {
|
1105
|
+
/**
|
1106
|
+
* Allows aborting an in-progress hook.
|
1107
|
+
*/
|
1108
|
+
signal?: AbortSignal | undefined;
|
1109
|
+
/**
|
1110
|
+
* A number of milliseconds the hook will fail after. If unspecified, subtests inherit this
|
1111
|
+
* value from their parent.
|
1112
|
+
* @default Infinity
|
1113
|
+
*/
|
1114
|
+
timeout?: number | undefined;
|
1115
|
+
}
|
1116
|
+
interface MockFunctionOptions {
|
1117
|
+
/**
|
1118
|
+
* The number of times that the mock will use the behavior of `implementation`.
|
1119
|
+
* Once the mock function has been called `times` times,
|
1120
|
+
* it will automatically restore the behavior of `original`.
|
1121
|
+
* This value must be an integer greater than zero.
|
1122
|
+
* @default Infinity
|
1123
|
+
*/
|
1124
|
+
times?: number | undefined;
|
1125
|
+
}
|
1126
|
+
interface MockMethodOptions extends MockFunctionOptions {
|
1127
|
+
/**
|
1128
|
+
* If `true`, `object[methodName]` is treated as a getter.
|
1129
|
+
* This option cannot be used with the `setter` option.
|
1130
|
+
*/
|
1131
|
+
getter?: boolean | undefined;
|
1132
|
+
/**
|
1133
|
+
* If `true`, `object[methodName]` is treated as a setter.
|
1134
|
+
* This option cannot be used with the `getter` option.
|
1135
|
+
*/
|
1136
|
+
setter?: boolean | undefined;
|
1137
|
+
}
|
1138
|
+
type Mock<F extends Function> = F & {
|
1139
|
+
mock: MockFunctionContext<F>;
|
1140
|
+
};
|
1141
|
+
interface MockModuleOptions {
|
1142
|
+
/**
|
1143
|
+
* If false, each call to `require()` or `import()` generates a new mock module.
|
1144
|
+
* If true, subsequent calls will return the same module mock, and the mock module is inserted into the CommonJS cache.
|
1145
|
+
* @default false
|
1146
|
+
*/
|
1147
|
+
cache?: boolean | undefined;
|
1148
|
+
/**
|
1149
|
+
* The value to use as the mocked module's default export.
|
1150
|
+
*
|
1151
|
+
* If this value is not provided, ESM mocks do not include a default export.
|
1152
|
+
* If the mock is a CommonJS or builtin module, this setting is used as the value of `module.exports`.
|
1153
|
+
* If this value is not provided, CJS and builtin mocks use an empty object as the value of `module.exports`.
|
1154
|
+
*/
|
1155
|
+
defaultExport?: any;
|
1156
|
+
/**
|
1157
|
+
* An object whose keys and values are used to create the named exports of the mock module.
|
1158
|
+
*
|
1159
|
+
* If the mock is a CommonJS or builtin module, these values are copied onto `module.exports`.
|
1160
|
+
* Therefore, if a mock is created with both named exports and a non-object default export,
|
1161
|
+
* the mock will throw an exception when used as a CJS or builtin module.
|
1162
|
+
*/
|
1163
|
+
namedExports?: object | undefined;
|
1164
|
+
}
|
1206
1165
|
/**
|
1207
|
-
*
|
1208
|
-
*
|
1166
|
+
* The `MockTracker` class is used to manage mocking functionality. The test runner
|
1167
|
+
* module provides a top level `mock` export which is a `MockTracker` instance.
|
1168
|
+
* Each test also provides its own `MockTracker` instance via the test context's `mock` property.
|
1209
1169
|
* @since v19.1.0, v18.13.0
|
1210
1170
|
*/
|
1211
|
-
|
1212
|
-
}
|
1213
|
-
/**
|
1214
|
-
* @since v20.18.0
|
1215
|
-
* @experimental
|
1216
|
-
*/
|
1217
|
-
class MockModuleContext {
|
1218
|
-
/**
|
1219
|
-
* Resets the implementation of the mock module.
|
1220
|
-
* @since v20.18.0
|
1221
|
-
*/
|
1222
|
-
restore(): void;
|
1223
|
-
}
|
1224
|
-
|
1225
|
-
type Timer = "setInterval" | "setTimeout" | "setImmediate" | "Date";
|
1226
|
-
|
1227
|
-
interface MockTimersOptions {
|
1228
|
-
apis: Timer[];
|
1229
|
-
now?: number | Date | undefined;
|
1230
|
-
}
|
1231
|
-
/**
|
1232
|
-
* Mocking timers is a technique commonly used in software testing to simulate and
|
1233
|
-
* control the behavior of timers, such as `setInterval` and `setTimeout`,
|
1234
|
-
* without actually waiting for the specified time intervals.
|
1235
|
-
*
|
1236
|
-
* The MockTimers API also allows for mocking of the `Date` constructor and
|
1237
|
-
* `setImmediate`/`clearImmediate` functions.
|
1238
|
-
*
|
1239
|
-
* The `MockTracker` provides a top-level `timers` export
|
1240
|
-
* which is a `MockTimers` instance.
|
1241
|
-
* @since v20.4.0
|
1242
|
-
* @experimental
|
1243
|
-
*/
|
1244
|
-
class MockTimers {
|
1245
|
-
/**
|
1246
|
-
* Enables timer mocking for the specified timers.
|
1247
|
-
*
|
1248
|
-
* **Note:** When you enable mocking for a specific timer, its associated
|
1249
|
-
* clear function will also be implicitly mocked.
|
1250
|
-
*
|
1251
|
-
* **Note:** Mocking `Date` will affect the behavior of the mocked timers
|
1252
|
-
* as they use the same internal clock.
|
1253
|
-
*
|
1254
|
-
* Example usage without setting initial time:
|
1255
|
-
*
|
1256
|
-
* ```js
|
1257
|
-
* import { mock } from 'node:test';
|
1258
|
-
* mock.timers.enable({ apis: ['setInterval', 'Date'], now: 1234 });
|
1259
|
-
* ```
|
1260
|
-
*
|
1261
|
-
* The above example enables mocking for the `Date` constructor, `setInterval` timer and
|
1262
|
-
* implicitly mocks the `clearInterval` function. Only the `Date` constructor from `globalThis`,
|
1263
|
-
* `setInterval` and `clearInterval` functions from `node:timers`, `node:timers/promises`, and `globalThis` will be mocked.
|
1264
|
-
*
|
1265
|
-
* Example usage with initial time set
|
1266
|
-
*
|
1267
|
-
* ```js
|
1268
|
-
* import { mock } from 'node:test';
|
1269
|
-
* mock.timers.enable({ apis: ['Date'], now: 1000 });
|
1270
|
-
* ```
|
1271
|
-
*
|
1272
|
-
* Example usage with initial Date object as time set
|
1273
|
-
*
|
1274
|
-
* ```js
|
1275
|
-
* import { mock } from 'node:test';
|
1276
|
-
* mock.timers.enable({ apis: ['Date'], now: new Date() });
|
1277
|
-
* ```
|
1278
|
-
*
|
1279
|
-
* Alternatively, if you call `mock.timers.enable()` without any parameters:
|
1280
|
-
*
|
1281
|
-
* All timers (`'setInterval'`, `'clearInterval'`, `'Date'`, `'setImmediate'`, `'clearImmediate'`, `'setTimeout'`, and `'clearTimeout'`)
|
1282
|
-
* will be mocked.
|
1283
|
-
*
|
1284
|
-
* The `setInterval`, `clearInterval`, `setTimeout`, and `clearTimeout` functions from `node:timers`, `node:timers/promises`,
|
1285
|
-
* and `globalThis` will be mocked.
|
1286
|
-
* The `Date` constructor from `globalThis` will be mocked.
|
1287
|
-
*
|
1288
|
-
* If there is no initial epoch set, the initial date will be based on 0 in the Unix epoch. This is `January 1st, 1970, 00:00:00 UTC`. You can
|
1289
|
-
* set an initial date by passing a now property to the `.enable()` method. This value will be used as the initial date for the mocked Date
|
1290
|
-
* object. It can either be a positive integer, or another Date object.
|
1291
|
-
* @since v20.4.0
|
1292
|
-
*/
|
1293
|
-
enable(options?: MockTimersOptions): void;
|
1294
|
-
/**
|
1295
|
-
* You can use the `.setTime()` method to manually move the mocked date to another time. This method only accepts a positive integer.
|
1296
|
-
* Note: This method will execute any mocked timers that are in the past from the new time.
|
1297
|
-
* In the below example we are setting a new time for the mocked date.
|
1298
|
-
* ```js
|
1299
|
-
* import assert from 'node:assert';
|
1300
|
-
* import { test } from 'node:test';
|
1301
|
-
* test('sets the time of a date object', (context) => {
|
1302
|
-
* // Optionally choose what to mock
|
1303
|
-
* context.mock.timers.enable({ apis: ['Date'], now: 100 });
|
1304
|
-
* assert.strictEqual(Date.now(), 100);
|
1305
|
-
* // Advance in time will also advance the date
|
1306
|
-
* context.mock.timers.setTime(1000);
|
1307
|
-
* context.mock.timers.tick(200);
|
1308
|
-
* assert.strictEqual(Date.now(), 1200);
|
1309
|
-
* });
|
1310
|
-
* ```
|
1311
|
-
*/
|
1312
|
-
setTime(time: number): void;
|
1313
|
-
/**
|
1314
|
-
* This function restores the default behavior of all mocks that were previously
|
1315
|
-
* created by this `MockTimers` instance and disassociates the mocks
|
1316
|
-
* from the `MockTracker` instance.
|
1317
|
-
*
|
1318
|
-
* **Note:** After each test completes, this function is called on
|
1319
|
-
* the test context's `MockTracker`.
|
1320
|
-
*
|
1321
|
-
* ```js
|
1322
|
-
* import { mock } from 'node:test';
|
1323
|
-
* mock.timers.reset();
|
1324
|
-
* ```
|
1325
|
-
* @since v20.4.0
|
1326
|
-
*/
|
1327
|
-
reset(): void;
|
1328
|
-
/**
|
1329
|
-
* Advances time for all mocked timers.
|
1330
|
-
*
|
1331
|
-
* **Note:** This diverges from how `setTimeout` in Node.js behaves and accepts
|
1332
|
-
* only positive numbers. In Node.js, `setTimeout` with negative numbers is
|
1333
|
-
* only supported for web compatibility reasons.
|
1334
|
-
*
|
1335
|
-
* The following example mocks a `setTimeout` function and
|
1336
|
-
* by using `.tick` advances in
|
1337
|
-
* time triggering all pending timers.
|
1338
|
-
*
|
1339
|
-
* ```js
|
1340
|
-
* import assert from 'node:assert';
|
1341
|
-
* import { test } from 'node:test';
|
1342
|
-
*
|
1343
|
-
* test('mocks setTimeout to be executed synchronously without having to actually wait for it', (context) => {
|
1344
|
-
* const fn = context.mock.fn();
|
1345
|
-
*
|
1346
|
-
* context.mock.timers.enable({ apis: ['setTimeout'] });
|
1347
|
-
*
|
1348
|
-
* setTimeout(fn, 9999);
|
1349
|
-
*
|
1350
|
-
* assert.strictEqual(fn.mock.callCount(), 0);
|
1351
|
-
*
|
1352
|
-
* // Advance in time
|
1353
|
-
* context.mock.timers.tick(9999);
|
1354
|
-
*
|
1355
|
-
* assert.strictEqual(fn.mock.callCount(), 1);
|
1356
|
-
* });
|
1357
|
-
* ```
|
1358
|
-
*
|
1359
|
-
* Alternativelly, the `.tick` function can be called many times
|
1360
|
-
*
|
1361
|
-
* ```js
|
1362
|
-
* import assert from 'node:assert';
|
1363
|
-
* import { test } from 'node:test';
|
1364
|
-
*
|
1365
|
-
* test('mocks setTimeout to be executed synchronously without having to actually wait for it', (context) => {
|
1366
|
-
* const fn = context.mock.fn();
|
1367
|
-
* context.mock.timers.enable({ apis: ['setTimeout'] });
|
1368
|
-
* const nineSecs = 9000;
|
1369
|
-
* setTimeout(fn, nineSecs);
|
1370
|
-
*
|
1371
|
-
* const twoSeconds = 3000;
|
1372
|
-
* context.mock.timers.tick(twoSeconds);
|
1373
|
-
* context.mock.timers.tick(twoSeconds);
|
1374
|
-
* context.mock.timers.tick(twoSeconds);
|
1375
|
-
*
|
1376
|
-
* assert.strictEqual(fn.mock.callCount(), 1);
|
1377
|
-
* });
|
1378
|
-
* ```
|
1379
|
-
*
|
1380
|
-
* Advancing time using `.tick` will also advance the time for any `Date` object
|
1381
|
-
* created after the mock was enabled (if `Date` was also set to be mocked).
|
1382
|
-
*
|
1383
|
-
* ```js
|
1384
|
-
* import assert from 'node:assert';
|
1385
|
-
* import { test } from 'node:test';
|
1386
|
-
*
|
1387
|
-
* test('mocks setTimeout to be executed synchronously without having to actually wait for it', (context) => {
|
1388
|
-
* const fn = context.mock.fn();
|
1389
|
-
*
|
1390
|
-
* context.mock.timers.enable({ apis: ['setTimeout', 'Date'] });
|
1391
|
-
* setTimeout(fn, 9999);
|
1392
|
-
*
|
1393
|
-
* assert.strictEqual(fn.mock.callCount(), 0);
|
1394
|
-
* assert.strictEqual(Date.now(), 0);
|
1395
|
-
*
|
1396
|
-
* // Advance in time
|
1397
|
-
* context.mock.timers.tick(9999);
|
1398
|
-
* assert.strictEqual(fn.mock.callCount(), 1);
|
1399
|
-
* assert.strictEqual(Date.now(), 9999);
|
1400
|
-
* });
|
1401
|
-
* ```
|
1402
|
-
* @since v20.4.0
|
1403
|
-
*/
|
1404
|
-
tick(milliseconds: number): void;
|
1405
|
-
/**
|
1406
|
-
* Triggers all pending mocked timers immediately. If the `Date` object is also
|
1407
|
-
* mocked, it will also advance the `Date` object to the furthest timer's time.
|
1408
|
-
*
|
1409
|
-
* The example below triggers all pending timers immediately,
|
1410
|
-
* causing them to execute without any delay.
|
1411
|
-
*
|
1412
|
-
* ```js
|
1413
|
-
* import assert from 'node:assert';
|
1414
|
-
* import { test } from 'node:test';
|
1415
|
-
*
|
1416
|
-
* test('runAll functions following the given order', (context) => {
|
1417
|
-
* context.mock.timers.enable({ apis: ['setTimeout', 'Date'] });
|
1418
|
-
* const results = [];
|
1419
|
-
* setTimeout(() => results.push(1), 9999);
|
1420
|
-
*
|
1421
|
-
* // Notice that if both timers have the same timeout,
|
1422
|
-
* // the order of execution is guaranteed
|
1423
|
-
* setTimeout(() => results.push(3), 8888);
|
1424
|
-
* setTimeout(() => results.push(2), 8888);
|
1425
|
-
*
|
1426
|
-
* assert.deepStrictEqual(results, []);
|
1427
|
-
*
|
1428
|
-
* context.mock.timers.runAll();
|
1429
|
-
* assert.deepStrictEqual(results, [3, 2, 1]);
|
1430
|
-
* // The Date object is also advanced to the furthest timer's time
|
1431
|
-
* assert.strictEqual(Date.now(), 9999);
|
1432
|
-
* });
|
1433
|
-
* ```
|
1434
|
-
*
|
1435
|
-
* **Note:** The `runAll()` function is specifically designed for
|
1436
|
-
* triggering timers in the context of timer mocking.
|
1437
|
-
* It does not have any effect on real-time system
|
1438
|
-
* clocks or actual timers outside of the mocking environment.
|
1439
|
-
* @since v20.4.0
|
1440
|
-
*/
|
1441
|
-
runAll(): void;
|
1442
|
-
/**
|
1443
|
-
* Calls {@link MockTimers.reset()}.
|
1444
|
-
*/
|
1445
|
-
[Symbol.dispose](): void;
|
1446
|
-
}
|
1447
|
-
export {
|
1448
|
-
after,
|
1449
|
-
afterEach,
|
1450
|
-
before,
|
1451
|
-
beforeEach,
|
1452
|
-
describe,
|
1453
|
-
it,
|
1454
|
-
Mock,
|
1455
|
-
mock,
|
1456
|
-
only,
|
1457
|
-
run,
|
1458
|
-
skip,
|
1459
|
-
suite,
|
1460
|
-
SuiteContext,
|
1461
|
-
test,
|
1462
|
-
test as default,
|
1463
|
-
TestContext,
|
1464
|
-
todo,
|
1465
|
-
};
|
1466
|
-
}
|
1467
|
-
|
1468
|
-
interface TestError extends Error {
|
1469
|
-
cause: Error;
|
1470
|
-
}
|
1471
|
-
interface TestLocationInfo {
|
1472
|
-
/**
|
1473
|
-
* The column number where the test is defined, or
|
1474
|
-
* `undefined` if the test was run through the REPL.
|
1475
|
-
*/
|
1476
|
-
column?: number;
|
1477
|
-
/**
|
1478
|
-
* The path of the test file, `undefined` if test was run through the REPL.
|
1479
|
-
*/
|
1480
|
-
file?: string;
|
1481
|
-
/**
|
1482
|
-
* The line number where the test is defined, or `undefined` if the test was run through the REPL.
|
1483
|
-
*/
|
1484
|
-
line?: number;
|
1485
|
-
}
|
1486
|
-
interface DiagnosticData extends TestLocationInfo {
|
1487
|
-
/**
|
1488
|
-
* The diagnostic message.
|
1489
|
-
*/
|
1490
|
-
message: string;
|
1491
|
-
/**
|
1492
|
-
* The nesting level of the test.
|
1493
|
-
*/
|
1494
|
-
nesting: number;
|
1495
|
-
}
|
1496
|
-
interface TestCoverage {
|
1497
|
-
/**
|
1498
|
-
* An object containing the coverage report.
|
1499
|
-
*/
|
1500
|
-
summary: {
|
1501
|
-
/**
|
1502
|
-
* An array of coverage reports for individual files.
|
1503
|
-
*/
|
1504
|
-
files: Array<{
|
1171
|
+
interface MockTracker {
|
1505
1172
|
/**
|
1506
|
-
*
|
1173
|
+
* This function is used to create a mock function.
|
1174
|
+
*
|
1175
|
+
* The following example creates a mock function that increments a counter by one
|
1176
|
+
* on each invocation. The `times` option is used to modify the mock behavior such
|
1177
|
+
* that the first two invocations add two to the counter instead of one.
|
1178
|
+
*
|
1179
|
+
* ```js
|
1180
|
+
* test('mocks a counting function', (t) => {
|
1181
|
+
* let cnt = 0;
|
1182
|
+
*
|
1183
|
+
* function addOne() {
|
1184
|
+
* cnt++;
|
1185
|
+
* return cnt;
|
1186
|
+
* }
|
1187
|
+
*
|
1188
|
+
* function addTwo() {
|
1189
|
+
* cnt += 2;
|
1190
|
+
* return cnt;
|
1191
|
+
* }
|
1192
|
+
*
|
1193
|
+
* const fn = t.mock.fn(addOne, addTwo, { times: 2 });
|
1194
|
+
*
|
1195
|
+
* assert.strictEqual(fn(), 2);
|
1196
|
+
* assert.strictEqual(fn(), 4);
|
1197
|
+
* assert.strictEqual(fn(), 5);
|
1198
|
+
* assert.strictEqual(fn(), 6);
|
1199
|
+
* });
|
1200
|
+
* ```
|
1201
|
+
* @since v19.1.0, v18.13.0
|
1202
|
+
* @param original An optional function to create a mock on.
|
1203
|
+
* @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
|
1204
|
+
* then restore the behavior of `original`.
|
1205
|
+
* @param options Optional configuration options for the mock function.
|
1206
|
+
* @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
|
1207
|
+
* behavior of the mocked function.
|
1507
1208
|
*/
|
1508
|
-
|
1209
|
+
fn<F extends Function = (...args: any[]) => undefined>(
|
1210
|
+
original?: F,
|
1211
|
+
options?: MockFunctionOptions,
|
1212
|
+
): Mock<F>;
|
1213
|
+
fn<F extends Function = (...args: any[]) => undefined, Implementation extends Function = F>(
|
1214
|
+
original?: F,
|
1215
|
+
implementation?: Implementation,
|
1216
|
+
options?: MockFunctionOptions,
|
1217
|
+
): Mock<F | Implementation>;
|
1509
1218
|
/**
|
1510
|
-
*
|
1219
|
+
* This function is used to create a mock on an existing object method. The
|
1220
|
+
* following example demonstrates how a mock is created on an existing object
|
1221
|
+
* method.
|
1222
|
+
*
|
1223
|
+
* ```js
|
1224
|
+
* test('spies on an object method', (t) => {
|
1225
|
+
* const number = {
|
1226
|
+
* value: 5,
|
1227
|
+
* subtract(a) {
|
1228
|
+
* return this.value - a;
|
1229
|
+
* },
|
1230
|
+
* };
|
1231
|
+
*
|
1232
|
+
* t.mock.method(number, 'subtract');
|
1233
|
+
* assert.strictEqual(number.subtract.mock.calls.length, 0);
|
1234
|
+
* assert.strictEqual(number.subtract(3), 2);
|
1235
|
+
* assert.strictEqual(number.subtract.mock.calls.length, 1);
|
1236
|
+
*
|
1237
|
+
* const call = number.subtract.mock.calls[0];
|
1238
|
+
*
|
1239
|
+
* assert.deepStrictEqual(call.arguments, [3]);
|
1240
|
+
* assert.strictEqual(call.result, 2);
|
1241
|
+
* assert.strictEqual(call.error, undefined);
|
1242
|
+
* assert.strictEqual(call.target, undefined);
|
1243
|
+
* assert.strictEqual(call.this, number);
|
1244
|
+
* });
|
1245
|
+
* ```
|
1246
|
+
* @since v19.1.0, v18.13.0
|
1247
|
+
* @param object The object whose method is being mocked.
|
1248
|
+
* @param methodName The identifier of the method on `object` to mock. If `object[methodName]` is not a function, an error is thrown.
|
1249
|
+
* @param implementation An optional function used as the mock implementation for `object[methodName]`.
|
1250
|
+
* @param options Optional configuration options for the mock method.
|
1251
|
+
* @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
|
1252
|
+
* behavior of the mocked method.
|
1511
1253
|
*/
|
1512
|
-
|
1254
|
+
method<
|
1255
|
+
MockedObject extends object,
|
1256
|
+
MethodName extends FunctionPropertyNames<MockedObject>,
|
1257
|
+
>(
|
1258
|
+
object: MockedObject,
|
1259
|
+
methodName: MethodName,
|
1260
|
+
options?: MockFunctionOptions,
|
1261
|
+
): MockedObject[MethodName] extends Function ? Mock<MockedObject[MethodName]>
|
1262
|
+
: never;
|
1263
|
+
method<
|
1264
|
+
MockedObject extends object,
|
1265
|
+
MethodName extends FunctionPropertyNames<MockedObject>,
|
1266
|
+
Implementation extends Function,
|
1267
|
+
>(
|
1268
|
+
object: MockedObject,
|
1269
|
+
methodName: MethodName,
|
1270
|
+
implementation: Implementation,
|
1271
|
+
options?: MockFunctionOptions,
|
1272
|
+
): MockedObject[MethodName] extends Function ? Mock<MockedObject[MethodName] | Implementation>
|
1273
|
+
: never;
|
1274
|
+
method<MockedObject extends object>(
|
1275
|
+
object: MockedObject,
|
1276
|
+
methodName: keyof MockedObject,
|
1277
|
+
options: MockMethodOptions,
|
1278
|
+
): Mock<Function>;
|
1279
|
+
method<MockedObject extends object>(
|
1280
|
+
object: MockedObject,
|
1281
|
+
methodName: keyof MockedObject,
|
1282
|
+
implementation: Function,
|
1283
|
+
options: MockMethodOptions,
|
1284
|
+
): Mock<Function>;
|
1513
1285
|
/**
|
1514
|
-
*
|
1286
|
+
* This function is syntax sugar for `MockTracker.method` with `options.getter` set to `true`.
|
1287
|
+
* @since v19.3.0, v18.13.0
|
1515
1288
|
*/
|
1516
|
-
|
1289
|
+
getter<
|
1290
|
+
MockedObject extends object,
|
1291
|
+
MethodName extends keyof MockedObject,
|
1292
|
+
>(
|
1293
|
+
object: MockedObject,
|
1294
|
+
methodName: MethodName,
|
1295
|
+
options?: MockFunctionOptions,
|
1296
|
+
): Mock<() => MockedObject[MethodName]>;
|
1297
|
+
getter<
|
1298
|
+
MockedObject extends object,
|
1299
|
+
MethodName extends keyof MockedObject,
|
1300
|
+
Implementation extends Function,
|
1301
|
+
>(
|
1302
|
+
object: MockedObject,
|
1303
|
+
methodName: MethodName,
|
1304
|
+
implementation?: Implementation,
|
1305
|
+
options?: MockFunctionOptions,
|
1306
|
+
): Mock<(() => MockedObject[MethodName]) | Implementation>;
|
1517
1307
|
/**
|
1518
|
-
*
|
1308
|
+
* This function is syntax sugar for `MockTracker.method` with `options.setter` set to `true`.
|
1309
|
+
* @since v19.3.0, v18.13.0
|
1519
1310
|
*/
|
1520
|
-
|
1311
|
+
setter<
|
1312
|
+
MockedObject extends object,
|
1313
|
+
MethodName extends keyof MockedObject,
|
1314
|
+
>(
|
1315
|
+
object: MockedObject,
|
1316
|
+
methodName: MethodName,
|
1317
|
+
options?: MockFunctionOptions,
|
1318
|
+
): Mock<(value: MockedObject[MethodName]) => void>;
|
1319
|
+
setter<
|
1320
|
+
MockedObject extends object,
|
1321
|
+
MethodName extends keyof MockedObject,
|
1322
|
+
Implementation extends Function,
|
1323
|
+
>(
|
1324
|
+
object: MockedObject,
|
1325
|
+
methodName: MethodName,
|
1326
|
+
implementation?: Implementation,
|
1327
|
+
options?: MockFunctionOptions,
|
1328
|
+
): Mock<((value: MockedObject[MethodName]) => void) | Implementation>;
|
1521
1329
|
/**
|
1522
|
-
*
|
1330
|
+
* This function is used to mock the exports of ECMAScript modules, CommonJS modules, and Node.js builtin modules.
|
1331
|
+
* Any references to the original module prior to mocking are not impacted.
|
1332
|
+
*
|
1333
|
+
* Only available through the [--experimental-test-module-mocks](https://nodejs.org/api/cli.html#--experimental-test-module-mocks) flag.
|
1334
|
+
* @since v20.18.0
|
1335
|
+
* @experimental
|
1336
|
+
* @param specifier A string identifying the module to mock.
|
1337
|
+
* @param options Optional configuration options for the mock module.
|
1523
1338
|
*/
|
1524
|
-
|
1339
|
+
module(specifier: string, options?: MockModuleOptions): MockModuleContext;
|
1525
1340
|
/**
|
1526
|
-
*
|
1341
|
+
* This function restores the default behavior of all mocks that were previously
|
1342
|
+
* 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
|
1343
|
+
* used to reset their behavior or
|
1344
|
+
* otherwise interact with them.
|
1345
|
+
*
|
1346
|
+
* After each test completes, this function is called on the test context's `MockTracker`. If the global `MockTracker` is used extensively, calling this
|
1347
|
+
* function manually is recommended.
|
1348
|
+
* @since v19.1.0, v18.13.0
|
1527
1349
|
*/
|
1528
|
-
|
1350
|
+
reset(): void;
|
1529
1351
|
/**
|
1530
|
-
*
|
1352
|
+
* This function restores the default behavior of all mocks that were previously
|
1353
|
+
* created by this `MockTracker`. Unlike `mock.reset()`, `mock.restoreAll()` does
|
1354
|
+
* not disassociate the mocks from the `MockTracker` instance.
|
1355
|
+
* @since v19.1.0, v18.13.0
|
1531
1356
|
*/
|
1532
|
-
|
1357
|
+
restoreAll(): void;
|
1358
|
+
readonly timers: MockTimers;
|
1359
|
+
}
|
1360
|
+
const mock: MockTracker;
|
1361
|
+
interface MockFunctionCall<
|
1362
|
+
F extends Function,
|
1363
|
+
ReturnType = F extends (...args: any) => infer T ? T
|
1364
|
+
: F extends abstract new(...args: any) => infer T ? T
|
1365
|
+
: unknown,
|
1366
|
+
Args = F extends (...args: infer Y) => any ? Y
|
1367
|
+
: F extends abstract new(...args: infer Y) => any ? Y
|
1368
|
+
: unknown[],
|
1369
|
+
> {
|
1533
1370
|
/**
|
1534
|
-
*
|
1371
|
+
* An array of the arguments passed to the mock function.
|
1535
1372
|
*/
|
1536
|
-
|
1373
|
+
arguments: Args;
|
1537
1374
|
/**
|
1538
|
-
*
|
1375
|
+
* If the mocked function threw then this property contains the thrown value.
|
1539
1376
|
*/
|
1540
|
-
|
1377
|
+
error: unknown | undefined;
|
1541
1378
|
/**
|
1542
|
-
* The
|
1379
|
+
* The value returned by the mocked function.
|
1380
|
+
*
|
1381
|
+
* If the mocked function threw, it will be `undefined`.
|
1543
1382
|
*/
|
1544
|
-
|
1383
|
+
result: ReturnType | undefined;
|
1545
1384
|
/**
|
1546
|
-
* An
|
1385
|
+
* An `Error` object whose stack can be used to determine the callsite of the mocked function invocation.
|
1547
1386
|
*/
|
1548
|
-
|
1549
|
-
/**
|
1550
|
-
* The name of the function.
|
1551
|
-
*/
|
1552
|
-
name: string;
|
1553
|
-
/**
|
1554
|
-
* The line number where the function is defined.
|
1555
|
-
*/
|
1556
|
-
line: number;
|
1557
|
-
/**
|
1558
|
-
* The number of times the function was called.
|
1559
|
-
*/
|
1560
|
-
count: number;
|
1561
|
-
}>;
|
1387
|
+
stack: Error;
|
1562
1388
|
/**
|
1563
|
-
*
|
1389
|
+
* If the mocked function is a constructor, this field contains the class being constructed.
|
1390
|
+
* Otherwise this will be `undefined`.
|
1564
1391
|
*/
|
1565
|
-
|
1566
|
-
/**
|
1567
|
-
* The line number where the branch is defined.
|
1568
|
-
*/
|
1569
|
-
line: number;
|
1570
|
-
/**
|
1571
|
-
* The number of times the branch was taken.
|
1572
|
-
*/
|
1573
|
-
count: number;
|
1574
|
-
}>;
|
1392
|
+
target: F extends abstract new(...args: any) => any ? F : undefined;
|
1575
1393
|
/**
|
1576
|
-
*
|
1394
|
+
* The mocked function's `this` value.
|
1577
1395
|
*/
|
1578
|
-
|
1579
|
-
|
1580
|
-
* The line number.
|
1581
|
-
*/
|
1582
|
-
line: number;
|
1583
|
-
/**
|
1584
|
-
* The number of times the line was covered.
|
1585
|
-
*/
|
1586
|
-
count: number;
|
1587
|
-
}>;
|
1588
|
-
}>;
|
1396
|
+
this: unknown;
|
1397
|
+
}
|
1589
1398
|
/**
|
1590
|
-
*
|
1399
|
+
* The `MockFunctionContext` class is used to inspect or manipulate the behavior of
|
1400
|
+
* mocks created via the `MockTracker` APIs.
|
1401
|
+
* @since v19.1.0, v18.13.0
|
1591
1402
|
*/
|
1592
|
-
|
1403
|
+
interface MockFunctionContext<F extends Function> {
|
1593
1404
|
/**
|
1594
|
-
*
|
1405
|
+
* A getter that returns a copy of the internal array used to track calls to the
|
1406
|
+
* mock. Each entry in the array is an object with the following properties.
|
1407
|
+
* @since v19.1.0, v18.13.0
|
1595
1408
|
*/
|
1596
|
-
|
1409
|
+
readonly calls: MockFunctionCall<F>[];
|
1597
1410
|
/**
|
1598
|
-
*
|
1411
|
+
* This function returns the number of times that this mock has been invoked. This
|
1412
|
+
* 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.
|
1413
|
+
* @since v19.1.0, v18.13.0
|
1414
|
+
* @return The number of times that this mock has been invoked.
|
1599
1415
|
*/
|
1600
|
-
|
1416
|
+
callCount(): number;
|
1601
1417
|
/**
|
1602
|
-
*
|
1418
|
+
* This function is used to change the behavior of an existing mock.
|
1419
|
+
*
|
1420
|
+
* The following example creates a mock function using `t.mock.fn()`, calls the
|
1421
|
+
* mock function, and then changes the mock implementation to a different function.
|
1422
|
+
*
|
1423
|
+
* ```js
|
1424
|
+
* test('changes a mock behavior', (t) => {
|
1425
|
+
* let cnt = 0;
|
1426
|
+
*
|
1427
|
+
* function addOne() {
|
1428
|
+
* cnt++;
|
1429
|
+
* return cnt;
|
1430
|
+
* }
|
1431
|
+
*
|
1432
|
+
* function addTwo() {
|
1433
|
+
* cnt += 2;
|
1434
|
+
* return cnt;
|
1435
|
+
* }
|
1436
|
+
*
|
1437
|
+
* const fn = t.mock.fn(addOne);
|
1438
|
+
*
|
1439
|
+
* assert.strictEqual(fn(), 1);
|
1440
|
+
* fn.mock.mockImplementation(addTwo);
|
1441
|
+
* assert.strictEqual(fn(), 3);
|
1442
|
+
* assert.strictEqual(fn(), 5);
|
1443
|
+
* });
|
1444
|
+
* ```
|
1445
|
+
* @since v19.1.0, v18.13.0
|
1446
|
+
* @param implementation The function to be used as the mock's new implementation.
|
1603
1447
|
*/
|
1604
|
-
|
1448
|
+
mockImplementation(implementation: F): void;
|
1605
1449
|
/**
|
1606
|
-
*
|
1450
|
+
* This function is used to change the behavior of an existing mock for a single
|
1451
|
+
* invocation. Once invocation `onCall` has occurred, the mock will revert to
|
1452
|
+
* whatever behavior it would have used had `mockImplementationOnce()` not been
|
1453
|
+
* called.
|
1454
|
+
*
|
1455
|
+
* The following example creates a mock function using `t.mock.fn()`, calls the
|
1456
|
+
* mock function, changes the mock implementation to a different function for the
|
1457
|
+
* next invocation, and then resumes its previous behavior.
|
1458
|
+
*
|
1459
|
+
* ```js
|
1460
|
+
* test('changes a mock behavior once', (t) => {
|
1461
|
+
* let cnt = 0;
|
1462
|
+
*
|
1463
|
+
* function addOne() {
|
1464
|
+
* cnt++;
|
1465
|
+
* return cnt;
|
1466
|
+
* }
|
1467
|
+
*
|
1468
|
+
* function addTwo() {
|
1469
|
+
* cnt += 2;
|
1470
|
+
* return cnt;
|
1471
|
+
* }
|
1472
|
+
*
|
1473
|
+
* const fn = t.mock.fn(addOne);
|
1474
|
+
*
|
1475
|
+
* assert.strictEqual(fn(), 1);
|
1476
|
+
* fn.mock.mockImplementationOnce(addTwo);
|
1477
|
+
* assert.strictEqual(fn(), 3);
|
1478
|
+
* assert.strictEqual(fn(), 4);
|
1479
|
+
* });
|
1480
|
+
* ```
|
1481
|
+
* @since v19.1.0, v18.13.0
|
1482
|
+
* @param implementation The function to be used as the mock's implementation for the invocation number specified by `onCall`.
|
1483
|
+
* @param onCall The invocation number that will use `implementation`. If the specified invocation has already occurred then an exception is thrown.
|
1607
1484
|
*/
|
1608
|
-
|
1485
|
+
mockImplementationOnce(implementation: F, onCall?: number): void;
|
1609
1486
|
/**
|
1610
|
-
*
|
1487
|
+
* Resets the call history of the mock function.
|
1488
|
+
* @since v19.3.0, v18.13.0
|
1611
1489
|
*/
|
1612
|
-
|
1490
|
+
resetCalls(): void;
|
1613
1491
|
/**
|
1614
|
-
*
|
1492
|
+
* Resets the implementation of the mock function to its original behavior. The
|
1493
|
+
* mock can still be used after calling this function.
|
1494
|
+
* @since v19.1.0, v18.13.0
|
1615
1495
|
*/
|
1616
|
-
|
1496
|
+
restore(): void;
|
1497
|
+
}
|
1498
|
+
/**
|
1499
|
+
* @since v20.18.0
|
1500
|
+
* @experimental
|
1501
|
+
*/
|
1502
|
+
interface MockModuleContext {
|
1617
1503
|
/**
|
1618
|
-
*
|
1504
|
+
* Resets the implementation of the mock module.
|
1505
|
+
* @since v20.18.0
|
1619
1506
|
*/
|
1620
|
-
|
1507
|
+
restore(): void;
|
1508
|
+
}
|
1509
|
+
interface MockTimersOptions {
|
1510
|
+
apis: ReadonlyArray<"setInterval" | "setTimeout" | "setImmediate" | "Date">;
|
1511
|
+
now?: number | Date | undefined;
|
1512
|
+
}
|
1513
|
+
/**
|
1514
|
+
* Mocking timers is a technique commonly used in software testing to simulate and
|
1515
|
+
* control the behavior of timers, such as `setInterval` and `setTimeout`,
|
1516
|
+
* without actually waiting for the specified time intervals.
|
1517
|
+
*
|
1518
|
+
* The MockTimers API also allows for mocking of the `Date` constructor and
|
1519
|
+
* `setImmediate`/`clearImmediate` functions.
|
1520
|
+
*
|
1521
|
+
* The `MockTracker` provides a top-level `timers` export
|
1522
|
+
* which is a `MockTimers` instance.
|
1523
|
+
* @since v20.4.0
|
1524
|
+
* @experimental
|
1525
|
+
*/
|
1526
|
+
interface MockTimers {
|
1621
1527
|
/**
|
1622
|
-
*
|
1528
|
+
* Enables timer mocking for the specified timers.
|
1529
|
+
*
|
1530
|
+
* **Note:** When you enable mocking for a specific timer, its associated
|
1531
|
+
* clear function will also be implicitly mocked.
|
1532
|
+
*
|
1533
|
+
* **Note:** Mocking `Date` will affect the behavior of the mocked timers
|
1534
|
+
* as they use the same internal clock.
|
1535
|
+
*
|
1536
|
+
* Example usage without setting initial time:
|
1537
|
+
*
|
1538
|
+
* ```js
|
1539
|
+
* import { mock } from 'node:test';
|
1540
|
+
* mock.timers.enable({ apis: ['setInterval', 'Date'], now: 1234 });
|
1541
|
+
* ```
|
1542
|
+
*
|
1543
|
+
* The above example enables mocking for the `Date` constructor, `setInterval` timer and
|
1544
|
+
* implicitly mocks the `clearInterval` function. Only the `Date` constructor from `globalThis`,
|
1545
|
+
* `setInterval` and `clearInterval` functions from `node:timers`, `node:timers/promises`, and `globalThis` will be mocked.
|
1546
|
+
*
|
1547
|
+
* Example usage with initial time set
|
1548
|
+
*
|
1549
|
+
* ```js
|
1550
|
+
* import { mock } from 'node:test';
|
1551
|
+
* mock.timers.enable({ apis: ['Date'], now: 1000 });
|
1552
|
+
* ```
|
1553
|
+
*
|
1554
|
+
* Example usage with initial Date object as time set
|
1555
|
+
*
|
1556
|
+
* ```js
|
1557
|
+
* import { mock } from 'node:test';
|
1558
|
+
* mock.timers.enable({ apis: ['Date'], now: new Date() });
|
1559
|
+
* ```
|
1560
|
+
*
|
1561
|
+
* Alternatively, if you call `mock.timers.enable()` without any parameters:
|
1562
|
+
*
|
1563
|
+
* All timers (`'setInterval'`, `'clearInterval'`, `'Date'`, `'setImmediate'`, `'clearImmediate'`, `'setTimeout'`, and `'clearTimeout'`)
|
1564
|
+
* will be mocked.
|
1565
|
+
*
|
1566
|
+
* The `setInterval`, `clearInterval`, `setTimeout`, and `clearTimeout` functions from `node:timers`, `node:timers/promises`,
|
1567
|
+
* and `globalThis` will be mocked.
|
1568
|
+
* The `Date` constructor from `globalThis` will be mocked.
|
1569
|
+
*
|
1570
|
+
* If there is no initial epoch set, the initial date will be based on 0 in the Unix epoch. This is `January 1st, 1970, 00:00:00 UTC`. You can
|
1571
|
+
* set an initial date by passing a now property to the `.enable()` method. This value will be used as the initial date for the mocked Date
|
1572
|
+
* object. It can either be a positive integer, or another Date object.
|
1573
|
+
* @since v20.4.0
|
1623
1574
|
*/
|
1624
|
-
|
1575
|
+
enable(options?: MockTimersOptions): void;
|
1625
1576
|
/**
|
1626
|
-
*
|
1577
|
+
* You can use the `.setTime()` method to manually move the mocked date to another time. This method only accepts a positive integer.
|
1578
|
+
* Note: This method will execute any mocked timers that are in the past from the new time.
|
1579
|
+
* In the below example we are setting a new time for the mocked date.
|
1580
|
+
* ```js
|
1581
|
+
* import assert from 'node:assert';
|
1582
|
+
* import { test } from 'node:test';
|
1583
|
+
* test('sets the time of a date object', (context) => {
|
1584
|
+
* // Optionally choose what to mock
|
1585
|
+
* context.mock.timers.enable({ apis: ['Date'], now: 100 });
|
1586
|
+
* assert.strictEqual(Date.now(), 100);
|
1587
|
+
* // Advance in time will also advance the date
|
1588
|
+
* context.mock.timers.setTime(1000);
|
1589
|
+
* context.mock.timers.tick(200);
|
1590
|
+
* assert.strictEqual(Date.now(), 1200);
|
1591
|
+
* });
|
1592
|
+
* ```
|
1627
1593
|
*/
|
1628
|
-
|
1629
|
-
|
1630
|
-
|
1631
|
-
|
1632
|
-
|
1633
|
-
|
1634
|
-
|
1635
|
-
|
1636
|
-
|
1637
|
-
|
1638
|
-
|
1639
|
-
|
1640
|
-
|
1641
|
-
|
1642
|
-
|
1643
|
-
|
1644
|
-
|
1645
|
-
|
1646
|
-
|
1647
|
-
|
1648
|
-
|
1649
|
-
|
1650
|
-
|
1651
|
-
|
1652
|
-
|
1653
|
-
|
1654
|
-
|
1655
|
-
|
1656
|
-
|
1657
|
-
|
1658
|
-
|
1659
|
-
|
1660
|
-
|
1661
|
-
|
1662
|
-
|
1663
|
-
|
1664
|
-
|
1665
|
-
|
1666
|
-
|
1667
|
-
|
1668
|
-
|
1669
|
-
|
1670
|
-
|
1671
|
-
|
1672
|
-
|
1673
|
-
|
1674
|
-
|
1675
|
-
|
1676
|
-
|
1677
|
-
|
1678
|
-
|
1679
|
-
|
1680
|
-
|
1681
|
-
|
1682
|
-
|
1683
|
-
|
1684
|
-
|
1685
|
-
|
1686
|
-
|
1687
|
-
|
1688
|
-
|
1689
|
-
|
1690
|
-
|
1691
|
-
|
1692
|
-
|
1693
|
-
|
1694
|
-
|
1695
|
-
|
1696
|
-
|
1697
|
-
|
1698
|
-
|
1699
|
-
|
1700
|
-
|
1701
|
-
|
1702
|
-
|
1703
|
-
|
1704
|
-
|
1705
|
-
|
1706
|
-
|
1707
|
-
|
1708
|
-
|
1709
|
-
|
1710
|
-
|
1711
|
-
|
1712
|
-
|
1713
|
-
|
1714
|
-
|
1715
|
-
|
1716
|
-
|
1717
|
-
|
1718
|
-
|
1719
|
-
|
1720
|
-
|
1721
|
-
|
1722
|
-
|
1723
|
-
|
1724
|
-
|
1725
|
-
|
1726
|
-
|
1727
|
-
|
1728
|
-
|
1729
|
-
|
1730
|
-
|
1731
|
-
|
1732
|
-
|
1733
|
-
|
1734
|
-
|
1735
|
-
|
1736
|
-
|
1737
|
-
|
1738
|
-
|
1739
|
-
|
1740
|
-
|
1741
|
-
|
1742
|
-
|
1743
|
-
|
1744
|
-
|
1745
|
-
|
1746
|
-
|
1747
|
-
|
1748
|
-
|
1749
|
-
|
1750
|
-
|
1751
|
-
|
1752
|
-
|
1753
|
-
|
1754
|
-
|
1755
|
-
|
1756
|
-
|
1757
|
-
|
1758
|
-
|
1759
|
-
|
1760
|
-
|
1761
|
-
|
1762
|
-
|
1763
|
-
|
1764
|
-
|
1765
|
-
|
1766
|
-
|
1767
|
-
|
1768
|
-
/**
|
1769
|
-
* The ordinal number of the test.
|
1770
|
-
*/
|
1771
|
-
testNumber: number;
|
1772
|
-
/**
|
1773
|
-
* Present if `context.todo` is called.
|
1774
|
-
*/
|
1775
|
-
todo?: string | boolean;
|
1776
|
-
/**
|
1777
|
-
* Present if `context.skip` is called.
|
1778
|
-
*/
|
1779
|
-
skip?: string | boolean;
|
1780
|
-
}
|
1781
|
-
interface TestPlan extends TestLocationInfo {
|
1782
|
-
/**
|
1783
|
-
* The nesting level of the test.
|
1784
|
-
*/
|
1785
|
-
nesting: number;
|
1786
|
-
/**
|
1787
|
-
* The number of subtests that have ran.
|
1788
|
-
*/
|
1789
|
-
count: number;
|
1790
|
-
}
|
1791
|
-
interface TestStart extends TestLocationInfo {
|
1792
|
-
/**
|
1793
|
-
* The test name.
|
1794
|
-
*/
|
1795
|
-
name: string;
|
1796
|
-
/**
|
1797
|
-
* The nesting level of the test.
|
1798
|
-
*/
|
1799
|
-
nesting: number;
|
1800
|
-
}
|
1801
|
-
interface TestStderr {
|
1802
|
-
/**
|
1803
|
-
* The path of the test file.
|
1804
|
-
*/
|
1805
|
-
file: string;
|
1806
|
-
/**
|
1807
|
-
* The message written to `stderr`.
|
1808
|
-
*/
|
1809
|
-
message: string;
|
1810
|
-
}
|
1811
|
-
interface TestStdout {
|
1812
|
-
/**
|
1813
|
-
* The path of the test file.
|
1814
|
-
*/
|
1815
|
-
file: string;
|
1816
|
-
/**
|
1817
|
-
* The message written to `stdout`.
|
1818
|
-
*/
|
1819
|
-
message: string;
|
1594
|
+
setTime(time: number): void;
|
1595
|
+
/**
|
1596
|
+
* This function restores the default behavior of all mocks that were previously
|
1597
|
+
* created by this `MockTimers` instance and disassociates the mocks
|
1598
|
+
* from the `MockTracker` instance.
|
1599
|
+
*
|
1600
|
+
* **Note:** After each test completes, this function is called on
|
1601
|
+
* the test context's `MockTracker`.
|
1602
|
+
*
|
1603
|
+
* ```js
|
1604
|
+
* import { mock } from 'node:test';
|
1605
|
+
* mock.timers.reset();
|
1606
|
+
* ```
|
1607
|
+
* @since v20.4.0
|
1608
|
+
*/
|
1609
|
+
reset(): void;
|
1610
|
+
/**
|
1611
|
+
* Advances time for all mocked timers.
|
1612
|
+
*
|
1613
|
+
* **Note:** This diverges from how `setTimeout` in Node.js behaves and accepts
|
1614
|
+
* only positive numbers. In Node.js, `setTimeout` with negative numbers is
|
1615
|
+
* only supported for web compatibility reasons.
|
1616
|
+
*
|
1617
|
+
* The following example mocks a `setTimeout` function and
|
1618
|
+
* by using `.tick` advances in
|
1619
|
+
* time triggering all pending timers.
|
1620
|
+
*
|
1621
|
+
* ```js
|
1622
|
+
* import assert from 'node:assert';
|
1623
|
+
* import { test } from 'node:test';
|
1624
|
+
*
|
1625
|
+
* test('mocks setTimeout to be executed synchronously without having to actually wait for it', (context) => {
|
1626
|
+
* const fn = context.mock.fn();
|
1627
|
+
*
|
1628
|
+
* context.mock.timers.enable({ apis: ['setTimeout'] });
|
1629
|
+
*
|
1630
|
+
* setTimeout(fn, 9999);
|
1631
|
+
*
|
1632
|
+
* assert.strictEqual(fn.mock.callCount(), 0);
|
1633
|
+
*
|
1634
|
+
* // Advance in time
|
1635
|
+
* context.mock.timers.tick(9999);
|
1636
|
+
*
|
1637
|
+
* assert.strictEqual(fn.mock.callCount(), 1);
|
1638
|
+
* });
|
1639
|
+
* ```
|
1640
|
+
*
|
1641
|
+
* Alternativelly, the `.tick` function can be called many times
|
1642
|
+
*
|
1643
|
+
* ```js
|
1644
|
+
* import assert from 'node:assert';
|
1645
|
+
* import { test } from 'node:test';
|
1646
|
+
*
|
1647
|
+
* test('mocks setTimeout to be executed synchronously without having to actually wait for it', (context) => {
|
1648
|
+
* const fn = context.mock.fn();
|
1649
|
+
* context.mock.timers.enable({ apis: ['setTimeout'] });
|
1650
|
+
* const nineSecs = 9000;
|
1651
|
+
* setTimeout(fn, nineSecs);
|
1652
|
+
*
|
1653
|
+
* const twoSeconds = 3000;
|
1654
|
+
* context.mock.timers.tick(twoSeconds);
|
1655
|
+
* context.mock.timers.tick(twoSeconds);
|
1656
|
+
* context.mock.timers.tick(twoSeconds);
|
1657
|
+
*
|
1658
|
+
* assert.strictEqual(fn.mock.callCount(), 1);
|
1659
|
+
* });
|
1660
|
+
* ```
|
1661
|
+
*
|
1662
|
+
* Advancing time using `.tick` will also advance the time for any `Date` object
|
1663
|
+
* created after the mock was enabled (if `Date` was also set to be mocked).
|
1664
|
+
*
|
1665
|
+
* ```js
|
1666
|
+
* import assert from 'node:assert';
|
1667
|
+
* import { test } from 'node:test';
|
1668
|
+
*
|
1669
|
+
* test('mocks setTimeout to be executed synchronously without having to actually wait for it', (context) => {
|
1670
|
+
* const fn = context.mock.fn();
|
1671
|
+
*
|
1672
|
+
* context.mock.timers.enable({ apis: ['setTimeout', 'Date'] });
|
1673
|
+
* setTimeout(fn, 9999);
|
1674
|
+
*
|
1675
|
+
* assert.strictEqual(fn.mock.callCount(), 0);
|
1676
|
+
* assert.strictEqual(Date.now(), 0);
|
1677
|
+
*
|
1678
|
+
* // Advance in time
|
1679
|
+
* context.mock.timers.tick(9999);
|
1680
|
+
* assert.strictEqual(fn.mock.callCount(), 1);
|
1681
|
+
* assert.strictEqual(Date.now(), 9999);
|
1682
|
+
* });
|
1683
|
+
* ```
|
1684
|
+
* @since v20.4.0
|
1685
|
+
*/
|
1686
|
+
tick(milliseconds: number): void;
|
1687
|
+
/**
|
1688
|
+
* Triggers all pending mocked timers immediately. If the `Date` object is also
|
1689
|
+
* mocked, it will also advance the `Date` object to the furthest timer's time.
|
1690
|
+
*
|
1691
|
+
* The example below triggers all pending timers immediately,
|
1692
|
+
* causing them to execute without any delay.
|
1693
|
+
*
|
1694
|
+
* ```js
|
1695
|
+
* import assert from 'node:assert';
|
1696
|
+
* import { test } from 'node:test';
|
1697
|
+
*
|
1698
|
+
* test('runAll functions following the given order', (context) => {
|
1699
|
+
* context.mock.timers.enable({ apis: ['setTimeout', 'Date'] });
|
1700
|
+
* const results = [];
|
1701
|
+
* setTimeout(() => results.push(1), 9999);
|
1702
|
+
*
|
1703
|
+
* // Notice that if both timers have the same timeout,
|
1704
|
+
* // the order of execution is guaranteed
|
1705
|
+
* setTimeout(() => results.push(3), 8888);
|
1706
|
+
* setTimeout(() => results.push(2), 8888);
|
1707
|
+
*
|
1708
|
+
* assert.deepStrictEqual(results, []);
|
1709
|
+
*
|
1710
|
+
* context.mock.timers.runAll();
|
1711
|
+
* assert.deepStrictEqual(results, [3, 2, 1]);
|
1712
|
+
* // The Date object is also advanced to the furthest timer's time
|
1713
|
+
* assert.strictEqual(Date.now(), 9999);
|
1714
|
+
* });
|
1715
|
+
* ```
|
1716
|
+
*
|
1717
|
+
* **Note:** The `runAll()` function is specifically designed for
|
1718
|
+
* triggering timers in the context of timer mocking.
|
1719
|
+
* It does not have any effect on real-time system
|
1720
|
+
* clocks or actual timers outside of the mocking environment.
|
1721
|
+
* @since v20.4.0
|
1722
|
+
*/
|
1723
|
+
runAll(): void;
|
1724
|
+
/**
|
1725
|
+
* Calls {@link MockTimers.reset()}.
|
1726
|
+
*/
|
1727
|
+
[Symbol.dispose](): void;
|
1728
|
+
}
|
1729
|
+
}
|
1730
|
+
type FunctionPropertyNames<T> = {
|
1731
|
+
[K in keyof T]: T[K] extends Function ? K : never;
|
1732
|
+
}[keyof T];
|
1733
|
+
export = test;
|
1820
1734
|
}
|
1821
1735
|
|
1822
1736
|
/**
|
@@ -1838,19 +1752,20 @@ interface TestStdout {
|
|
1838
1752
|
*/
|
1839
1753
|
declare module "node:test/reporters" {
|
1840
1754
|
import { Transform, TransformOptions } from "node:stream";
|
1755
|
+
import { EventData } from "node:test";
|
1841
1756
|
|
1842
1757
|
type TestEvent =
|
1843
|
-
| { type: "test:coverage"; data: TestCoverage }
|
1844
|
-
| { type: "test:complete"; data: TestComplete }
|
1845
|
-
| { type: "test:dequeue"; data: TestDequeue }
|
1846
|
-
| { type: "test:diagnostic"; data:
|
1847
|
-
| { type: "test:enqueue"; data: TestEnqueue }
|
1848
|
-
| { type: "test:fail"; data: TestFail }
|
1849
|
-
| { type: "test:pass"; data: TestPass }
|
1850
|
-
| { type: "test:plan"; data: TestPlan }
|
1851
|
-
| { type: "test:start"; data: TestStart }
|
1852
|
-
| { type: "test:stderr"; data: TestStderr }
|
1853
|
-
| { type: "test:stdout"; data: TestStdout }
|
1758
|
+
| { type: "test:coverage"; data: EventData.TestCoverage }
|
1759
|
+
| { type: "test:complete"; data: EventData.TestComplete }
|
1760
|
+
| { type: "test:dequeue"; data: EventData.TestDequeue }
|
1761
|
+
| { type: "test:diagnostic"; data: EventData.TestDiagnostic }
|
1762
|
+
| { type: "test:enqueue"; data: EventData.TestEnqueue }
|
1763
|
+
| { type: "test:fail"; data: EventData.TestFail }
|
1764
|
+
| { type: "test:pass"; data: EventData.TestPass }
|
1765
|
+
| { type: "test:plan"; data: EventData.TestPlan }
|
1766
|
+
| { type: "test:start"; data: EventData.TestStart }
|
1767
|
+
| { type: "test:stderr"; data: EventData.TestStderr }
|
1768
|
+
| { type: "test:stdout"; data: EventData.TestStdout }
|
1854
1769
|
| { type: "test:watch:drained"; data: undefined };
|
1855
1770
|
type TestEventGenerator = AsyncGenerator<TestEvent, void>;
|
1856
1771
|
|