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