1
1
interface TestOptions {
2
2
/**
3
- * The number of tests that can be run at the same time. If unspecified, subtests inherit this value from their parent.
4
- * Default: 1 .
3
+ * If a number is provided, then that many tests would run in parallel. If truthy, it would run (number of cpu cores - 1) tests in parallel. For subtests, it will be Infinity tests in parallel. If falsy, it would only run one test at a time. If unspecified, subtests inherit this value from their parent.
4
+ * @default false .
5
5
*/
6
6
concurrency ?: boolean | number ;
7
7
8
+ /**
9
+ * If truthy, and the test context is configured to run only tests, then this test will be run. Otherwise, the test is skipped.
10
+ * @default false.
11
+ */
12
+ only ?: boolean ;
13
+
8
14
/**
9
15
* If truthy, the test is skipped. If a string is provided, that string is displayed in the test results as the reason for skipping the test.
10
- * Default: false.
16
+ * @default false.
11
17
*/
12
18
skip ?: boolean | string ;
13
19
14
20
/**
15
21
* If truthy, the test marked as TODO. If a string is provided, that string is displayed in the test results as the reason why the test is TODO.
16
- * Default: false.
22
+ * @default false.
17
23
*/
18
24
todo ?: boolean | string ;
19
25
20
26
/**
21
27
* A number of milliseconds the test will fail after. If unspecified, subtests inherit this value from their parent.
22
- * Default: Infinity
28
+ * @default Infinity
23
29
*/
24
30
timeout ?: number ;
25
31
@@ -29,40 +35,114 @@ interface TestOptions {
29
35
signal ?: AbortSignal ;
30
36
}
31
37
32
- type TestFn = ( t : TestContext ) => any | Promise < any > ;
38
+ type TestFn = ( t : TestContext , done : ( result ?: any ) => void ) => any ;
33
39
34
40
export default test ;
35
41
36
- export function test ( name : string , options : TestOptions , fn : TestFn ) : void ;
37
- export function test ( name : string , fn : TestFn ) : void ;
38
- export function test ( options : TestOptions , fn : TestFn ) : void ;
39
- export function test ( fn : TestFn ) : void ;
42
+ /**
43
+ * @param name The name of the test, which is displayed when reporting test results.
44
+ * Default: The name property of fn, or '<anonymous>' if fn does not have a name.
45
+ * @param options Configuration options for the test.
46
+ * @param fn The function under test. The first argument to this function is a TestContext object. If the test uses callbacks, the callback function is passed as the second argument.
47
+ * Default: A no-op function.
48
+ * @returns A {@link Promise} resolved with `undefined` once the test completes.
49
+ */
50
+ export function test (
51
+ name : string ,
52
+ options : TestOptions ,
53
+ fn : TestFn
54
+ ) : Promise < void > ;
55
+ export function test ( name : string , fn : TestFn ) : Promise < void > ;
56
+ export function test ( options : TestOptions , fn : TestFn ) : Promise < void > ;
57
+ export function test ( fn : TestFn ) : Promise < void > ;
40
58
41
59
type SuiteFn = ( t : SuiteContext ) => void ;
42
60
61
+ /**
62
+ * @param name The name of the suite, which is displayed when reporting test results.
63
+ * Default: The name property of fn, or '<anonymous>' if fn does not have a name.
64
+ * @param options Configuration options for the suite. supports the same options as test([name][, options][, fn]).
65
+ * @param fn The function under suite declaring all subtests and subsuites. The first argument to this function is a SuiteContext object.
66
+ * Default: A no-op function.
67
+ * @returns `undefined`
68
+ */
43
69
export function describe ( name : string , options : TestOptions , fn : SuiteFn ) : void ;
44
70
export function describe ( name : string , fn : SuiteFn ) : void ;
45
71
export function describe ( options : TestOptions , fn : SuiteFn ) : void ;
46
72
export function describe ( fn : SuiteFn ) : void ;
47
73
48
- type ItFn = ( t : ItContext ) => any | Promise < any > ;
74
+ type ItFn = ( done : ( result ?: any ) => void ) => any ;
49
75
76
+ /**
77
+ * @param name The name of the test, which is displayed when reporting test results.
78
+ * Default: The name property of fn, or '<anonymous>' if fn does not have a name.
79
+ * @param options Configuration options for the suite. supports the same options as test([name][, options][, fn]).
80
+ * @param fn The function under test. If the test uses callbacks, the callback function is passed as an argument.
81
+ * Default: A no-op function.
82
+ * @returns `undefined`
83
+ */
50
84
export function it ( name : string , options : TestOptions , fn : ItFn ) : void ;
51
85
export function it ( name : string , fn : ItFn ) : void ;
52
86
export function it ( options : TestOptions , fn : ItFn ) : void ;
53
87
export function it ( fn : ItFn ) : void ;
54
88
89
+ type HookFn = ( done : ( result ?: any ) => void ) => any ;
90
+
91
+ /**
92
+ * This function is used to create a hook running before running a suite.
93
+ *
94
+ * @param fn The hook function. If the hook uses callbacks, the callback function is passed as the second argument.
95
+ * Default: A no-op function.
96
+ * @param options Configuration options for the hook.
97
+ */
98
+ export function before ( fn ?: HookFn , options ?: HookOptions ) : void ;
99
+
100
+ /**
101
+ * This function is used to create a hook running after running a suite.
102
+ *
103
+ * @param fn The hook function. If the hook uses callbacks, the callback function is passed as the second argument.
104
+ * Default: A no-op function.
105
+ * @param options Configuration options for the hook.
106
+ */
107
+ export function after ( fn ?: HookFn , options ?: HookOptions ) : void ;
108
+
109
+ /**
110
+ * This function is used to create a hook running before each subtest of the current suite.
111
+ *
112
+ * @param fn The hook function. If the hook uses callbacks, the callback function is passed as the second argument.
113
+ * Default: A no-op function.
114
+ * @param options Configuration options for the hook.
115
+ */
116
+ export function beforeEach ( fn ?: HookFn , options ?: HookOptions ) : void ;
117
+
118
+ /**
119
+ * This function is used to create a hook running after each subtest of the current test.
120
+ *
121
+ * @param fn The hook function. If the hook uses callbacks, the callback function is passed as the second argument.
122
+ * Default: A no-op function.
123
+ * @param options Configuration options for the hook.
124
+ */
125
+ export function afterEach ( fn ?: HookFn , options ?: HookOptions ) : void ;
126
+
55
127
/**
56
128
* An instance of TestContext is passed to each test function in order to interact with the test runner.
57
129
* However, the TestContext constructor is not exposed as part of the API.
58
130
*/
59
131
declare class TestContext {
132
+ /**
133
+ * This function is used to create a hook running before each subtest of the current test.
134
+ */
135
+ public beforeEach : typeof beforeEach ;
136
+
137
+ /**
138
+ * This function is used to create a hook running after each subtest of the current test.
139
+ */
140
+ public afterEach : typeof afterEach ;
141
+
60
142
/**
61
143
* This function is used to create subtests under the current test. This function behaves in the same fashion as the top level test() function.
62
144
*/
63
- public test ( name : string , options : TestOptions , fn : TestFn ) : Promise < void > ;
64
- public test ( name : string , fn : TestFn ) : Promise < void > ;
65
- public test ( fn : TestFn ) : Promise < void > ;
145
+ public test : typeof test ;
66
146
67
147
/**
68
148
* This function is used to write TAP diagnostics to the output.
@@ -72,6 +152,20 @@ declare class TestContext {
72
152
*/
73
153
public diagnostic ( message : string ) : void ;
74
154
155
+ /**
156
+ * The name of the test.
157
+ */
158
+ public name : string ;
159
+
160
+ /**
161
+ * If `shouldRunOnlyTests` is truthy, the test context will only run tests that have the `only`
162
+ * option set. Otherwise, all tests are run. If Node.js was not started with the `--test-only`
163
+ * command-line option, this function is a no-op.
164
+ *
165
+ * @param shouldRunOnlyTests Whether or not to run `only` tests.
166
+ */
167
+ public runOnly ( shouldRunOnlyTests : boolean ) : void ;
168
+
75
169
/**
76
170
* This function causes the test's output to indicate the test as skipped.
77
171
* If message is provided, it is included in the TAP output.
@@ -108,12 +202,19 @@ declare class SuiteContext {
108
202
}
109
203
110
204
/**
111
- * An instance of ItContext is passed to each suite function in order to interact with the test runner.
112
- * However, the ItContext constructor is not exposed as part of the API.
205
+ * Configuration options for hooks.
113
206
*/
114
- declare class ItContext {
207
+ interface HookOptions {
115
208
/**
116
- * Can be used to abort test subtasks when the test has been aborted .
209
+ * Allows aborting an in-progress hook .
117
210
*/
118
- public signal : AbortSignal ;
211
+ signal ?: AbortSignal ;
212
+
213
+ /**
214
+ * A number of milliseconds the hook will fail after. If unspecified, subtests inherit this
215
+ * value from their parent.
216
+ *
217
+ * @default Infinity
218
+ */
219
+ timeout ?: number ;
119
220
}
0 commit comments