Skip to content

Commit 8362f80

Browse files
authored
fix(types): align describe and test callback types (#1422)
1 parent a590d49 commit 8362f80

5 files changed

Lines changed: 44 additions & 19 deletions

File tree

packages/core/src/types/api.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ export interface TestEachFn {
8080
cases: readonly T[],
8181
): (
8282
description: string,
83-
fn: (...args: [...T]) => MaybePromise<void>,
83+
fn?: (...args: [...T]) => MaybePromise<void>,
8484
options?: number | TestOptions,
8585
) => void;
8686
<T>(
8787
cases: readonly T[],
8888
): (
8989
description: string,
90-
fn: (...args: T[]) => MaybePromise<void>,
90+
fn?: (...args: T[]) => MaybePromise<void>,
9191
options?: number | TestOptions,
9292
) => void;
9393
<T extends Record<string, unknown>>(
@@ -124,10 +124,13 @@ export interface DescribeEachFn {
124124
): (description: string, fn?: (param: T) => MaybePromise<void>) => void;
125125
<T extends readonly [unknown, ...unknown[]]>(
126126
cases: readonly T[],
127-
): (description: string, fn: (...args: [...T]) => MaybePromise<void>) => void;
127+
): (
128+
description: string,
129+
fn?: (...args: [...T]) => MaybePromise<void>,
130+
) => void;
128131
<T>(
129132
cases: readonly T[],
130-
): (description: string, fn: (param: T) => MaybePromise<void>) => void;
133+
): (description: string, fn?: (param: T) => MaybePromise<void>) => void;
131134
<T extends Record<string, unknown>>(
132135
strings: TemplateStringsArray,
133136
...expressions: unknown[]
@@ -157,7 +160,7 @@ export type TestAPI<ExtraContext = object> = TestFn<ExtraContext> & {
157160
skipIf: (condition: boolean) => TestAPI<ExtraContext>;
158161
};
159162

160-
type DescribeFn = (description: string, fn?: () => void) => void;
163+
type DescribeFn = (description: string, fn?: () => MaybePromise<void>) => void;
161164

162165
export type DescribeAPI = DescribeFn & {
163166
each: DescribeEachFn;

website/docs/en/api/runtime-api/test-api/describe.mdx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ description: describe defines a test suite. It supports chainable modifiers and
99

1010
## describe
1111

12-
- **Type:** `(name: string, fn: () => void | Promise<void>) => void`
12+
- **Type:** `(name: string, fn?: () => void | Promise<void>) => void`
1313

1414
Defines a test suite that can contain multiple test cases or nested describe blocks.
15+
The callback can be asynchronous; Rstest waits for it during collection before running the tests declared inside it.
1516

1617
```ts
17-
import { describe, test } from '@rstest/core';
18+
import { describe, expect, test } from '@rstest/core';
1819

1920
describe('math', () => {
2021
test('add', () => {
@@ -26,6 +27,16 @@ describe('math', () => {
2627
});
2728
```
2829

30+
```ts
31+
describe('async setup', async () => {
32+
const data = await loadFixtureData();
33+
34+
test('uses collected data', () => {
35+
expect(data).toBeDefined();
36+
});
37+
});
38+
```
39+
2940
## describe.only
3041

3142
Only run the describe block(s) marked with `only`.
@@ -68,7 +79,7 @@ describe.todo('should implement this suite');
6879

6980
## describe.each
7081

71-
- **Type:** `describe.each(cases: ReadonlyArray<T>)(name: string, fn: (param: T) => void | Promise<void>) => void`
82+
- **Type:** `describe.each(cases: ReadonlyArray<T>)(name: string, fn?: (param: T) => void | Promise<void>) => void`
7283

7384
Creates a describe block for each item in the provided array.
7485

@@ -113,7 +124,7 @@ describe.each<{ a: number; b: number; expected: number }>`
113124

114125
## describe.for
115126

116-
- **Type:** `describe.for(cases: ReadonlyArray<T>)(name: string, fn: (param: T) => void | Promise<void>) => void`
127+
- **Type:** `describe.for(cases: ReadonlyArray<T>)(name: string, fn?: (param: T) => void | Promise<void>) => void`
117128

118129
Alternative to `describe.each` for more flexible parameter types.
119130

website/docs/en/api/runtime-api/test-api/test.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Alias: `it`.
1313

1414
## test
1515

16-
- **Type:** `(name: string, fn: (testContext: TestContext) => void | Promise<void>, options?: number | TestOptions) => void`
16+
- **Type:** `(name: string, fn?: (testContext: TestContext) => void | Promise<void>, options?: number | TestOptions) => void`
1717

1818
Defines a test case.
1919

@@ -114,7 +114,7 @@ test.todo('should implement this test');
114114

115115
## test.each
116116

117-
- **Type:** `test.each(cases: ReadonlyArray<T>)(name: string, fn: (param: T) => void | Promise<void>, options?: number | TestOptions) => void`
117+
- **Type:** `test.each(cases: ReadonlyArray<T>)(name: string, fn?: (param: T) => void | Promise<void>, options?: number | TestOptions) => void`
118118

119119
Runs the same test logic for each item in the provided array.
120120

@@ -197,7 +197,7 @@ test.each([
197197

198198
## test.for
199199

200-
- **Type:** `test.for(cases: ReadonlyArray<T>)(name: string, fn: (param: T, testContext: TestContext) => void | Promise<void>, options?: number | TestOptions) => void`
200+
- **Type:** `test.for(cases: ReadonlyArray<T>)(name: string, fn?: (param: T, testContext: TestContext) => void | Promise<void>, options?: number | TestOptions) => void`
201201

202202
Alternative to `test.each` to provide `TestContext`.
203203

website/docs/zh/api/runtime-api/test-api/describe.mdx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ description: describe 用于定义测试套件(test suite),支持链式修
99

1010
## describe
1111

12-
- **类型:** `(name: string, fn: () => void | Promise<void>) => void`
12+
- **类型:** `(name: string, fn?: () => void | Promise<void>) => void`
1313

1414
定义一个测试套件,可以包含多个测试用例或嵌套的 describe。
15+
回调可以是异步函数;Rstest 会在收集阶段等待它完成,然后再运行其中声明的测试。
1516

1617
```ts
17-
import { describe, test } from '@rstest/core';
18+
import { describe, expect, test } from '@rstest/core';
1819

1920
describe('math', () => {
2021
test('add', () => {
@@ -26,6 +27,16 @@ describe('math', () => {
2627
});
2728
```
2829

30+
```ts
31+
describe('async setup', async () => {
32+
const data = await loadFixtureData();
33+
34+
test('uses collected data', () => {
35+
expect(data).toBeDefined();
36+
});
37+
});
38+
```
39+
2940
## describe.only
3041

3142
只运行被 only 标记的 describe 块。
@@ -68,7 +79,7 @@ describe.todo('should implement this suite');
6879

6980
## describe.each
7081

71-
- **类型:** `describe.each(cases: ReadonlyArray<T>)(name: string, fn: (param: T) => void | Promise<void>) => void`
82+
- **类型:** `describe.each(cases: ReadonlyArray<T>)(name: string, fn?: (param: T) => void | Promise<void>) => void`
7283

7384
为数组中的每一项生成一个 describe 块。
7485

@@ -113,7 +124,7 @@ describe.each<{ a: number; b: number; expected: number }>`
113124

114125
## describe.for
115126

116-
- **类型:** `describe.for(cases: ReadonlyArray<T>)(name: string, fn: (param: T) => void | Promise<void>) => void`
127+
- **类型:** `describe.for(cases: ReadonlyArray<T>)(name: string, fn?: (param: T) => void | Promise<void>) => void`
117128

118129
类似 each,但参数类型更灵活。
119130

website/docs/zh/api/runtime-api/test-api/test.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { ApiMeta } from '@components/ApiMeta';
1313

1414
## test
1515

16-
- **类型:** `(name: string, fn: (testContext: TestContext) => void | Promise<void>, options?: number | TestOptions) => void`
16+
- **类型:** `(name: string, fn?: (testContext: TestContext) => void | Promise<void>, options?: number | TestOptions) => void`
1717

1818
定义一个测试用例。
1919

@@ -114,7 +114,7 @@ test.todo('should implement this test');
114114

115115
## test.each
116116

117-
- **类型:** `test.each(cases: ReadonlyArray<T>)(name: string, fn: (param: T) => void | Promise<void>, options?: number | TestOptions) => void`
117+
- **类型:** `test.each(cases: ReadonlyArray<T>)(name: string, fn?: (param: T) => void | Promise<void>, options?: number | TestOptions) => void`
118118

119119
对提供的数组中的每一项运行相同的测试逻辑。
120120

@@ -197,7 +197,7 @@ test.each([
197197

198198
## test.for
199199

200-
- **类型:** `test.for(cases: ReadonlyArray<T>)(name: string, fn: (param: T, testContext: TestContext) => void | Promise<void>, options?: number | TestOptions) => void`
200+
- **类型:** `test.for(cases: ReadonlyArray<T>)(name: string, fn?: (param: T, testContext: TestContext) => void | Promise<void>, options?: number | TestOptions) => void`
201201

202202
`test.each` 的替代方案,提供 `TestContext`
203203

0 commit comments

Comments
 (0)