Skip to content

Commit 6e4430b

Browse files
authored
docs: introduce beforeEach / beforeAll returns (#607)
1 parent 9c17678 commit 6e4430b

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@ beforeAll(async (ctx) => {
2121
});
2222
```
2323

24+
`beforeAll` also supports returning a function that runs after all tests for cleanup (equivalent to `afterAll`):
25+
26+
```ts
27+
import { beforeAll } from '@rstest/core';
28+
29+
beforeAll(async () => {
30+
const cleanUp = await doSomething();
31+
32+
// Cleanup logic after all tests
33+
return async () => {
34+
await cleanUp();
35+
};
36+
});
37+
```
38+
2439
## afterAll
2540

2641
- **Type:** `(fn: (ctx: SuiteContext) => void | Promise<void>, timeout?: number) => void`
@@ -49,6 +64,21 @@ beforeEach(async () => {
4964
});
5065
```
5166

67+
`beforeEach` also supports returning a function that runs after each test for cleanup (equivalent to `afterEach`):
68+
69+
```ts
70+
import { beforeEach } from '@rstest/core';
71+
72+
beforeEach(async () => {
73+
const cleanUp = await doSomething();
74+
75+
// Cleanup logic after each test
76+
return async () => {
77+
await cleanUp();
78+
};
79+
});
80+
```
81+
5282
## afterEach
5383

5484
- **Type:** `(fn: (ctx: { task: { result: Readonly<TestResult> } }) => void | Promise<void>, timeout?: number) => void`

website/docs/en/guide/migration/jest.mdx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,15 @@ The `done` callback is not supported in Rstest. Instead, you can return a Promis
129129
+ }));
130130
```
131131

132+
### Hooks
133+
134+
The return functions of the `beforeEach` and `beforeAll` hooks in Rstest are used to perform cleaning work after testing.
135+
136+
```diff
137+
- beforeEach(() => doSomething());
138+
+ beforeEach(() => { doSomething() });
139+
```
140+
132141
### Timeout
133142

134143
If you used `jest.setTimeout()` to set the timeout for a test, you can use `rstest.setConfig()` instead.

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@ beforeAll(async (ctx) => {
2121
});
2222
```
2323

24+
`beforeAll` 也支持返回一个函数,在所有测试之后运行,用于清理操作(等价于 `afterAll`):
25+
26+
```ts
27+
import { beforeAll } from '@rstest/core';
28+
29+
beforeAll(async () => {
30+
const cleanUp = await doSomething();
31+
32+
// 在所有测试后的清理逻辑
33+
return async () => {
34+
await cleanUp();
35+
};
36+
});
37+
```
38+
2439
## afterAll
2540

2641
- **类型:** `(fn: (ctx: SuiteContext) => void | Promise<void>, timeout?: number) => void`
@@ -49,6 +64,21 @@ beforeEach(async () => {
4964
});
5065
```
5166

67+
`beforeEach` 也支持返回一个函数,在每个测试之后运行,用于清理操作(等价于 `afterEach`):
68+
69+
```ts
70+
import { beforeEach } from '@rstest/core';
71+
72+
beforeEach(async () => {
73+
const cleanUp = await doSomething();
74+
75+
// 每个测试后的清理逻辑
76+
return async () => {
77+
await cleanUp();
78+
};
79+
});
80+
```
81+
5282
## afterEach
5383

5484
- **类型:** `(fn: (ctx: { task: { result: Readonly<TestResult> } }) => void | Promise<void>, timeout?: number) => void`

website/docs/zh/guide/migration/jest.mdx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,15 @@ Rstest 不支持 `done` 回调。作为替代,你可以返回一个 Promise
129129
+ }));
130130
```
131131

132+
### Hooks
133+
134+
Rstest 中 `beforeEach``beforeAll` 钩子的返回函数用于执行测试后的清理工作。
135+
136+
```diff
137+
- beforeEach(() => doSomething());
138+
+ beforeEach(() => { doSomething() });
139+
```
140+
132141
### 超时设置
133142

134143
如果你使用 `jest.setTimeout()` 来设置测试的超时时间,你可以改用 `rstest.setConfig()`

0 commit comments

Comments
 (0)