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
[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
-
<CreateTurboCallout />
13
+
<Callout>
14
+
For information on how to set up Vitest in a monorepo, refer to [Vitest's
15
+
Workspace guide](https://vitest.dev/guide/workspace). This guide will teach
16
+
how to use Vitest with concepts from Turborepo.
17
+
</Callout>
14
18
15
19
## Setting up
16
20
@@ -29,12 +33,15 @@ Let's say we have a monorepo that looks like this:
29
33
</Folder>
30
34
</Files>
31
35
32
-
Both `apps/web` and `packages/ui` have their own test suite. Their `package.json` files include a `test` script that runs Vitest:
36
+
Both `apps/web` and `packages/ui` have their own test suites, with `vitest`[installed into the packages that use them](/repo/docs/crafting-your-repository/managing-dependencies#install-dependencies-where-theyre-used). Their `package.json` files include a `test` script that runs Vitest:
33
37
34
38
```json title="./apps/web/package.json"
35
39
{
40
+
"devDependencies": {
41
+
"vitest": "latest"
42
+
},
36
43
"scripts": {
37
-
"test": "vitest"
44
+
"test": "vitest run"
38
45
}
39
46
}
40
47
```
@@ -44,7 +51,9 @@ Inside the root `turbo.json`, create a `test` task:
44
51
```json title="./turbo.json"
45
52
{
46
53
"tasks": {
47
-
"test": {}
54
+
"test": {
55
+
"dependsOn": ["^test"]
56
+
}
48
57
}
49
58
}
50
59
```
@@ -53,16 +62,24 @@ Now, `turbo test` can parallelize and cache all of the test suites from each pac
53
62
54
63
## Running tests in watch mode
55
64
56
-
When you run your test suite normally, it completes and outputs to `stdout`. This means you can [cache it](/repo/docs/crafting-your-repository/caching) with Turborepo.
65
+
When you run your test suite in CI, it logs to `stdout` and eventually exits upon completion. This means you can [cache it](/repo/docs/crafting-your-repository/caching) with Turborepo.
57
66
58
67
But when you run your tests in a watched mode, the process never exits. This makes a watch task more like a [development task](/repo/docs/crafting-your-repository/developing-applications).
59
68
60
-
Because of this difference, we recommend specifying **two separate Turborepo tasks**: one for running your tests, and one for running them in watch mode. Inside your each `package.json` file for each workspace:
69
+
Because of this difference, we recommend specifying **two separate Turborepo tasks**: one for running your tests, and one for running them in watch mode.
70
+
71
+
<Callout>
72
+
This strategy below creates two tasks, one for local development and one for
73
+
CI. You could choose to make the `test` task for local development and create
74
+
a `test:ci` task instead.
75
+
</Callout>
76
+
77
+
For example, inside your each `package.json` file for each workspace:
61
78
62
79
```json title="./apps/web/package.json"
63
80
{
64
81
"scripts": {
65
-
"test": "vitest",
82
+
"test": "vitest run",
66
83
"test:watch": "vitest --watch"
67
84
}
68
85
}
@@ -82,7 +99,7 @@ Inside the root `turbo.json`:
82
99
}
83
100
```
84
101
85
-
You can now either run this task using [global `turbo`](/repo/docs/getting-started/installation#global-installation) as `turbo test:watch` or from a script in your root `package.json`:
102
+
You can now either run your tasks using [global `turbo`](/repo/docs/getting-started/installation#global-installation) as `turbo test:watch` or from a script in your root `package.json`:
0 commit comments