|
| 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. |
0 commit comments