Skip to content

Commit cb6c951

Browse files
9aoyCopilot
andauthored
docs: add projects guide (#724)
Co-authored-by: Copilot <[email protected]>
1 parent f87674d commit cb6c951

File tree

11 files changed

+348
-158
lines changed

11 files changed

+348
-158
lines changed

website/docs/en/config/test/projects.mdx

Lines changed: 1 addition & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ Define multiple test projects. It can be an array of directories, configuration
1717

1818
Rstest will run the tests for each project according to the configuration defined in each project, and the test results from all projects will be combined and displayed.
1919

20-
You can filter the specified projects to run by using the [--project](/guide/basic/test-filter#filter-by-project-name) option.
21-
22-
If there is no `projects` field, `rstest` will treat current directory as a single project.
23-
2420
```ts name='rstest.config.ts'
2521
import { defineConfig } from '@rstest/core';
2622

@@ -52,67 +48,4 @@ export default defineConfig({
5248
});
5349
```
5450

55-
## Configuration notes
56-
57-
- Project configuration does not inherit root configuration. If there is shared configuration between your sub-projects, you can extract the shared configuration and import it in the sub-project.
58-
- Some root-level options such as `reporters`, `pool`, and `isolate` are not valid in a project configuration.
59-
60-
```ts title='packages/pkg-a/rstest.config.ts'
61-
import { defineConfig } from '@rstest/core';
62-
import sharedConfig from '../shared/rstest.config';
63-
64-
export default defineConfig({
65-
...sharedConfig,
66-
});
67-
```
68-
69-
## Inline configuration
70-
71-
`rstest` supports configuring projects inline in the `projects` field. This lets you define multiple test projects in a single root without creating separate config files for each test project.
72-
73-
```ts name='rstest.config.ts'
74-
import { defineConfig } from '@rstest/core';
75-
76-
export default defineConfig({
77-
projects: [
78-
// inline project configs
79-
{
80-
name: 'node',
81-
include: ['tests/node/**/*.{test,spec}.{js,cjs,mjs,ts,tsx}'],
82-
},
83-
{
84-
name: 'react',
85-
include: ['tests/react/**/*.{test,spec}.{js,cjs,mjs,ts,tsx}'],
86-
testEnvironment: 'jsdom',
87-
},
88-
],
89-
});
90-
```
91-
92-
## Nested projects
93-
94-
Rstest supports nested project definitions within the rstest config of sub-projects. This allows you to define more test projects in sub-projects without having to define all projects in the root project.
95-
96-
This is especially useful when your sub-projects need to support multiple test environments or multiple configurations.
97-
98-
### Example
99-
100-
Define Node.js and browser test environments for `packages/pkg-a`:
101-
102-
```ts title='packages/pkg-a/rstest.config.ts'
103-
import { defineConfig } from '@rstest/core';
104-
105-
export default defineConfig({
106-
projects: [
107-
{
108-
name: 'node',
109-
include: ['tests/node/**/*.{test,spec}.?(c|m)[jt]s?(x)'],
110-
},
111-
{
112-
name: 'react',
113-
include: ['tests/react/**/*.{test,spec}.?(c|m)[jt]s?(x)'],
114-
testEnvironment: 'jsdom',
115-
},
116-
],
117-
});
118-
```
51+
More information about projects can be found in [Test projects](/guide/basic/projects).

website/docs/en/guide/advanced/debugging.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Debug mode
44

5-
Rstest provides a debug mode to troubleshoot problems, you can add the `DEBUG=rstest` environment variable when building to enable Rstest's debug mode.
5+
Rstest provides a debug mode to troubleshoot problems, you can add the `DEBUG=rstest` environment variable when testing to enable Rstest's debug mode.
66

77
```bash
88
DEBUG=rstest pnpm test
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
["cli", "configure-rstest", "test-filter", "vscode-extension", "upgrade-rstest"]
1+
[
2+
"cli",
3+
"configure-rstest",
4+
"test-filter",
5+
"projects",
6+
"vscode-extension",
7+
"upgrade-rstest"
8+
]
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
# Test projects
2+
3+
Rstest supports running multiple test projects simultaneously within a single Rstest process. These projects can have different test configurations and test environments.
4+
5+
## Use cases
6+
7+
- **Monorepo projects**: When your codebase contains multiple sub-packages or modules, each sub-project might have different test configuration requirements.
8+
- **Different test environments**: Different sub-projects (or even the same sub-project) might need to run in different environments (e.g., Node environment and browser environment).
9+
- **File-specific testing**: You can specify different test or build configurations for specific test files/directories.
10+
11+
## Example
12+
13+
Define each sub-project in a monorepo as a project through the [projects](/config/test/projects) field, where each sub-project has its own test configuration.
14+
15+
Rstest will automatically recognize each subdirectory under the `packages` directory as an independent test project and run tests according to the [rstest.config.ts](/guide/basic/configure-rstest#configuration-files) file (if present) in the subdirectory.
16+
17+
```ts name='rstest.config.ts'
18+
import { defineConfig } from '@rstest/core';
19+
20+
export default defineConfig({
21+
projects: [
22+
// A monorepo: each package directory is a project
23+
'packages/*',
24+
],
25+
});
26+
```
27+
28+
## Configuration description
29+
30+
You can define multiple test projects through the [projects](/config/test/projects) field. Rstest will run the corresponding tests according to the configurations defined by each project, and all test results will be merged and displayed.
31+
32+
If there is no `projects` field, `rstest` will treat the current directory as a single project.
33+
34+
### Configuration syntax
35+
36+
The `projects` field supports the following configuration methods:
37+
38+
- **Directory path**: Specify a directory, and Rstest will automatically recognize all subdirectories under that directory as projects.
39+
- **Configuration file path**: Specify a configuration file path, and Rstest will run tests according to that file's configuration.
40+
- **Glob pattern**: Use glob patterns to match multiple directories or files, and Rstest will use the matching results as projects.
41+
- **Inline configuration object**: Directly define project configuration objects in the `projects` field. This allows you to define multiple test projects within a single project without creating separate configuration files for each test project.
42+
- **Nested projects**: Define projects nested within the rstest config of sub-projects.
43+
44+
```ts name='rstest.config.ts'
45+
import { defineConfig } from '@rstest/core';
46+
47+
export default defineConfig({
48+
projects: [
49+
// A monorepo: each package directory is a project
50+
'packages/*',
51+
52+
// All projects under the apps directory that provide an rstest config file
53+
'apps/**/rstest.config.ts',
54+
55+
// A specific project directory
56+
'<rootDir>/services/auth',
57+
58+
// A specific project's config file
59+
'./projects/web/rstest.config.ts',
60+
61+
// inline project configs
62+
{
63+
name: 'node',
64+
include: ['tests/node/**/*.{test,spec}.{js,cjs,mjs,ts,tsx}'],
65+
},
66+
{
67+
name: 'react',
68+
include: ['tests/react/**/*.{test,spec}.{js,cjs,mjs,ts,tsx}'],
69+
testEnvironment: 'jsdom',
70+
},
71+
],
72+
});
73+
```
74+
75+
### Root configuration
76+
77+
When the `projects` field exists in the root directory's rstest config file, Rstest will not treat it as a test project. In this case, the root directory's rstest.config file is only used to define `projects` and [global configuration](#global-configuration).
78+
79+
If you want to treat the root directory as a project as well, you can define the test configuration for the root directory in `projects`.
80+
81+
```ts name='rstest.config.ts'
82+
import { defineConfig } from '@rstest/core';
83+
84+
export default defineConfig({
85+
projects: [
86+
{
87+
name: 'root',
88+
include: ['<rootDir>/tests/**/*.{test,spec}.{js,cjs,mjs,ts,tsx}'],
89+
},
90+
'packages/*',
91+
],
92+
});
93+
```
94+
95+
#### Global configuration
96+
97+
The following configurations are global configurations and are invalid when configured in projects. If you need to modify global configuration, you need to configure it in the root project's rstest config or override it through CLI options.
98+
99+
- `reporters`: Global reporters configuration.
100+
- `pool`: Global pool configuration.
101+
- `isolate`: Global isolate configuration.
102+
- `coverage`: Global coverage configuration.
103+
- `bail`: Global bail configuration.
104+
105+
### Project configuration
106+
107+
Project configuration does not inherit the configuration from the root directory. Only the `projects` field and [global configuration](#global-configuration) in the root directory are effective.
108+
109+
If there are reusable configuration items between your sub-projects, you can extract shared configurations and introduce them in sub-projects respectively:
110+
111+
```ts title='packages/pkg-a/rstest.config.ts'
112+
import { defineConfig, mergeRstestConfig } from '@rstest/core';
113+
import sharedConfig from '../shared/rstest.config';
114+
115+
export default mergeRstestConfig(sharedConfig, {
116+
name: 'pkg-a',
117+
});
118+
```
119+
120+
You can override all project configurations through [CLI options](/guide/basic/cli#cli-options).
121+
122+
### Nested projects
123+
124+
Rstest supports defining nested projects in sub-project rstest config files. This allows you to define more test projects in sub-projects without defining all projects in the root project.
125+
126+
This is especially useful when your sub-projects need to support multiple test environments or multiple configurations.
127+
128+
For example, define Node.js and browser test environments for `packages/pkg-a`:
129+
130+
```ts title='packages/pkg-a/rstest.config.ts'
131+
import { defineConfig } from '@rstest/core';
132+
133+
export default defineConfig({
134+
projects: [
135+
{
136+
name: 'node',
137+
include: ['tests/node/**/*.{test,spec}.?(c|m)[jt]s?(x)'],
138+
},
139+
{
140+
name: 'react',
141+
include: ['tests/react/**/*.{test,spec}.?(c|m)[jt]s?(x)'],
142+
testEnvironment: 'jsdom',
143+
},
144+
],
145+
});
146+
```
147+
148+
### Inspect configuration
149+
150+
If you want to view the final effective configuration of Rstest, you can enable [debug mode](/guide/advanced/debugging#debug-mode) through the `DEBUG=rstest` environment variable. Rstest will write the final effective Rstest configuration and build configuration to the output directory.
151+
152+
## Filtering projects
153+
154+
You can filter projects through the [--project](/guide/basic/test-filter#filter-by-project-name) CLI option, or you can filter specific files under projects by directly filtering file names or file paths.
155+
156+
For more information, please refer to the [Test Filtering](/guide/basic/test-filter) chapter.

website/docs/en/guide/start/features.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Rstest is powered by Rspack, enabling high-performance builds and optimizations
1616

1717
Rstest provides multi-project testing capabilities, allowing you to run tests for multiple projects in parallel within a single Rstest process.
1818

19-
Learn more about [Projects](/config/test/projects).
19+
Learn more about [Test projects](/guide/basic/projects).
2020

2121
## In-source tests
2222

website/docs/zh/config/test/projects.mdx

Lines changed: 1 addition & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ type Projects = (string | ProjectConfig)[];
1515

1616
定义多个测试项目,可以是一个目录、配置文件或 glob 模式,也可以是一个对象。 Rstest 将会按照各个项目定义的配置运行对应的测试,所有项目的测试结果将会合并展示。
1717

18-
你可以通过 [--project](/guide/basic/test-filter#根据项目名称过滤) 选项来过滤运行特定项目。
19-
20-
如果没有 `projects` 字段,`rstest` 会将当前目录视为单个项目。
21-
2218
```ts name='rstest.config.ts'
2319
import { defineConfig } from '@rstest/core';
2420

@@ -50,69 +46,4 @@ export default defineConfig({
5046
});
5147
```
5248

53-
## 配置说明
54-
55-
需要注意的是:
56-
57-
- project 配置并不会继承全局配置,如果你的子项目间存在共享配置,可以抽取 shared 配置,并在子项目中引入
58-
- 一些全局配置,如 `reporters``pool``isolate` 等,在 project 配置中是无效的
59-
60-
```ts title='packages/pkg-a/rstest.config.ts'
61-
import { defineConfig } from '@rstest/core';
62-
import sharedConfig from '../shared/rstest.config';
63-
64-
export default defineConfig({
65-
...sharedConfig,
66-
});
67-
```
68-
69-
## 内联配置
70-
71-
Rstest 支持在 `projects` 字段中直接通过内联的方式配置 project。这允许你在单个项目下定义多个测试项目,而无需为每个测试项目创建单独的配置文件。
72-
73-
```ts name='rstest.config.ts'
74-
import { defineConfig } from '@rstest/core';
75-
76-
export default defineConfig({
77-
projects: [
78-
// inline project configs
79-
{
80-
name: 'node',
81-
include: ['tests/node/**/*.{test,spec}.{js,cjs,mjs,ts,tsx}'],
82-
},
83-
{
84-
name: 'react',
85-
include: ['tests/react/**/*.{test,spec}.{js,cjs,mjs,ts,tsx}'],
86-
testEnvironment: 'jsdom',
87-
},
88-
],
89-
});
90-
```
91-
92-
## 嵌套 Projects
93-
94-
Rstest 支持在子项目的 rstest config 中嵌套定义 projects。这允许你在子项目中定义更多的测试项目,而无需在根项目中定义所有项目。
95-
96-
这尤其适用于你的子项目需要同时支持多个测试环境或多份配置的情况。
97-
98-
### 示例
99-
100-
`packages/pkg-a` 定义 Node.js 和浏览器两个测试环境:
101-
102-
```ts title='packages/pkg-a/rstest.config.ts'
103-
import { defineConfig } from '@rstest/core';
104-
105-
export default defineConfig({
106-
projects: [
107-
{
108-
name: 'node',
109-
include: ['tests/node/**/*.{test,spec}.?(c|m)[jt]s?(x)'],
110-
},
111-
{
112-
name: 'react',
113-
include: ['tests/react/**/*.{test,spec}.?(c|m)[jt]s?(x)'],
114-
testEnvironment: 'jsdom',
115-
},
116-
],
117-
});
118-
```
49+
更多关于项目配置的信息,请参考[多项目测试](/guide/basic/projects)

website/docs/zh/guide/advanced/debugging.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 开启调试模式
44

5-
为了便于排查问题,Rstest 提供了调试模式,你可以在执行构建时添加 `DEBUG=rstest` 环境变量来开启 Rstest 的调试模式。
5+
为了便于排查问题,Rstest 提供了调试模式,你可以在执行测试时添加 `DEBUG=rstest` 环境变量来开启 Rstest 的调试模式。
66

77
```bash
88
DEBUG=rstest pnpm test
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
["cli", "configure-rstest", "test-filter", "vscode-extension", "upgrade-rstest"]
1+
[
2+
"cli",
3+
"configure-rstest",
4+
"test-filter",
5+
"projects",
6+
"vscode-extension",
7+
"upgrade-rstest"
8+
]

0 commit comments

Comments
 (0)