@nuxt/docs-nightly 4.0.3-29239650.0f98b653 → 4.0.4-29239870.6bc9dccf
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -54,10 +54,28 @@ We currently ship an environment for unit testing code that needs a [Nuxt](https
|
|
|
54
54
|
2. Create a `vitest.config.ts` with the following content:
|
|
55
55
|
|
|
56
56
|
```ts twoslash
|
|
57
|
-
import {
|
|
57
|
+
import { defineConfig } from 'vitest/config'
|
|
58
|
+
import { defineVitestProject } from '@nuxt/test-utils/config'
|
|
58
59
|
|
|
59
|
-
export default
|
|
60
|
-
|
|
60
|
+
export default defineConfig({
|
|
61
|
+
test: {
|
|
62
|
+
projects: [
|
|
63
|
+
{
|
|
64
|
+
test: {
|
|
65
|
+
name: 'unit',
|
|
66
|
+
include: ['test/{e2e,unit}/*.{test,spec}.ts'],
|
|
67
|
+
environment: 'node',
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
await defineVitestProject({
|
|
71
|
+
test: {
|
|
72
|
+
name: 'nuxt',
|
|
73
|
+
include: ['test/nuxt/*.{test,spec}.ts'],
|
|
74
|
+
environment: 'nuxt',
|
|
75
|
+
},
|
|
76
|
+
}),
|
|
77
|
+
],
|
|
78
|
+
},
|
|
61
79
|
})
|
|
62
80
|
```
|
|
63
81
|
|
|
@@ -72,24 +90,16 @@ It is possible to set environment variables for testing by using the `.env.test`
|
|
|
72
90
|
|
|
73
91
|
### Using a Nuxt Runtime Environment
|
|
74
92
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
You can opt in to a Nuxt environment by adding `.nuxt.` to the test file's name (for example, `my-file.nuxt.test.ts` or `my-file.nuxt.spec.ts`) or by adding `@vitest-environment nuxt` as a comment directly in the test file.
|
|
93
|
+
Using [Vitest projects](https://vitest.dev/guide/projects.html#test-projects), you have fine-grained control over which tests run in which environment:
|
|
78
94
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
import { test } from 'vitest'
|
|
95
|
+
- **Unit tests**: Place regular unit tests in `test/unit/` - these run in a Node environment for speed
|
|
96
|
+
- **Nuxt tests**: Place tests that rely on the Nuxt runtime environment in `test/nuxt/` - these will run within a Nuxt runtime environment
|
|
82
97
|
|
|
83
|
-
|
|
84
|
-
// ... test with Nuxt environment!
|
|
85
|
-
})
|
|
86
|
-
```
|
|
98
|
+
#### Alternative: Simple Setup
|
|
87
99
|
|
|
88
|
-
|
|
100
|
+
If you prefer a simpler setup and want all tests to run in the Nuxt environment, you can use the basic configuration:
|
|
89
101
|
|
|
90
102
|
```ts twoslash
|
|
91
|
-
// vitest.config.ts
|
|
92
|
-
import { fileURLToPath } from 'node:url'
|
|
93
103
|
import { defineVitestConfig } from '@nuxt/test-utils/config'
|
|
94
104
|
|
|
95
105
|
export default defineVitestConfig({
|
|
@@ -109,7 +119,7 @@ export default defineVitestConfig({
|
|
|
109
119
|
})
|
|
110
120
|
```
|
|
111
121
|
|
|
112
|
-
If you
|
|
122
|
+
If you're using the simple setup with `environment: 'nuxt'` by default, you can opt _out_ of the [Nuxt environment](https://vitest.dev/guide/environment.html#test-environment) per test file as needed.
|
|
113
123
|
|
|
114
124
|
```ts twoslash
|
|
115
125
|
// @vitest-environment node
|
|
@@ -120,6 +130,45 @@ test('my test', () => {
|
|
|
120
130
|
})
|
|
121
131
|
```
|
|
122
132
|
|
|
133
|
+
::warning
|
|
134
|
+
This approach is not recommended as it creates a hybrid environment where Nuxt Vite plugins run but the Nuxt entry and `nuxtApp` are not initialized. This can lead to hard-to-debug errors.
|
|
135
|
+
::
|
|
136
|
+
|
|
137
|
+
### Organizing Your Tests
|
|
138
|
+
|
|
139
|
+
With the project-based setup, you might organize your tests as follows:
|
|
140
|
+
|
|
141
|
+
```bash [Directory structure]
|
|
142
|
+
test/
|
|
143
|
+
├── e2e/
|
|
144
|
+
│ └── ssr.test.ts
|
|
145
|
+
├── nuxt/
|
|
146
|
+
│ ├── components.test.ts
|
|
147
|
+
│ └── composables.test.ts
|
|
148
|
+
├── unit/
|
|
149
|
+
│ └── utils.test.ts
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
You can of course opt for any test structure, but keeping the Nuxt runtime environment separated from Nuxt end-to-end tests is important for test stability.
|
|
153
|
+
|
|
154
|
+
#### Running Tests
|
|
155
|
+
|
|
156
|
+
With the project setup, you can run different test suites:
|
|
157
|
+
|
|
158
|
+
```bash
|
|
159
|
+
# Run all tests
|
|
160
|
+
npx vitest
|
|
161
|
+
|
|
162
|
+
# Run only unit tests
|
|
163
|
+
npx vitest --project unit
|
|
164
|
+
|
|
165
|
+
# Run only Nuxt tests
|
|
166
|
+
npx vitest --project nuxt
|
|
167
|
+
|
|
168
|
+
# Run tests in watch mode
|
|
169
|
+
npx vitest --watch
|
|
170
|
+
```
|
|
171
|
+
|
|
123
172
|
::warning
|
|
124
173
|
When you run your tests within the Nuxt environment, they will be running in a [`happy-dom`](https://github.com/capricorn86/happy-dom) or [`jsdom`](https://github.com/jsdom/jsdom) environment. Before your tests run, a global Nuxt app will be initialized (including, for example, running any plugins or code you've defined in your `app.vue`).
|
|
125
174
|
|
|
@@ -68,7 +68,7 @@ In Nuxt, there are three types of middleware:
|
|
|
68
68
|
- **Named route middleware**
|
|
69
69
|
- **Anonymous (or inline) route middleware**
|
|
70
70
|
|
|
71
|
-
Nuxt
|
|
71
|
+
Nuxt executes all global middleware on the initial page load (both on server and client) and then again before any client-side navigation. Named and anonymous middleware are executed only on the routes specified in the middleware property of the page(route) meta defined in the corresponding page components.
|
|
72
72
|
|
|
73
73
|
For details about each type and examples, see the [Middleware documentation](/docs/guide/directory-structure/middleware).
|
|
74
74
|
|