You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
### Description
Updates the Vitest documentation to address GitHub Issue #10548. This
PR:
* Replaces deprecated Vitest "workspaces" with "projects" terminology
and configuration.
* Introduces a new `@repo/vitest-config` package for shared Vitest
configuration, eliminating relative path traversals and promoting
monorepo best practices.
* Updates example code snippets and `turbo.json` to reflect the new
package-based approach for improved cacheability and maintainability.
### Testing Instructions
Review the updated `docs/site/content/docs/guides/tools/vitest.mdx`
file.
* Verify that "workspaces" references are correctly replaced with
"projects".
* Ensure the new shared configuration package (`@repo/vitest-config`) is
clearly explained and demonstrated.
* Check the code snippets for accuracy and adherence to the recommended
monorepo structure.
---------
Co-authored-by: Cursor Agent <[email protected]>
[Vitest](https://vitest.dev/) is a test runner from the Vite ecosystem. Integrating it with Turborepo will lead to enormous speed-ups.
12
12
13
-
[The Vitest documentation](https://vitest.dev/guide/workspace) shows how to create a "Vitest Workspace" that runs all tests in the monorepo from one root command, enabling behavior like merged coverage reports out-of-the-box. This feature doesn't follow modern best practices for monorepos, since its designed for compatibility with Jest (whose Workspace feature was built before [package manager Workspaces](/docs/crafting-your-repository/structuring-a-repository)).
13
+
[The Vitest documentation](https://vitest.dev/guide/workspace) shows how to create a "Vitest Projects" configuration that runs all tests in the monorepo from one root command, enabling behavior like merged coverage reports out-of-the-box. This feature doesn't follow modern best practices for monorepos, since its designed for compatibility with Jest (whose Workspace feature was built before [package manager Workspaces](/docs/crafting-your-repository/structuring-a-repository)).
14
+
15
+
<Callouttype="warning">
16
+
Vitest has deprecated workspaces in favor of projects. When using projects, individual project vitest configs can't extend the root config anymore since they would inherit the projects configuration. Instead, a separate shared file like `vitest.shared.ts` is needed.
17
+
</Callout>
14
18
15
19
Because of this you have two options, each with their own tradeoffs:
16
20
17
21
-[Leveraging Turborepo for caching](#leveraging-turborepo-for-caching)
[Vitest's Workspace feature](#using-vitests-workspace-feature) creates an out-of-the-box coverage report that merges all of your packages' tests coverage reports. Following the Turborepo strategy, though, you'll have to merge the coverage reports yourself.
150
+
[Vitest's Projects feature](#using-vitests-projects-feature) creates an out-of-the-box coverage report that merges all of your packages' tests coverage reports. Following the Turborepo strategy, though, you'll have to merge the coverage reports yourself.
147
151
148
152
<Callouttype="info">
149
153
The [`with-vitest`
@@ -189,13 +193,13 @@ With this in place, run `turbo test && turbo report` to create a merged coverage
189
193
started with it quickly using `npx create-turbo@latest --example with-vitest`.
190
194
</Callout>
191
195
192
-
### Using Vitest's Workspace feature
196
+
### Using Vitest's Projects feature
193
197
194
-
The Vitest Workspace feature doesn't follow the same model as a [package manager Workspace](/docs/crafting-your-repository/structuring-a-repository). Instead, it uses a root script that then reaches out into each package in the repository to handle the tests in that respective package.
198
+
The Vitest Projects feature doesn't follow the same model as a [package manager Workspace](/docs/crafting-your-repository/structuring-a-repository). Instead, it uses a root script that then reaches out into each package in the repository to handle the tests in that respective package.
195
199
196
200
In this model, there aren't package boundaries, from a modern JavaScript ecosystem perspective. This means you can't rely on Turborepo's caching, since Turborepo leans on those package boundaries.
197
201
198
-
Because of this, you'll need to use [Root Tasks](/docs/crafting-your-repository/configuring-tasks#registering-root-tasks) if you want to run the tests using Turborepo. Once you've configured [a Vitest Workspace](https://vitest.dev/guide/workspace), create the Root Tasks for Turborepo:
202
+
Because of this, you'll need to use [Root Tasks](/docs/crafting-your-repository/configuring-tasks#registering-root-tasks) if you want to run the tests using Turborepo. Once you've configured [a Vitest Projects setup](https://vitest.dev/guide/workspace), create the Root Tasks for Turborepo:
199
203
200
204
```json title="./turbo.json"
201
205
{
@@ -215,21 +219,120 @@ Because of this, you'll need to use [Root Tasks](/docs/crafting-your-repository/
215
219
216
220
### Using a hybrid approach
217
221
218
-
You can combine the benefits of both approaches by implementing a hybrid solution.This approach unifies local development using Vitest's Workspace approach while preserving Turborepo's caching in CI. This comes at the tradeoff of slightly more configuration and a mixed task running model in the repository.
222
+
You can combine the benefits of both approaches by implementing a hybrid solution. This approach unifies local development using Vitest's Projects feature while preserving Turborepo's caching in CI. This comes at the tradeoff of slightly more configuration and a mixed task running model in the repository.
223
+
224
+
First, create a shared configuration package since individual projects can't extend the root config when using projects. Create a new package for your shared Vitest configuration:
@@ -239,10 +342,10 @@ While your root `package.json` includes scripts for running tests globally:
239
342
```json title="./package.json"
240
343
{
241
344
"scripts": {
242
-
"test:workspace": "turbo run test",
243
-
"test:workspace:watch": "vitest --watch"
345
+
"test:projects": "turbo run test",
346
+
"test:projects:watch": "vitest --watch"
244
347
}
245
348
}
246
349
```
247
350
248
-
This configuration allows developers to run `pnpm test:workspace:watch` at the root for a seamless local development experience, while CI continues to use `turbo run test` to leverage package-level caching. **You'll still need to handle merged coverage reports manually as described in the previous section**.
351
+
This configuration allows developers to run `pnpm test:projects:watch` at the root for a seamless local development experience using Vitest projects, while CI continues to use `turbo run test` to leverage package-level caching. **You'll still need to handle merged coverage reports manually as described in the previous section**.
Copy file name to clipboardExpand all lines: examples/with-vitest/README.md
+28-3Lines changed: 28 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,10 +6,35 @@ This Turborepo starter is maintained by the Turborepo core team.
6
6
7
7
This example is based on the `basic` example (`npx create-turbo@latest`) to demonstrate how to use Vitest and get the most out of Turborepo's caching.
8
8
9
-
For this reason, the only commands in the root package.json are `turbo run test` and `turbo run view-report`.
9
+
This example demonstrates two approaches to Vitest configuration:
10
10
11
-
`turbo run test`: Runs the test in each package using Turborepo.
12
-
`turbo run view-report`: Collects coverage from each package and shows it in a merged report.
11
+
1.**Package-level caching (Recommended)**: Each package has its own Vitest configuration that imports shared settings from `@repo/vitest-config`. This approach leverages Turborepo's caching effectively.
12
+
13
+
2.**Vitest Projects**: A root `vitest.config.ts` uses Vitest's projects feature for unified test running during development.
14
+
15
+
## Getting Started
16
+
17
+
First, install dependencies and build the shared configuration package:
18
+
19
+
```bash
20
+
pnpm install
21
+
pnpm build --filter=@repo/vitest-config
22
+
```
23
+
24
+
## Available Commands
25
+
26
+
-`pnpm test`: Runs tests in each package using Turborepo (leverages caching)
27
+
-`pnpm test:projects`: Same as above, explicitly named for the package-level approach
28
+
-`pnpm test:projects:watch`: Runs tests using Vitest's projects feature in watch mode
29
+
-`pnpm view-report`: Collects coverage from each package and shows it in a merged report
30
+
31
+
## Configuration Structure
32
+
33
+
The example uses a shared `@repo/vitest-config` package that exports:
34
+
35
+
-`sharedConfig`: Base configuration with coverage settings
36
+
-`baseConfig`: For Node.js packages (like `math`)
37
+
-`uiConfig`: For packages requiring jsdom environment (like `web`, `docs`)
0 commit comments