-
Notifications
You must be signed in to change notification settings - Fork 30.5k
Expand file tree
/
Copy pathtest.d.ts
More file actions
2240 lines (2240 loc) · 101 KB
/
test.d.ts
File metadata and controls
2240 lines (2240 loc) · 101 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* The `node:test` module facilitates the creation of JavaScript tests.
* To access it:
*
* ```js
* import test from 'node:test';
* ```
*
* This module is only available under the `node:` scheme. The following will not
* work:
*
* ```js
* import test from 'node:test';
* ```
*
* Tests created via the `test` module consist of a single function that is
* processed in one of three ways:
*
* 1. A synchronous function that is considered failing if it throws an exception,
* and is considered passing otherwise.
* 2. A function that returns a `Promise` that is considered failing if the `Promise` rejects, and is considered passing if the `Promise` fulfills.
* 3. A function that receives a callback function. If the callback receives any
* truthy value as its first argument, the test is considered failing. If a
* falsy value is passed as the first argument to the callback, the test is
* considered passing. If the test function receives a callback function and
* also returns a `Promise`, the test will fail.
*
* The following example illustrates how tests are written using the `test` module.
*
* ```js
* test('synchronous passing test', (t) => {
* // This test passes because it does not throw an exception.
* assert.strictEqual(1, 1);
* });
*
* test('synchronous failing test', (t) => {
* // This test fails because it throws an exception.
* assert.strictEqual(1, 2);
* });
*
* test('asynchronous passing test', async (t) => {
* // This test passes because the Promise returned by the async
* // function is settled and not rejected.
* assert.strictEqual(1, 1);
* });
*
* test('asynchronous failing test', async (t) => {
* // This test fails because the Promise returned by the async
* // function is rejected.
* assert.strictEqual(1, 2);
* });
*
* test('failing test using Promises', (t) => {
* // Promises can be used directly as well.
* return new Promise((resolve, reject) => {
* setImmediate(() => {
* reject(new Error('this will cause the test to fail'));
* });
* });
* });
*
* test('callback passing test', (t, done) => {
* // done() is the callback function. When the setImmediate() runs, it invokes
* // done() with no arguments.
* setImmediate(done);
* });
*
* test('callback failing test', (t, done) => {
* // When the setImmediate() runs, done() is invoked with an Error object and
* // the test fails.
* setImmediate(() => {
* done(new Error('callback failure'));
* });
* });
* ```
*
* If any tests fail, the process exit code is set to `1`.
* @since v18.0.0, v16.17.0
* @see [source](https://github.com/nodejs/node/blob/v25.x/lib/test.js)
*/
declare module "node:test" {
import { AssertMethodNames } from "node:assert";
import { Readable, ReadableEventMap } from "node:stream";
import { TestEvent } from "node:test/reporters";
import { URL } from "node:url";
import TestFn = test.TestFn;
import TestOptions = test.TestOptions;
/**
* The `test()` function is the value imported from the `test` module. Each
* invocation of this function results in reporting the test to the `TestsStream`.
*
* The `TestContext` object passed to the `fn` argument can be used to perform
* actions related to the current test. Examples include skipping the test, adding
* additional diagnostic information, or creating subtests.
*
* `test()` returns a `Promise` that fulfills once the test completes.
* if `test()` is called within a suite, it fulfills immediately.
* The return value can usually be discarded for top level tests.
* However, the return value from subtests should be used to prevent the parent
* test from finishing first and cancelling the subtest
* as shown in the following example.
*
* ```js
* test('top level test', async (t) => {
* // The setTimeout() in the following subtest would cause it to outlive its
* // parent test if 'await' is removed on the next line. Once the parent test
* // completes, it will cancel any outstanding subtests.
* await t.test('longer running subtest', async (t) => {
* return new Promise((resolve, reject) => {
* setTimeout(resolve, 1000);
* });
* });
* });
* ```
*
* 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
* canceling tests because a running test might block the application thread and
* thus prevent the scheduled cancellation.
* @since v18.0.0, v16.17.0
* @param name The name of the test, which is displayed when reporting test results.
* Defaults to the `name` property of `fn`, or `'<anonymous>'` if `fn` does not have a name.
* @param options Configuration options for the test.
* @param fn 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 second argument.
* @return Fulfilled with `undefined` once the test completes, or immediately if the test runs within a suite.
*/
function test(name?: string, fn?: TestFn): Promise<void>;
function test(name?: string, options?: TestOptions, fn?: TestFn): Promise<void>;
function test(options?: TestOptions, fn?: TestFn): Promise<void>;
function test(fn?: TestFn): Promise<void>;
namespace test {
export { test };
export { suite as describe, test as it };
}
namespace test {
/**
* **Note:** `shard` is used to horizontally parallelize test running across
* machines or processes, ideal for large-scale executions across varied
* environments. It's incompatible with `watch` mode, tailored for rapid
* code iteration by automatically rerunning tests on file changes.
*
* ```js
* import { tap } from 'node:test/reporters';
* import { run } from 'node:test';
* import process from 'node:process';
* import path from 'node:path';
*
* run({ files: [path.resolve('./tests/test.js')] })
* .compose(tap)
* .pipe(process.stdout);
* ```
* @since v18.9.0, v16.19.0
* @param options Configuration options for running tests.
*/
function run(options?: RunOptions): TestsStream;
/**
* The `suite()` function is imported from the `node:test` module.
* @param name The name of the suite, which is displayed when reporting test results.
* Defaults to the `name` property of `fn`, or `'<anonymous>'` if `fn` does not have a name.
* @param options Configuration options for the suite. This supports the same options as {@link test}.
* @param fn The suite function declaring nested tests and suites. The first argument to this function is a {@link SuiteContext} object.
* @return Immediately fulfilled with `undefined`.
* @since v20.13.0
*/
function suite(name?: string, options?: TestOptions, fn?: SuiteFn): Promise<void>;
function suite(name?: string, fn?: SuiteFn): Promise<void>;
function suite(options?: TestOptions, fn?: SuiteFn): Promise<void>;
function suite(fn?: SuiteFn): Promise<void>;
namespace suite {
/**
* Shorthand for skipping a suite. This is the same as calling {@link suite} with `options.skip` set to `true`.
* @since v20.13.0
*/
function skip(name?: string, options?: TestOptions, fn?: SuiteFn): Promise<void>;
function skip(name?: string, fn?: SuiteFn): Promise<void>;
function skip(options?: TestOptions, fn?: SuiteFn): Promise<void>;
function skip(fn?: SuiteFn): Promise<void>;
/**
* Shorthand for marking a suite as `TODO`. This is the same as calling {@link suite} with `options.todo` set to `true`.
* @since v20.13.0
*/
function todo(name?: string, options?: TestOptions, fn?: SuiteFn): Promise<void>;
function todo(name?: string, fn?: SuiteFn): Promise<void>;
function todo(options?: TestOptions, fn?: SuiteFn): Promise<void>;
function todo(fn?: SuiteFn): Promise<void>;
/**
* Shorthand for marking a suite as `only`. This is the same as calling {@link suite} with `options.only` set to `true`.
* @since v20.13.0
*/
function only(name?: string, options?: TestOptions, fn?: SuiteFn): Promise<void>;
function only(name?: string, fn?: SuiteFn): Promise<void>;
function only(options?: TestOptions, fn?: SuiteFn): Promise<void>;
function only(fn?: SuiteFn): Promise<void>;
}
/**
* Shorthand for skipping a test. This is the same as calling {@link test} with `options.skip` set to `true`.
* @since v20.2.0
*/
function skip(name?: string, options?: TestOptions, fn?: TestFn): Promise<void>;
function skip(name?: string, fn?: TestFn): Promise<void>;
function skip(options?: TestOptions, fn?: TestFn): Promise<void>;
function skip(fn?: TestFn): Promise<void>;
/**
* Shorthand for marking a test as `TODO`. This is the same as calling {@link test} with `options.todo` set to `true`.
* @since v20.2.0
*/
function todo(name?: string, options?: TestOptions, fn?: TestFn): Promise<void>;
function todo(name?: string, fn?: TestFn): Promise<void>;
function todo(options?: TestOptions, fn?: TestFn): Promise<void>;
function todo(fn?: TestFn): Promise<void>;
/**
* Shorthand for marking a test as `only`. This is the same as calling {@link test} with `options.only` set to `true`.
* @since v20.2.0
*/
function only(name?: string, options?: TestOptions, fn?: TestFn): Promise<void>;
function only(name?: string, fn?: TestFn): Promise<void>;
function only(options?: TestOptions, fn?: TestFn): Promise<void>;
function only(fn?: TestFn): Promise<void>;
/**
* The type of a function passed to {@link test}. The first argument to this function is a {@link TestContext} object.
* If the test uses callbacks, the callback function is passed as the second argument.
*/
type TestFn = (t: TestContext, done: (result?: any) => void) => void | Promise<void>;
/**
* The type of a suite test function. The argument to this function is a {@link SuiteContext} object.
*/
type SuiteFn = (s: SuiteContext) => void | Promise<void>;
interface TestShard {
/**
* A positive integer between 1 and `total` that specifies the index of the shard to run.
*/
index: number;
/**
* A positive integer that specifies the total number of shards to split the test files to.
*/
total: number;
}
interface RunOptions {
/**
* If a number is provided, then that many test processes would run in parallel, where each process corresponds to one test file.
* If `true`, it would run `os.availableParallelism() - 1` test files in parallel. If `false`, it would only run one test file at a time.
* @default false
*/
concurrency?: number | boolean | undefined;
/**
* Specifies the current working directory to be used by the test runner.
* Serves as the base path for resolving files according to the
* [test runner execution model](https://nodejs.org/docs/latest-v25.x/api/test.html#test-runner-execution-model).
* @since v23.0.0
* @default process.cwd()
*/
cwd?: string | undefined;
/**
* An array containing the list of files to run. If omitted, files are run according to the
* [test runner execution model](https://nodejs.org/docs/latest-v25.x/api/test.html#test-runner-execution-model).
*/
files?: readonly string[] | undefined;
/**
* Configures the test runner to exit the process once all known
* tests have finished executing even if the event loop would
* otherwise remain active.
* @default false
*/
forceExit?: boolean | undefined;
/**
* An array containing the list of glob patterns to match test files.
* This option cannot be used together with `files`. If omitted, files are run according to the
* [test runner execution model](https://nodejs.org/docs/latest-v25.x/api/test.html#test-runner-execution-model).
* @since v22.6.0
*/
globPatterns?: readonly string[] | undefined;
/**
* Sets inspector port of test child process.
* This can be a number, or a function that takes no arguments and returns a
* number. If a nullish value is provided, each process gets its own port,
* incremented from the primary's `process.debugPort`. This option is ignored
* if the `isolation` option is set to `'none'` as no child processes are
* spawned.
* @default undefined
*/
inspectPort?: number | (() => number) | undefined;
/**
* Configures the type of test isolation. If set to
* `'process'`, each test file is run in a separate child process. If set to
* `'none'`, all test files run in the current process.
* @default 'process'
* @since v22.8.0
*/
isolation?: "process" | "none" | undefined;
/**
* If truthy, the test context will only run tests that have the `only` option set
*/
only?: boolean | undefined;
/**
* A function that accepts the `TestsStream` instance and can be used to setup listeners before any tests are run.
* @default undefined
*/
setup?: ((reporter: TestsStream) => void | Promise<void>) | undefined;
/**
* An array of CLI flags to pass to the `node` executable when
* spawning the subprocesses. This option has no effect when `isolation` is `'none`'.
* @since v22.10.0
* @default []
*/
execArgv?: readonly string[] | undefined;
/**
* An array of CLI flags to pass to each test file when spawning the
* subprocesses. This option has no effect when `isolation` is `'none'`.
* @since v22.10.0
* @default []
*/
argv?: readonly string[] | undefined;
/**
* Allows aborting an in-progress test execution.
*/
signal?: AbortSignal | undefined;
/**
* If provided, only run tests whose name matches the provided pattern.
* Strings are interpreted as JavaScript regular expressions.
* @default undefined
*/
testNamePatterns?: string | RegExp | ReadonlyArray<string | RegExp> | undefined;
/**
* A String, RegExp or a RegExp Array, that can be used to exclude running tests whose
* name matches the provided pattern. Test name patterns are interpreted as JavaScript
* regular expressions. For each test that is executed, any corresponding test hooks,
* such as `beforeEach()`, are also run.
* @default undefined
* @since v22.1.0
*/
testSkipPatterns?: string | RegExp | ReadonlyArray<string | RegExp> | undefined;
/**
* The number of milliseconds after which the test execution will fail.
* If unspecified, subtests inherit this value from their parent.
* @default Infinity
*/
timeout?: number | undefined;
/**
* Whether to run in watch mode or not.
* @default false
*/
watch?: boolean | undefined;
/**
* Running tests in a specific shard.
* @default undefined
*/
shard?: TestShard | undefined;
/**
* A file path where the test runner will
* store the state of the tests to allow rerunning only the failed tests on a next run.
* @since v24.7.0
* @default undefined
*/
rerunFailuresFilePath?: string | undefined;
/**
* enable [code coverage](https://nodejs.org/docs/latest-v25.x/api/test.html#collecting-code-coverage) collection.
* @since v22.10.0
* @default false
*/
coverage?: boolean | undefined;
/**
* Excludes specific files from code coverage
* using a glob pattern, which can match both absolute and relative file paths.
* This property is only applicable when `coverage` was set to `true`.
* If both `coverageExcludeGlobs` and `coverageIncludeGlobs` are provided,
* files must meet **both** criteria to be included in the coverage report.
* @since v22.10.0
* @default undefined
*/
coverageExcludeGlobs?: string | readonly string[] | undefined;
/**
* Includes specific files in code coverage
* using a glob pattern, which can match both absolute and relative file paths.
* This property is only applicable when `coverage` was set to `true`.
* If both `coverageExcludeGlobs` and `coverageIncludeGlobs` are provided,
* files must meet **both** criteria to be included in the coverage report.
* @since v22.10.0
* @default undefined
*/
coverageIncludeGlobs?: string | readonly string[] | undefined;
/**
* Require a minimum percent of covered lines. If code
* coverage does not reach the threshold specified, the process will exit with code `1`.
* @since v22.10.0
* @default 0
*/
lineCoverage?: number | undefined;
/**
* Require a minimum percent of covered branches. If code
* coverage does not reach the threshold specified, the process will exit with code `1`.
* @since v22.10.0
* @default 0
*/
branchCoverage?: number | undefined;
/**
* Require a minimum percent of covered functions. If code
* coverage does not reach the threshold specified, the process will exit with code `1`.
* @since v22.10.0
* @default 0
*/
functionCoverage?: number | undefined;
}
interface TestsStreamEventMap extends ReadableEventMap {
"data": [data: TestEvent];
"test:coverage": [data: EventData.TestCoverage];
"test:complete": [data: EventData.TestComplete];
"test:dequeue": [data: EventData.TestDequeue];
"test:diagnostic": [data: EventData.TestDiagnostic];
"test:enqueue": [data: EventData.TestEnqueue];
"test:fail": [data: EventData.TestFail];
"test:pass": [data: EventData.TestPass];
"test:plan": [data: EventData.TestPlan];
"test:start": [data: EventData.TestStart];
"test:stderr": [data: EventData.TestStderr];
"test:stdout": [data: EventData.TestStdout];
"test:summary": [data: EventData.TestSummary];
"test:watch:drained": [];
"test:watch:restarted": [];
}
/**
* A successful call to `run()` will return a new `TestsStream` object, streaming a series of events representing the execution of the tests.
*
* 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.
* @since v18.9.0, v16.19.0
*/
interface TestsStream extends Readable {
// #region InternalEventEmitter
addListener<E extends keyof TestsStreamEventMap>(
eventName: E,
listener: (...args: TestsStreamEventMap[E]) => void,
): this;
addListener(eventName: string | symbol, listener: (...args: any[]) => void): this;
emit<E extends keyof TestsStreamEventMap>(eventName: E, ...args: TestsStreamEventMap[E]): boolean;
emit(eventName: string | symbol, ...args: any[]): boolean;
listenerCount<E extends keyof TestsStreamEventMap>(
eventName: E,
listener?: (...args: TestsStreamEventMap[E]) => void,
): number;
listenerCount(eventName: string | symbol, listener?: (...args: any[]) => void): number;
listeners<E extends keyof TestsStreamEventMap>(eventName: E): ((...args: TestsStreamEventMap[E]) => void)[];
listeners(eventName: string | symbol): ((...args: any[]) => void)[];
off<E extends keyof TestsStreamEventMap>(
eventName: E,
listener: (...args: TestsStreamEventMap[E]) => void,
): this;
off(eventName: string | symbol, listener: (...args: any[]) => void): this;
on<E extends keyof TestsStreamEventMap>(
eventName: E,
listener: (...args: TestsStreamEventMap[E]) => void,
): this;
on(eventName: string | symbol, listener: (...args: any[]) => void): this;
once<E extends keyof TestsStreamEventMap>(
eventName: E,
listener: (...args: TestsStreamEventMap[E]) => void,
): this;
once(eventName: string | symbol, listener: (...args: any[]) => void): this;
prependListener<E extends keyof TestsStreamEventMap>(
eventName: E,
listener: (...args: TestsStreamEventMap[E]) => void,
): this;
prependListener(eventName: string | symbol, listener: (...args: any[]) => void): this;
prependOnceListener<E extends keyof TestsStreamEventMap>(
eventName: E,
listener: (...args: TestsStreamEventMap[E]) => void,
): this;
prependOnceListener(eventName: string | symbol, listener: (...args: any[]) => void): this;
rawListeners<E extends keyof TestsStreamEventMap>(
eventName: E,
): ((...args: TestsStreamEventMap[E]) => void)[];
rawListeners(eventName: string | symbol): ((...args: any[]) => void)[];
// eslint-disable-next-line @definitelytyped/no-unnecessary-generics
removeAllListeners<E extends keyof TestsStreamEventMap>(eventName?: E): this;
removeAllListeners(eventName?: string | symbol): this;
removeListener<E extends keyof TestsStreamEventMap>(
eventName: E,
listener: (...args: TestsStreamEventMap[E]) => void,
): this;
removeListener(eventName: string | symbol, listener: (...args: any[]) => void): this;
// #endregion
}
namespace EventData {
interface Error extends globalThis.Error {
cause: globalThis.Error;
}
interface LocationInfo {
/**
* The column number where the test is defined, or
* `undefined` if the test was run through the REPL.
*/
column?: number;
/**
* The path of the test file, `undefined` if test was run through the REPL.
*/
file?: string;
/**
* The line number where the test is defined, or `undefined` if the test was run through the REPL.
*/
line?: number;
}
interface TestDiagnostic extends LocationInfo {
/**
* The diagnostic message.
*/
message: string;
/**
* The nesting level of the test.
*/
nesting: number;
/**
* The severity level of the diagnostic message.
* Possible values are:
* * `'info'`: Informational messages.
* * `'warn'`: Warnings.
* * `'error'`: Errors.
*/
level: "info" | "warn" | "error";
}
interface TestCoverage {
/**
* An object containing the coverage report.
*/
summary: {
/**
* An array of coverage reports for individual files.
*/
files: Array<{
/**
* The absolute path of the file.
*/
path: string;
/**
* The total number of lines.
*/
totalLineCount: number;
/**
* The total number of branches.
*/
totalBranchCount: number;
/**
* The total number of functions.
*/
totalFunctionCount: number;
/**
* The number of covered lines.
*/
coveredLineCount: number;
/**
* The number of covered branches.
*/
coveredBranchCount: number;
/**
* The number of covered functions.
*/
coveredFunctionCount: number;
/**
* The percentage of lines covered.
*/
coveredLinePercent: number;
/**
* The percentage of branches covered.
*/
coveredBranchPercent: number;
/**
* The percentage of functions covered.
*/
coveredFunctionPercent: number;
/**
* An array of functions representing function coverage.
*/
functions: Array<{
/**
* The name of the function.
*/
name: string;
/**
* The line number where the function is defined.
*/
line: number;
/**
* The number of times the function was called.
*/
count: number;
}>;
/**
* An array of branches representing branch coverage.
*/
branches: Array<{
/**
* The line number where the branch is defined.
*/
line: number;
/**
* The number of times the branch was taken.
*/
count: number;
}>;
/**
* An array of lines representing line numbers and the number of times they were covered.
*/
lines: Array<{
/**
* The line number.
*/
line: number;
/**
* The number of times the line was covered.
*/
count: number;
}>;
}>;
/**
* An object containing whether or not the coverage for
* each coverage type.
* @since v22.9.0
*/
thresholds: {
/**
* The function coverage threshold.
*/
function: number;
/**
* The branch coverage threshold.
*/
branch: number;
/**
* The line coverage threshold.
*/
line: number;
};
/**
* An object containing a summary of coverage for all files.
*/
totals: {
/**
* The total number of lines.
*/
totalLineCount: number;
/**
* The total number of branches.
*/
totalBranchCount: number;
/**
* The total number of functions.
*/
totalFunctionCount: number;
/**
* The number of covered lines.
*/
coveredLineCount: number;
/**
* The number of covered branches.
*/
coveredBranchCount: number;
/**
* The number of covered functions.
*/
coveredFunctionCount: number;
/**
* The percentage of lines covered.
*/
coveredLinePercent: number;
/**
* The percentage of branches covered.
*/
coveredBranchPercent: number;
/**
* The percentage of functions covered.
*/
coveredFunctionPercent: number;
};
/**
* The working directory when code coverage began. This
* is useful for displaying relative path names in case
* the tests changed the working directory of the Node.js process.
*/
workingDirectory: string;
};
/**
* The nesting level of the test.
*/
nesting: number;
}
interface TestComplete extends LocationInfo {
/**
* Additional execution metadata.
*/
details: {
/**
* Whether the test passed or not.
*/
passed: boolean;
/**
* The duration of the test in milliseconds.
*/
duration_ms: number;
/**
* An error wrapping the error thrown by the test if it did not pass.
*/
error?: Error;
/**
* The type of the test, used to denote whether this is a suite.
*/
type?: "suite" | "test";
};
/**
* The test name.
*/
name: string;
/**
* The nesting level of the test.
*/
nesting: number;
/**
* The ordinal number of the test.
*/
testNumber: number;
/**
* Present if `context.todo` is called.
*/
todo?: string | boolean;
/**
* Present if `context.skip` is called.
*/
skip?: string | boolean;
}
interface TestDequeue extends LocationInfo {
/**
* The test name.
*/
name: string;
/**
* The nesting level of the test.
*/
nesting: number;
/**
* The test type. Either `'suite'` or `'test'`.
* @since v22.15.0
*/
type: "suite" | "test";
}
interface TestEnqueue extends LocationInfo {
/**
* The test name.
*/
name: string;
/**
* The nesting level of the test.
*/
nesting: number;
/**
* The test type. Either `'suite'` or `'test'`.
* @since v22.15.0
*/
type: "suite" | "test";
}
interface TestFail extends LocationInfo {
/**
* Additional execution metadata.
*/
details: {
/**
* The duration of the test in milliseconds.
*/
duration_ms: number;
/**
* An error wrapping the error thrown by the test.
*/
error: Error;
/**
* The type of the test, used to denote whether this is a suite.
* @since v20.0.0, v19.9.0, v18.17.0
*/
type?: "suite" | "test";
/**
* The attempt number of the test run,
* present only when using the `--test-rerun-failures` flag.
* @since v24.7.0
*/
attempt?: number;
};
/**
* The test name.
*/
name: string;
/**
* The nesting level of the test.
*/
nesting: number;
/**
* The ordinal number of the test.
*/
testNumber: number;
/**
* Present if `context.todo` is called.
*/
todo?: string | boolean;
/**
* Present if `context.skip` is called.
*/
skip?: string | boolean;
}
interface TestPass extends LocationInfo {
/**
* Additional execution metadata.
*/
details: {
/**
* The duration of the test in milliseconds.
*/
duration_ms: number;
/**
* The type of the test, used to denote whether this is a suite.
* @since 20.0.0, 19.9.0, 18.17.0
*/
type?: "suite" | "test";
/**
* The attempt number of the test run,
* present only when using the `--test-rerun-failures` flag.
* @since v24.7.0
*/
attempt?: number;
/**
* The attempt number the test passed on,
* present only when using the `--test-rerun-failures` flag.
* @since v24.7.0
*/
passed_on_attempt?: number;
};
/**
* The test name.
*/
name: string;
/**
* The nesting level of the test.
*/
nesting: number;
/**
* The ordinal number of the test.
*/
testNumber: number;
/**
* Present if `context.todo` is called.
*/
todo?: string | boolean;
/**
* Present if `context.skip` is called.
*/
skip?: string | boolean;
}
interface TestPlan extends LocationInfo {
/**
* The nesting level of the test.
*/
nesting: number;
/**
* The number of subtests that have ran.
*/
count: number;
}
interface TestStart extends LocationInfo {
/**
* The test name.
*/
name: string;
/**
* The nesting level of the test.
*/
nesting: number;
}
interface TestStderr {
/**
* The path of the test file.
*/
file: string;
/**
* The message written to `stderr`.
*/
message: string;
}
interface TestStdout {
/**
* The path of the test file.
*/
file: string;
/**
* The message written to `stdout`.
*/
message: string;
}
interface TestSummary {
/**
* An object containing the counts of various test results.
*/
counts: {
/**
* The total number of cancelled tests.
*/
cancelled: number;
/**
* The total number of passed tests.
*/
passed: number;
/**
* The total number of skipped tests.
*/
skipped: number;
/**
* The total number of suites run.
*/
suites: number;
/**
* The total number of tests run, excluding suites.
*/
tests: number;
/**
* The total number of TODO tests.
*/
todo: number;
/**
* The total number of top level tests and suites.
*/
topLevel: number;
};
/**
* The duration of the test run in milliseconds.
*/
duration_ms: number;
/**
* The path of the test file that generated the
* summary. If the summary corresponds to multiple files, this value is
* `undefined`.
*/
file: string | undefined;
/**
* Indicates whether or not the test run is considered
* successful or not. If any error condition occurs, such as a failing test or
* unmet coverage threshold, this value will be set to `false`.
*/
success: boolean;
}
}
/**
* An instance of `TestContext` is passed to each test function in order to
* interact with the test runner. However, the `TestContext` constructor is not
* exposed as part of the API.
* @since v18.0.0, v16.17.0
*/
interface TestContext {
/**
* An object containing assertion methods bound to the test context.
* The top-level functions from the `node:assert` module are exposed here for the purpose of creating test plans.
*
* **Note:** Some of the functions from `node:assert` contain type assertions. If these are called via the
* TestContext `assert` object, then the context parameter in the test's function signature **must be explicitly typed**
* (ie. the parameter must have a type annotation), otherwise an error will be raised by the TypeScript compiler:
* ```ts
* import { test, type TestContext } from 'node:test';
*
* // The test function's context parameter must have a type annotation.
* test('example', (t: TestContext) => {
* t.assert.deepStrictEqual(actual, expected);
* });
*
* // Omitting the type annotation will result in a compilation error.
* test('example', t => {
* t.assert.deepStrictEqual(actual, expected); // Error: 't' needs an explicit type annotation.
* });
* ```
* @since v22.2.0, v20.15.0
*/
readonly assert: TestContextAssert;
readonly attempt: number;
/**
* This function is used to create a hook running before subtest of the current test.
* @param fn The hook function. The first argument to this function is a `TestContext` object.
* If the hook uses callbacks, the callback function is passed as the second argument.
* @param options Configuration options for the hook.
* @since v20.1.0, v18.17.0
*/
before(fn?: TestContextHookFn, options?: HookOptions): void;
/**
* This function is used to create a hook running before each subtest of the current test.
* @param fn The hook function. The first argument to this function is a `TestContext` object.
* If the hook uses callbacks, the callback function is passed as the second argument.
* @param options Configuration options for the hook.
* @since v18.8.0
*/
beforeEach(fn?: TestContextHookFn, options?: HookOptions): void;
/**
* This function is used to create a hook that runs after the current test finishes.
* @param fn The hook function. The first argument to this function is a `TestContext` object.
* If the hook uses callbacks, the callback function is passed as the second argument.
* @param options Configuration options for the hook.
* @since v18.13.0
*/
after(fn?: TestContextHookFn, options?: HookOptions): void;
/**
* This function is used to create a hook running after each subtest of the current test.
* @param fn The hook function. The first argument to this function is a `TestContext` object.
* If the hook uses callbacks, the callback function is passed as the second argument.