Skip to content

Commit 2990739

Browse files
committed
fix: improve types
1 parent 9b04919 commit 2990739

File tree

2 files changed

+121
-19
lines changed

2 files changed

+121
-19
lines changed

.prettierrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

lib/test.d.ts

Lines changed: 120 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
interface TestOptions {
22
/**
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.
55
*/
66
concurrency?: boolean | number;
77

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+
814
/**
915
* 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.
1117
*/
1218
skip?: boolean | string;
1319

1420
/**
1521
* 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.
1723
*/
1824
todo?: boolean | string;
1925

2026
/**
2127
* A number of milliseconds the test will fail after. If unspecified, subtests inherit this value from their parent.
22-
* Default: Infinity
28+
* @default Infinity
2329
*/
2430
timeout?: number;
2531

@@ -29,40 +35,114 @@ interface TestOptions {
2935
signal?: AbortSignal;
3036
}
3137

32-
type TestFn = (t: TestContext) => any | Promise<any>;
38+
type TestFn = (t: TestContext, done: (result?: any) => void) => any;
3339

3440
export default test;
3541

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>;
4058

4159
type SuiteFn = (t: SuiteContext) => void;
4260

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+
*/
4369
export function describe(name: string, options: TestOptions, fn: SuiteFn): void;
4470
export function describe(name: string, fn: SuiteFn): void;
4571
export function describe(options: TestOptions, fn: SuiteFn): void;
4672
export function describe(fn: SuiteFn): void;
4773

48-
type ItFn = (t: ItContext) => any | Promise<any>;
74+
type ItFn = (done: (result?: any) => void) => any;
4975

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+
*/
5084
export function it(name: string, options: TestOptions, fn: ItFn): void;
5185
export function it(name: string, fn: ItFn): void;
5286
export function it(options: TestOptions, fn: ItFn): void;
5387
export function it(fn: ItFn): void;
5488

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+
55127
/**
56128
* An instance of TestContext is passed to each test function in order to interact with the test runner.
57129
* However, the TestContext constructor is not exposed as part of the API.
58130
*/
59131
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+
60142
/**
61143
* This function is used to create subtests under the current test. This function behaves in the same fashion as the top level test() function.
62144
*/
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;
66146

67147
/**
68148
* This function is used to write TAP diagnostics to the output.
@@ -72,6 +152,20 @@ declare class TestContext {
72152
*/
73153
public diagnostic(message: string): void;
74154

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+
75169
/**
76170
* This function causes the test's output to indicate the test as skipped.
77171
* If message is provided, it is included in the TAP output.
@@ -108,12 +202,19 @@ declare class SuiteContext {
108202
}
109203

110204
/**
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.
113206
*/
114-
declare class ItContext {
207+
interface HookOptions {
115208
/**
116-
* Can be used to abort test subtasks when the test has been aborted.
209+
* Allows aborting an in-progress hook.
117210
*/
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;
119220
}

0 commit comments

Comments
 (0)