|
| 1 | +--- |
| 2 | +description: 'Run tests with Rstack CLI, inherit application or library settings through Rstest adapters, and configure multiple test projects.' |
| 3 | +--- |
| 4 | + |
1 | 5 | # Testing |
2 | 6 |
|
3 | 7 | Rstack CLI uses [Rstest](https://rstest.rs/) to run tests. |
@@ -28,23 +32,80 @@ Import test APIs and configuration helpers from [`rstack/test`](./api-reference# |
28 | 32 | import { defineInlineProject, expect, test } from 'rstack/test'; |
29 | 33 | ``` |
30 | 34 |
|
31 | | -## Single project |
| 35 | +## Configuration inheritance |
| 36 | + |
| 37 | +When `define.test()` does not set Rstest's [`extends`](https://rstest.rs/config/test/extends), Rstack CLI automatically converts the configuration registered by `define.app()` or `define.lib()` into an Rstest configuration. The inherited configuration is merged with the options passed directly to `define.test()`. |
| 38 | + |
| 39 | +### Inherit the application configuration |
32 | 40 |
|
33 | | -For a single test project, pass the Rstest options directly to `define.test()`: |
| 41 | +When `define.app()` is registered, Rstack CLI converts it with [`@rstest/adapter-rsbuild`](https://rstest.rs/guide/integration/rsbuild) and uses the result as the test configuration's `extends` value: |
34 | 42 |
|
35 | 43 | ```ts title="rstack.config.ts" |
36 | 44 | import { define } from 'rstack'; |
37 | 45 |
|
38 | 46 | define.app({ |
39 | | - // Shared application configuration |
| 47 | + resolve: { |
| 48 | + alias: { |
| 49 | + '@': './src', |
| 50 | + }, |
| 51 | + }, |
40 | 52 | }); |
41 | 53 |
|
42 | 54 | define.test({ |
| 55 | + // Inherits `resolve.alias` from `define.app()`. |
43 | 56 | testEnvironment: 'happy-dom', |
44 | 57 | }); |
45 | 58 | ``` |
46 | 59 |
|
47 | | -When `extends` is omitted, Rstack CLI 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. |
| 60 | +### Inherit the library configuration |
| 61 | + |
| 62 | +When `define.lib()` is registered, Rstack CLI converts it with [`@rstest/adapter-rslib`](https://rstest.rs/guide/integration/rslib): |
| 63 | + |
| 64 | +```ts title="rstack.config.ts" |
| 65 | +import { define } from 'rstack'; |
| 66 | + |
| 67 | +define.lib({ |
| 68 | + resolve: { |
| 69 | + alias: { |
| 70 | + '@': './src', |
| 71 | + }, |
| 72 | + }, |
| 73 | +}); |
| 74 | + |
| 75 | +define.test({ |
| 76 | + // Inherits `resolve.alias` from `define.lib()`. |
| 77 | + testEnvironment: 'node', |
| 78 | +}); |
| 79 | +``` |
| 80 | + |
| 81 | +:::tip |
| 82 | + |
| 83 | +When both configurations are registered, Rstack CLI gives `define.app()` precedence. |
| 84 | + |
| 85 | +::: |
| 86 | + |
| 87 | +### Disable automatic inheritance |
| 88 | + |
| 89 | +To keep the test configuration independent, set `extends` explicitly. An empty object disables automatic inheritance without extending another configuration: |
| 90 | + |
| 91 | +```ts title="rstack.config.ts" |
| 92 | +import { define } from 'rstack'; |
| 93 | + |
| 94 | +define.app({ |
| 95 | + resolve: { |
| 96 | + alias: { |
| 97 | + '@': './src', |
| 98 | + }, |
| 99 | + }, |
| 100 | +}); |
| 101 | + |
| 102 | +define.test({ |
| 103 | + extends: {}, |
| 104 | + testEnvironment: 'node', |
| 105 | +}); |
| 106 | +``` |
| 107 | + |
| 108 | +For multiple projects, setting `extends` on the root `define.test()` configuration disables automatic inheritance for every project. Setting it on an inline project disables inheritance only for that project. |
48 | 109 |
|
49 | 110 | ## Multiple projects |
50 | 111 |
|
@@ -101,25 +162,3 @@ define.test({ |
101 | 162 | ``` |
102 | 163 |
|
103 | 164 | Rstack CLI 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. |
|
0 commit comments