Skip to content

Commit 74f3869

Browse files
authored
docs: add testing guide (#67)
1 parent 93b9555 commit 74f3869

13 files changed

Lines changed: 281 additions & 154 deletions

File tree

README.md

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -113,54 +113,6 @@ For example, import Rstest APIs without adding `@rstest/core` as a direct depend
113113
import { expect, test } from 'rstack/test';
114114
```
115115

116-
## Test projects
117-
118-
`define.test()` accepts an Rstest configuration. For a single project, Rstack automatically applies the `define.app()` configuration, or falls back to `define.lib()`:
119-
120-
```ts
121-
import { define } from 'rstack';
122-
123-
define.app({
124-
// Rsbuild configuration
125-
});
126-
127-
define.test({
128-
testEnvironment: 'happy-dom',
129-
});
130-
```
131-
132-
For multiple test environments in the same app or library, use inline projects:
133-
134-
```ts
135-
import { define } from 'rstack';
136-
import { defineInlineProject } from 'rstack/test';
137-
138-
define.app({
139-
// Shared by all inline projects
140-
});
141-
142-
define.test({
143-
projects: [
144-
defineInlineProject({
145-
name: 'node',
146-
include: ['./tests/node/**/*.test.ts'],
147-
testEnvironment: 'node',
148-
}),
149-
defineInlineProject({
150-
name: 'dom',
151-
include: ['./tests/dom/**/*.test.tsx'],
152-
testEnvironment: 'happy-dom',
153-
}),
154-
],
155-
});
156-
```
157-
158-
Rstack applies the shared app or library adapter to every inline project without an explicit `extends`. String project entries are passed to Rstest unchanged and load their own configuration.
159-
160-
Run all projects with `rs test`, or select one with `rs test --project dom`.
161-
162-
See [`examples/rstest-inline-projects`](./examples/rstest-inline-projects) for a complete React SSR example.
163-
164116
## Credits
165117

166118
Rstack CLI is inspired by:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
["index", "commands", "testing"]
1+
["index", "commands"]

examples/documentation/docs/api/testing.mdx

Lines changed: 0 additions & 105 deletions
This file was deleted.

website/docs/en/guide/_meta.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@
1818
"name": "api-reference",
1919
"label": "API reference"
2020
},
21+
{
22+
"type": "section-header",
23+
"label": "Practices"
24+
},
25+
{
26+
"type": "file",
27+
"name": "testing",
28+
"label": "Testing"
29+
},
2130
{
2231
"type": "dir-section-header",
2332
"name": "cli",

website/docs/en/guide/api-reference.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ import { describe, expect, test } from 'rstack/test';
5555

5656
See the [Rstest runtime API](https://rstest.rs/api/runtime-api/) for test APIs and the [Rstest core APIs](https://rstest.rs/api/javascript-api/rstest-core) for configuration helpers.
5757

58+
> For more guidance on testing, see [Testing](./testing).
59+
5860
### `rstack/lint`
5961

6062
`rstack/lint` re-exports all public APIs from `@rslint/core`, including JavaScript and TypeScript presets and framework plugins.

website/docs/en/guide/cli/test.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
The `rs test` command uses [Rstest](https://rstest.rs/guide/basic/cli) to run tests.
44

5+
For more guidance on testing, see [Testing](../testing).
6+
57
## Usage
68

79
```bash

website/docs/en/guide/configuration.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ When `extends` is omitted, Rstack automatically connects the test configuration
144144

145145
If the root test configuration does not define `extends` and contains `projects`, Rstack applies automatic inheritance to each inline project that omits its own `extends`. A function-based application or library configuration is resolved once and shared by those projects. String project entries are passed to Rstest unchanged; they load their external configurations independently and do not inherit the current application or library configuration.
146146

147+
> For more guidance on testing, see [Testing](./testing).
148+
147149
### `define.lint()` \{#define-lint}
148150

149151
Defines the [Rslint configuration](https://rslint.rs/config/). Pass the configuration directly, or use an async function to load presets and plugins from `rstack/lint` on demand.

website/docs/en/guide/testing.mdx

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Testing
2+
3+
Rstack uses [Rstest](https://rstest.rs/) to run tests.
4+
5+
```bash
6+
rs test
7+
```
8+
9+
For command-line options and subcommands, see [`rs test`](./cli/test).
10+
11+
## Configure tests
12+
13+
Register an Rstest configuration with [`define.test()`](./configuration#define-test). It accepts the same configuration as Rstest's `defineConfig()`:
14+
15+
```ts title="rstack.config.ts"
16+
import { define } from 'rstack';
17+
18+
define.test({
19+
testEnvironment: 'node',
20+
});
21+
```
22+
23+
## Test APIs
24+
25+
Import test APIs and configuration helpers from [`rstack/test`](./api-reference#rstacktest):
26+
27+
```ts
28+
import { defineInlineProject, expect, test } from 'rstack/test';
29+
```
30+
31+
## Single project
32+
33+
For a single test project, pass the Rstest options directly to `define.test()`:
34+
35+
```ts title="rstack.config.ts"
36+
import { define } from 'rstack';
37+
38+
define.app({
39+
// Shared application configuration
40+
});
41+
42+
define.test({
43+
testEnvironment: 'happy-dom',
44+
});
45+
```
46+
47+
When `extends` is omitted, Rstack uses the Rsbuild adapter to extend the test configuration from `define.app()`. If no application configuration is defined, it uses the Rslib adapter with `define.lib()` instead. `define.app()` takes precedence when both are defined.
48+
49+
## Multiple projects
50+
51+
Set Rstest's [`projects`](https://rstest.rs/config/test/projects) option to run multiple test configurations together. Entries can be inline projects or strings that Rstest resolves as external projects.
52+
53+
### Inline projects
54+
55+
Use inline projects when different test environments should share the current application or library configuration:
56+
57+
```ts title="rstack.config.ts"
58+
import { define } from 'rstack';
59+
import { defineInlineProject } from 'rstack/test';
60+
61+
define.app({
62+
// Shared by both inline projects
63+
});
64+
65+
define.test({
66+
projects: [
67+
defineInlineProject({
68+
name: 'node',
69+
include: ['./tests/node/**/*.test.ts'],
70+
testEnvironment: 'node',
71+
}),
72+
defineInlineProject({
73+
name: 'dom',
74+
include: ['./tests/dom/**/*.test.tsx'],
75+
testEnvironment: 'happy-dom',
76+
}),
77+
],
78+
});
79+
```
80+
81+
Rstack applies the corresponding adapter to each inline project that omits `extends`. A function-based `define.app()` or `define.lib()` configuration is resolved once, then shared by those inline projects.
82+
83+
Run one project by name:
84+
85+
```bash
86+
rs test --project dom
87+
```
88+
89+
See [`examples/rstest-inline-projects`](https://github.com/rstackjs/rstack-cli/tree/main/examples/rstest-inline-projects) for a complete React SSR example using Node.js and happy-dom.
90+
91+
### External projects
92+
93+
Use a string entry for an externally configured project:
94+
95+
```ts title="rstack.config.ts"
96+
import { define } from 'rstack';
97+
98+
define.test({
99+
projects: ['./legacy/rstest.config.ts'],
100+
});
101+
```
102+
103+
Rstack passes string entries to Rstest unchanged. External projects load their own configuration and do not inherit the current `define.app()` or `define.lib()` configuration. Use external projects when each project manages its configuration independently.
104+
105+
## Customize inheritance
106+
107+
Set Rstest's [`extends`](https://rstest.rs/config/test/extends) option explicitly when a project should not inherit the current application or library configuration:
108+
109+
```ts title="rstack.config.ts"
110+
import { define } from 'rstack';
111+
import { defineInlineProject } from 'rstack/test';
112+
113+
define.test({
114+
projects: [
115+
defineInlineProject({
116+
name: 'standalone',
117+
extends: {
118+
testEnvironment: 'node',
119+
},
120+
}),
121+
],
122+
});
123+
```
124+
125+
Setting `extends` on an inline project disables automatic inheritance only for that project. Setting it on the root `define.test()` configuration disables automatic inheritance for the entire test configuration.

website/docs/zh/guide/_meta.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@
1818
"name": "api-reference",
1919
"label": "API 参考"
2020
},
21+
{
22+
"type": "section-header",
23+
"label": "实践"
24+
},
25+
{
26+
"type": "file",
27+
"name": "testing",
28+
"label": "测试"
29+
},
2130
{
2231
"type": "dir-section-header",
2332
"name": "cli",

website/docs/zh/guide/api-reference.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ import { describe, expect, test } from 'rstack/test';
5555

5656
测试 API 请参阅 [Rstest 运行时 API](https://rstest.rs/zh/api/runtime-api/),配置辅助 API 请参阅 [Rstest 核心 API](https://rstest.rs/zh/api/javascript-api/rstest-core)
5757

58+
> 如需了解更多测试相关用法,请参阅[测试](./testing)
59+
5860
### `rstack/lint`
5961

6062
`rstack/lint` 重导出 `@rslint/core` 的全部公开 API,包括 JavaScript 和 TypeScript 预设以及框架插件。

0 commit comments

Comments
 (0)