Skip to content

Commit d5daec2

Browse files
9aoyCopilot
andauthored
docs: add Migrating from Jest chapter (#574)
Co-authored-by: Copilot <[email protected]>
1 parent 4a9201e commit d5daec2

File tree

4 files changed

+200
-0
lines changed

4 files changed

+200
-0
lines changed

website/docs/en/guide/_meta.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,10 @@
1313
"type": "dir",
1414
"name": "advanced",
1515
"label": "Advanced"
16+
},
17+
{
18+
"type": "dir",
19+
"name": "migration",
20+
"label": "Migration"
1621
}
1722
]
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Migrating from jest
2+
3+
Rstest is designed to be Jest-compatible, making migration from Jest projects straightforward. Here's how to migrate your Jest project to Rstest:
4+
5+
## Installation and setup
6+
7+
First, you need to install Rstest as a development dependency.
8+
9+
import { PackageManagerTabs } from '@theme';
10+
11+
<PackageManagerTabs command="add @rstest/core -D" />
12+
13+
Next, update the test script in your `package.json` to use [rstest](/guide/basic/cli) instead of `jest`. For example:
14+
15+
```diff
16+
"scripts": {
17+
- "test": "jest"
18+
+ "test": "rstest"
19+
}
20+
```
21+
22+
## Configuration migration
23+
24+
Update your jest config file (e.g., `jest.config.js` or `jest.config.ts`) to a `rstest.config.ts` file.
25+
26+
```ts title='rstest.config.ts'
27+
import { defineConfig } from '@rstest/core';
28+
29+
export default defineConfig({
30+
globals: true,
31+
});
32+
```
33+
34+
Here are some common Jest configurations and their Rstest equivalents:
35+
36+
| Jest Configuration | Rstest Equivalent |
37+
| ---------------------------- | -------------------- |
38+
| `testRegex` | `include` |
39+
| `testMatch` | `include` |
40+
| `testPathIgnorePatterns` | `exclude` |
41+
| `injectGlobals` | `globals` |
42+
| `moduleNameMapper` | `resolve.alias` |
43+
| `collectCoverage` | `coverage.enabled` |
44+
| `coverageDirectory` | `coverage.directory` |
45+
| `coverageProvider` | `coverage.provider` |
46+
| `coveragePathIgnorePatterns` | `coverage.exclude` |
47+
48+
For more details, please refer to the [Configuration](/config) section.
49+
50+
### Inject globals
51+
52+
Rstest does not mount the test APIs (e.g., `describe`, `expect`, `it`, `test`) to the global object by default, which is different from Jest.
53+
54+
If you want to continue using the global test APIs, you can enable the `globals` option in your `rstest.config.ts` file:
55+
56+
```ts title='rstest.config.ts'
57+
import { defineConfig } from '@rstest/core';
58+
59+
export default defineConfig({
60+
globals: true,
61+
});
62+
```
63+
64+
### Code transformation
65+
66+
Rstest uses `swc` for code transformation by default, which is different from Jest's `babel-jest`. Most of the time, you don't need to change anything.
67+
68+
However, if you have custom Babel configurations or use specific Babel plugins/presets, you can add [Rsbuild's Babel Plugin](https://rsbuild.rs/plugins/list/plugin-babel):
69+
70+
```ts title='rstest.config.ts'
71+
import { pluginBabel } from '@rsbuild/plugin-babel';
72+
import { defineConfig } from '@rstest/core';
73+
74+
export default defineConfig({
75+
plugins: [pluginBabel()],
76+
});
77+
```
78+
79+
## Update test API
80+
81+
Your existing Jest test files should work with minimal changes since Rstest provides Jest-compatible APIs. Simply update your imports from Jest to Rstest:
82+
83+
```diff
84+
- import { describe, expect, it, test } from '@jest/globals';
85+
+ import { describe, expect, it, test } from '@rstest/core';
86+
```
87+
88+
Rstest provides a `rstest` API that you can use to access Rstest's utilities, such as `rstest.fn()` and `rstest.mock()`. Just like Jest's `jest.fn()` and `jest.mock()`. More utilities can be found in the [Rstest APIs](/api/).
89+
90+
```diff
91+
- const fn = jest.fn();
92+
+ const fn = rstest.fn();
93+
94+
fn.mockResolvedValue('foo');
95+
```

website/docs/zh/guide/_meta.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,10 @@
1313
"type": "dir",
1414
"name": "advanced",
1515
"label": "进阶"
16+
},
17+
{
18+
"type": "dir",
19+
"name": "migration",
20+
"label": "迁移"
1621
}
1722
]
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# 从 Jest 迁移
2+
3+
Rstest 提供兼容 Jest 的 API,这使得从 Jest 项目迁移变得简单。以下是如何将你的 Jest 项目迁移到 Rstest:
4+
5+
## 安装依赖
6+
7+
首先,你需要安装 Rstest 依赖。
8+
9+
import { PackageManagerTabs } from '@theme';
10+
11+
<PackageManagerTabs command="add @rstest/core -D" />
12+
13+
接下来,更新 `package.json` 中的测试脚本,使用 [rstest](/guide/basic/cli) 替代 `jest`。例如:
14+
15+
```diff
16+
"scripts": {
17+
- "test": "jest"
18+
+ "test": "rstest"
19+
}
20+
```
21+
22+
## 配置迁移
23+
24+
将你的 jest 配置文件(例如 `jest.config.js``jest.config.ts`)更新为 `rstest.config.ts` 文件:
25+
26+
```ts title='rstest.config.ts'
27+
import { defineConfig } from '@rstest/core';
28+
29+
export default defineConfig({
30+
globals: true,
31+
});
32+
```
33+
34+
以下是一些常见的 Jest 配置及其对应的 Rstest 配置:
35+
36+
| Jest 配置 | Rstest 等效配置 |
37+
| ---------------------------- | -------------------- |
38+
| `testRegex` | `include` |
39+
| `testMatch` | `include` |
40+
| `testPathIgnorePatterns` | `exclude` |
41+
| `injectGlobals` | `globals` |
42+
| `moduleNameMapper` | `resolve.alias` |
43+
| `collectCoverage` | `coverage.enabled` |
44+
| `coverageDirectory` | `coverage.directory` |
45+
| `coverageProvider` | `coverage.provider` |
46+
| `coveragePathIgnorePatterns` | `coverage.exclude` |
47+
48+
更多详情,请参考[配置](/config)部分。
49+
50+
### 注入全局 API
51+
52+
与 Jest 不同,Rstest 默认不会将测试 API(如 `describe``expect``it``test`)挂载到全局对象上。
53+
54+
如果你希望继续使用全局测试 API,可以在 `rstest.config.ts` 文件中启用 `globals` 选项:
55+
56+
```ts title='rstest.config.ts'
57+
import { defineConfig } from '@rstest/core';
58+
59+
export default defineConfig({
60+
globals: true,
61+
});
62+
```
63+
64+
### 代码转换
65+
66+
Rstest 默认使用 `swc` 进行代码转换,这与 Jest 的 `babel-jest` 不同。大多数情况下,你不需要做任何更改。
67+
68+
如果你有自定义的 Babel 配置或使用特定的 Babel 插件/预设,你可以添加 [Rsbuild Babel 插件](https://rsbuild.rs/zh/plugins/list/plugin-babel)
69+
70+
```ts title='rstest.config.ts'
71+
import { pluginBabel } from '@rsbuild/plugin-babel';
72+
import { defineConfig } from '@rstest/core';
73+
74+
export default defineConfig({
75+
plugins: [pluginBabel()],
76+
});
77+
```
78+
79+
## 更新测试 API
80+
81+
Rstest 提供了与 Jest 兼容的 API。因此,你只需将导入从 Jest 更改为 Rstest:
82+
83+
```diff
84+
- import { describe, expect, it, test } from '@jest/globals';
85+
+ import { describe, expect, it, test } from '@rstest/core';
86+
```
87+
88+
Rstest 提供了一个 `rstest` API,你可以使用它来访问 Rstest 的工具函数,如 `rstest.fn()``rstest.mock()`。就像 Jest 的 `jest.fn()``jest.mock()` 一样。更多工具函数可以在 [Rstest APIs](/api/) 中找到。
89+
90+
```diff
91+
- const fn = jest.fn();
92+
+ const fn = rstest.fn();
93+
94+
fn.mockResolvedValue('foo');
95+
```

0 commit comments

Comments
 (0)