@nuxt/docs-nightly 4.5.1-29752449.30d24817 → 4.5.2-2610052030-052d82e
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.
- package/1.getting-started/01.introduction.md +8 -8
- package/1.getting-started/02.installation.md +2 -4
- package/1.getting-started/03.configuration.md +16 -16
- package/1.getting-started/04.views.md +8 -8
- package/1.getting-started/05.assets.md +10 -10
- package/1.getting-started/06.styling.md +10 -10
- package/1.getting-started/07.routing.md +15 -15
- package/1.getting-started/08.seo-meta.md +15 -15
- package/1.getting-started/09.transitions.md +10 -10
- package/1.getting-started/10.data-fetching.md +61 -47
- package/1.getting-started/11.state-management.md +11 -11
- package/1.getting-started/12.error-handling.md +20 -18
- package/1.getting-started/13.server.md +7 -7
- package/1.getting-started/14.layers.md +6 -6
- package/1.getting-started/15.prerendering.md +22 -28
- package/1.getting-started/16.deployment.md +22 -6
- package/1.getting-started/17.testing.md +200 -21
- package/1.getting-started/18.upgrade.md +78 -278
- package/2.directory-structure/0.nuxt.md +2 -2
- package/2.directory-structure/0.output.md +3 -3
- package/2.directory-structure/1.app/1.assets.md +3 -3
- package/2.directory-structure/1.app/1.components.md +26 -109
- package/2.directory-structure/1.app/1.composables.md +5 -5
- package/2.directory-structure/1.app/1.layouts.md +10 -10
- package/2.directory-structure/1.app/1.middleware.md +14 -10
- package/2.directory-structure/1.app/1.pages.md +25 -20
- package/2.directory-structure/1.app/1.plugins.md +7 -7
- package/2.directory-structure/1.app/1.utils.md +9 -5
- package/2.directory-structure/1.app/3.app-config.md +7 -7
- package/2.directory-structure/1.app/3.app.md +6 -6
- package/2.directory-structure/1.app/3.error.md +1 -1
- package/2.directory-structure/1.content.md +1 -1
- package/2.directory-structure/1.layers.md +13 -13
- package/2.directory-structure/1.modules.md +3 -3
- package/2.directory-structure/1.node_modules.md +1 -1
- package/2.directory-structure/1.server.md +33 -14
- package/2.directory-structure/1.shared.md +7 -5
- package/2.directory-structure/1.test.md +25 -0
- package/2.directory-structure/2.env.md +7 -7
- package/2.directory-structure/2.nuxtignore.md +3 -3
- package/2.directory-structure/2.nuxtrc.md +3 -3
- package/2.directory-structure/3.nuxt-config.md +2 -2
- package/2.directory-structure/3.tsconfig.md +21 -11
- package/2.directory-structure/index.md +29 -25
- package/3.guide/0.index.md +6 -6
- package/3.guide/1.concepts/1.rendering.md +76 -12
- package/3.guide/1.concepts/10.vuejs-development.md +73 -8
- package/3.guide/1.concepts/2.nuxt-lifecycle.md +18 -18
- package/3.guide/1.concepts/3.auto-imports.md +11 -11
- package/3.guide/1.concepts/4.server-engine.md +6 -6
- package/3.guide/1.concepts/5.modules.md +4 -4
- package/3.guide/1.concepts/6.server-components.md +198 -0
- package/3.guide/1.concepts/7.esm.md +5 -5
- package/3.guide/1.concepts/8.typescript.md +9 -9
- package/3.guide/2.best-practices/accessibility.md +155 -0
- package/3.guide/2.best-practices/hydration.md +5 -5
- package/3.guide/2.best-practices/performance.md +13 -9
- package/3.guide/3.ai/1.mcp.md +104 -19
- package/3.guide/4.modules/2.module-anatomy.md +5 -5
- package/3.guide/4.modules/3.recipes-basics.md +8 -8
- package/3.guide/4.modules/4.module-dependencies.md +1 -1
- package/3.guide/4.modules/5.recipes-advanced.md +6 -8
- package/3.guide/4.modules/6.testing.md +2 -2
- package/3.guide/4.modules/7.best-practices.md +2 -2
- package/3.guide/4.modules/9.server-compatibility.md +215 -0
- package/3.guide/4.modules/index.md +9 -9
- package/3.guide/5.recipes/1.custom-routing.md +15 -11
- package/3.guide/5.recipes/2.vite-plugin.md +3 -3
- package/3.guide/5.recipes/3.custom-usefetch.md +5 -5
- package/3.guide/5.recipes/4.sessions-and-authentication.md +2 -2
- package/3.guide/5.recipes/5.mostly-static-sites.md +127 -0
- package/3.guide/6.going-further/1.events.md +1 -1
- package/3.guide/6.going-further/1.experimental-features.md +254 -22
- package/3.guide/6.going-further/1.features.md +42 -6
- package/3.guide/6.going-further/1.internals.md +10 -10
- package/3.guide/6.going-further/10.runtime-config.md +28 -6
- package/3.guide/6.going-further/11.nightly-release-channel.md +1 -1
- package/3.guide/6.going-further/2.hooks.md +6 -6
- package/3.guide/6.going-further/4.kit.md +4 -4
- package/3.guide/6.going-further/5.server-imports.md +221 -0
- package/3.guide/6.going-further/6.nuxt-app.md +7 -7
- package/3.guide/6.going-further/7.layers.md +13 -13
- package/3.guide/6.going-further/8.builders.md +38 -14
- package/4.api/1.components/1.nuxt-client-fallback.md +1 -1
- package/4.api/1.components/10.nuxt-picture.md +1 -1
- package/4.api/1.components/11.teleports.md +1 -1
- package/4.api/1.components/12.nuxt-route-announcer.md +5 -3
- package/4.api/1.components/14.nuxt-announcer.md +3 -3
- package/4.api/1.components/2.nuxt-page.md +5 -5
- package/4.api/1.components/3.nuxt-layout.md +8 -8
- package/4.api/1.components/4.nuxt-link.md +6 -4
- package/4.api/1.components/5.nuxt-loading-indicator.md +3 -3
- package/4.api/1.components/6.nuxt-error-boundary.md +1 -1
- package/4.api/1.components/8.nuxt-island.md +5 -5
- package/4.api/2.composables/create-use-async-data.md +51 -8
- package/4.api/2.composables/create-use-fetch.md +126 -9
- package/4.api/2.composables/use-announcer.md +3 -3
- package/4.api/2.composables/use-app-config.md +1 -1
- package/4.api/2.composables/use-async-data.md +19 -11
- package/4.api/2.composables/use-cookie.md +22 -6
- package/4.api/2.composables/use-error.md +1 -1
- package/4.api/2.composables/use-fetch.md +14 -10
- package/4.api/2.composables/use-head-safe.md +2 -2
- package/4.api/2.composables/use-head.md +2 -2
- package/4.api/2.composables/use-hydration.md +1 -1
- package/4.api/2.composables/use-layout.md +1 -1
- package/4.api/2.composables/use-lazy-async-data.md +8 -8
- package/4.api/2.composables/use-lazy-fetch.md +11 -11
- package/4.api/2.composables/use-loading-indicator.md +2 -2
- package/4.api/2.composables/use-nuxt-app.md +13 -13
- package/4.api/2.composables/use-nuxt-data.md +1 -1
- package/4.api/2.composables/use-preview-mode.md +3 -3
- package/4.api/2.composables/use-request-event.md +29 -3
- package/4.api/2.composables/use-request-fetch.md +2 -2
- package/4.api/2.composables/use-request-header.md +1 -1
- package/4.api/2.composables/use-request-headers.md +1 -1
- package/4.api/2.composables/use-request-url.md +1 -1
- package/4.api/2.composables/use-response-header.md +2 -2
- package/4.api/2.composables/use-route-announcer.md +4 -2
- package/4.api/2.composables/use-route.md +2 -2
- package/4.api/2.composables/use-router.md +5 -5
- package/4.api/2.composables/use-runtime-config.md +5 -5
- package/4.api/2.composables/use-runtime-hook.md +1 -1
- package/4.api/2.composables/use-seo-meta.md +3 -3
- package/4.api/2.composables/use-server-seo-meta.md +5 -5
- package/4.api/2.composables/use-state.md +4 -4
- package/4.api/3.utils/$fetch.md +5 -5
- package/4.api/3.utils/abort-navigation.md +1 -1
- package/4.api/3.utils/add-route-middleware.md +2 -2
- package/4.api/3.utils/call-once.md +3 -3
- package/4.api/3.utils/clear-error.md +2 -2
- package/4.api/3.utils/clear-nuxt-data.md +1 -1
- package/4.api/3.utils/clear-nuxt-state.md +2 -2
- package/4.api/3.utils/create-error.md +19 -1
- package/4.api/3.utils/define-nuxt-plugin.md +2 -2
- package/4.api/3.utils/define-nuxt-route-middleware.md +3 -3
- package/4.api/3.utils/define-page-meta.md +12 -12
- package/4.api/3.utils/define-route-rules.md +4 -4
- package/4.api/3.utils/define-use-async-data-addon.md +57 -0
- package/4.api/3.utils/define-use-fetch-addon.md +168 -0
- package/4.api/3.utils/navigate-to.md +6 -2
- package/4.api/3.utils/prefetch-components.md +1 -1
- package/4.api/3.utils/preload-route-components.md +2 -2
- package/4.api/3.utils/prerender-routes.md +3 -3
- package/4.api/3.utils/refresh-cookie.md +1 -1
- package/4.api/3.utils/refresh-nuxt-data.md +3 -3
- package/4.api/3.utils/reload-nuxt-app.md +1 -1
- package/4.api/3.utils/set-page-layout.md +1 -1
- package/4.api/3.utils/set-response-status.md +2 -2
- package/4.api/3.utils/show-error.md +3 -3
- package/4.api/3.utils/update-app-config.md +2 -2
- package/4.api/4.commands/README.md +9 -0
- package/4.api/5.kit/1.modules.md +3 -3
- package/4.api/5.kit/10.runtime-config.md +1 -1
- package/4.api/5.kit/10.templates.md +42 -0
- package/4.api/5.kit/12.nitro.md +59 -18
- package/4.api/5.kit/14.logging.md +56 -2
- package/4.api/5.kit/15.builder.md +1 -1
- package/4.api/5.kit/18.dependencies.md +90 -0
- package/4.api/5.kit/2.programmatic.md +78 -1
- package/4.api/5.kit/3.compatibility.md +2 -2
- package/4.api/5.kit/4.autoimports.md +13 -13
- package/4.api/5.kit/5.components.md +3 -3
- package/4.api/5.kit/6.context.md +2 -2
- package/4.api/5.kit/7.pages.md +35 -4
- package/4.api/5.kit/8.layout.md +1 -1
- package/4.api/6.advanced/1.hooks.md +6 -3
- package/4.api/6.nuxt-config.md +44 -21
- package/4.api/index.md +7 -7
- package/5.community/2.getting-help.md +1 -1
- package/5.community/3.reporting-bugs.md +1 -1
- package/5.community/4.contribution.md +14 -12
- package/5.community/5.framework-contribution.md +4 -4
- package/5.community/6.roadmap.md +6 -6
- package/6.bridge/1.overview.md +12 -12
- package/6.bridge/2.typescript.md +1 -1
- package/6.bridge/3.bridge-composition-api.md +6 -6
- package/6.bridge/4.plugins-and-middleware.md +4 -4
- package/6.bridge/5.nuxt3-compatible-api.md +9 -9
- package/6.bridge/6.meta.md +5 -5
- package/6.bridge/7.runtime-config.md +1 -1
- package/6.bridge/8.nitro.md +1 -1
- package/6.bridge/9.vite.md +1 -1
- package/7.migration/1.overview.md +2 -2
- package/7.migration/10.bundling.md +1 -1
- package/7.migration/11.server.md +3 -3
- package/7.migration/2.configuration.md +6 -6
- package/7.migration/20.module-authors.md +7 -7
- package/7.migration/3.auto-imports.md +3 -3
- package/7.migration/4.meta.md +4 -4
- package/7.migration/5.plugins-and-middleware.md +3 -3
- package/7.migration/6.pages-and-layouts.md +13 -13
- package/7.migration/7.component-options.md +15 -15
- package/7.migration/8.runtime-config.md +4 -4
- package/README.md +1 -1
- package/errors/e8007.md +28 -0
- package/package.json +2 -2
- package/4.api/4.commands/.navigation.yml +0 -3
- package/4.api/4.commands/add.md +0 -112
- package/4.api/4.commands/analyze.md +0 -42
- package/4.api/4.commands/build-module.md +0 -42
- package/4.api/4.commands/build.md +0 -48
- package/4.api/4.commands/cleanup.md +0 -38
- package/4.api/4.commands/dev.md +0 -61
- package/4.api/4.commands/devtools.md +0 -38
- package/4.api/4.commands/generate.md +0 -43
- package/4.api/4.commands/info.md +0 -33
- package/4.api/4.commands/init.md +0 -50
- package/4.api/4.commands/module.md +0 -84
- package/4.api/4.commands/prepare.md +0 -41
- package/4.api/4.commands/preview.md +0 -44
- package/4.api/4.commands/test.md +0 -40
- package/4.api/4.commands/typecheck.md +0 -44
- package/4.api/4.commands/upgrade.md +0 -37
|
@@ -5,7 +5,7 @@ navigation.icon: i-lucide-circle-check
|
|
|
5
5
|
---
|
|
6
6
|
|
|
7
7
|
::tip
|
|
8
|
-
If you are a module author, you can find more specific information in the [Module Author's guide](/docs/
|
|
8
|
+
If you are a module author, you can find more specific information in the [Module Author's guide](/docs/guide/modules/testing).
|
|
9
9
|
::
|
|
10
10
|
|
|
11
11
|
Nuxt offers first-class support for end-to-end and unit testing of your Nuxt application via `@nuxt/test-utils`, a library of test utilities and configuration that currently powers the [tests we use on Nuxt itself](https://github.com/nuxt/nuxt/tree/main/test) and tests throughout the module ecosystem.
|
|
@@ -86,10 +86,14 @@ We currently ship an environment for unit testing code that needs a [Nuxt](https
|
|
|
86
86
|
})
|
|
87
87
|
```
|
|
88
88
|
|
|
89
|
+
::note
|
|
90
|
+
`defineVitestProject` is only for Nuxt-environment tests. End-to-end tests should be configured as a regular `test.environment: 'node'` project.
|
|
91
|
+
::
|
|
92
|
+
|
|
89
93
|
3. If your Nuxt-environment tests live outside `test/nuxt/`, see [TypeScript Support in Tests](#typescript-support-in-tests) to add them to the TypeScript context.
|
|
90
94
|
|
|
91
95
|
::tip
|
|
92
|
-
When importing `@nuxt/test-utils` in your vitest config,
|
|
96
|
+
When importing `@nuxt/test-utils` in your vitest config, it is necessary to have `"type": "module"` specified in your `package.json` or rename your vitest config file appropriately.
|
|
93
97
|
> i.e., `vitest.config.m{ts,js}`.
|
|
94
98
|
::
|
|
95
99
|
|
|
@@ -163,7 +167,7 @@ You can of course opt for any test structure, but keeping the Nuxt runtime envir
|
|
|
163
167
|
|
|
164
168
|
#### TypeScript Support in Tests
|
|
165
169
|
|
|
166
|
-
By default, test files in `test/nuxt/` and `tests/nuxt/` directories are included in the [Nuxt app TypeScript context](/docs/
|
|
170
|
+
By default, test files in `test/nuxt/` and `tests/nuxt/` directories are included in the [Nuxt app TypeScript context](/docs/guide/concepts/typescript#how-nuxt-uses-project-references). That means they will recognise Nuxt aliases (like `~/`, `@/`, `#imports`) and TypeScript will be aware of auto-imports that work in your Nuxt app.
|
|
167
171
|
|
|
168
172
|
::tip
|
|
169
173
|
This matches the recommended structure where only tests that need the Nuxt runtime environment are placed in these directories. Unit tests in other directories like `test/unit/` can be added manually if needed.
|
|
@@ -261,16 +265,15 @@ Under the hood, `mountSuspended` wraps `mount` from `@vue/test-utils`, so you ca
|
|
|
261
265
|
|
|
262
266
|
For example:
|
|
263
267
|
|
|
264
|
-
```ts twoslash
|
|
268
|
+
```ts twoslash [tests/components/SomeComponents.nuxt.spec.ts]
|
|
265
269
|
// @noErrors
|
|
266
|
-
import { expect, it } from 'vitest'
|
|
267
270
|
import type { Component } from 'vue'
|
|
268
271
|
|
|
269
272
|
declare module '#components' {
|
|
270
273
|
export const SomeComponent: Component
|
|
271
274
|
}
|
|
272
275
|
// ---cut---
|
|
273
|
-
|
|
276
|
+
import { expect, it } from 'vitest'
|
|
274
277
|
import { mountSuspended } from '@nuxt/test-utils/runtime'
|
|
275
278
|
import { SomeComponent } from '#components'
|
|
276
279
|
|
|
@@ -282,15 +285,11 @@ it('can mount some component', async () => {
|
|
|
282
285
|
})
|
|
283
286
|
```
|
|
284
287
|
|
|
285
|
-
```ts twoslash
|
|
286
|
-
// @noErrors
|
|
288
|
+
```ts twoslash [tests/App.nuxt.spec.ts]
|
|
287
289
|
import { expect, it } from 'vitest'
|
|
288
|
-
// ---cut---
|
|
289
|
-
// tests/components/SomeComponents.nuxt.spec.ts
|
|
290
290
|
import { mountSuspended } from '@nuxt/test-utils/runtime'
|
|
291
291
|
import App from '~/app.vue'
|
|
292
292
|
|
|
293
|
-
// tests/App.nuxt.spec.ts
|
|
294
293
|
it('can also mount an app', async () => {
|
|
295
294
|
const component = await mountSuspended(App, { route: '/test' })
|
|
296
295
|
expect(component.html()).toMatchInlineSnapshot(`
|
|
@@ -302,9 +301,34 @@ it('can also mount an app', async () => {
|
|
|
302
301
|
})
|
|
303
302
|
```
|
|
304
303
|
|
|
304
|
+
If you need to spy on component setup state, you can set the `spy` option to `true` and access the return value of the setup via `setupState`.
|
|
305
|
+
|
|
306
|
+
```ts twoslash [tests/components/SomeComponents.nuxt.spec.ts]
|
|
307
|
+
// @noErrors
|
|
308
|
+
import type { Component } from 'vue'
|
|
309
|
+
|
|
310
|
+
declare module '#components' {
|
|
311
|
+
export const SomeComponent: Component
|
|
312
|
+
}
|
|
313
|
+
// ---cut---
|
|
314
|
+
import { expect, it, vi } from 'vitest'
|
|
315
|
+
import { mountSuspended } from '@nuxt/test-utils/runtime'
|
|
316
|
+
import { SomeComponent } from '#components'
|
|
317
|
+
|
|
318
|
+
it('can spy on setup state', async () => {
|
|
319
|
+
const component = await mountSuspended(SomeComponent, { spy: true })
|
|
320
|
+
vi.mocked(component.setupState.someMethod).mockImplementation(() => 'mocked')
|
|
321
|
+
})
|
|
322
|
+
```
|
|
323
|
+
|
|
305
324
|
The options object accepts `@vue/test-utils` mount options and the following properties:
|
|
306
325
|
|
|
307
326
|
- `route`: the initial route, or `false` to skip the initial route change (default `/`).
|
|
327
|
+
- `spy`: enable spy component setup state (default `false`).
|
|
328
|
+
|
|
329
|
+
The return object includes the `@vue/test-utils` mount result and the following properties:
|
|
330
|
+
|
|
331
|
+
- `setupState`: the return value of the component setup.
|
|
308
332
|
|
|
309
333
|
#### `renderSuspended`
|
|
310
334
|
|
|
@@ -318,16 +342,15 @@ The passed in component will be rendered inside a `<div id="test-wrapper"></div>
|
|
|
318
342
|
|
|
319
343
|
Examples:
|
|
320
344
|
|
|
321
|
-
```ts twoslash
|
|
345
|
+
```ts twoslash [tests/components/SomeComponents.nuxt.spec.ts]
|
|
322
346
|
// @noErrors
|
|
323
|
-
import { expect, it } from 'vitest'
|
|
324
347
|
import type { Component } from 'vue'
|
|
325
348
|
|
|
326
349
|
declare module '#components' {
|
|
327
350
|
export const SomeComponent: Component
|
|
328
351
|
}
|
|
329
352
|
// ---cut---
|
|
330
|
-
|
|
353
|
+
import { expect, it } from 'vitest'
|
|
331
354
|
import { renderSuspended } from '@nuxt/test-utils/runtime'
|
|
332
355
|
import { SomeComponent } from '#components'
|
|
333
356
|
import { screen } from '@testing-library/vue'
|
|
@@ -338,11 +361,8 @@ it('can render some component', async () => {
|
|
|
338
361
|
})
|
|
339
362
|
```
|
|
340
363
|
|
|
341
|
-
```ts twoslash
|
|
342
|
-
// @noErrors
|
|
364
|
+
```ts twoslash [tests/App.nuxt.spec.ts]
|
|
343
365
|
import { expect, it } from 'vitest'
|
|
344
|
-
// ---cut---
|
|
345
|
-
// tests/App.nuxt.spec.ts
|
|
346
366
|
import { renderSuspended } from '@nuxt/test-utils/runtime'
|
|
347
367
|
import App from '~/app.vue'
|
|
348
368
|
|
|
@@ -361,6 +381,11 @@ it('can also render an app', async () => {
|
|
|
361
381
|
The options object accepts `@testing-library/vue` render options and the following properties:
|
|
362
382
|
|
|
363
383
|
- `route`: the initial route, or `false` to skip the initial route change (default `/`).
|
|
384
|
+
- `spy`: enable spy component setup state (default `false`). See the [`mountSuspended`](#mountsuspended) example above.
|
|
385
|
+
|
|
386
|
+
The return object includes the `@testing-library/vue` render result and the following properties:
|
|
387
|
+
|
|
388
|
+
- `setupState`: the return value of the component setup.
|
|
364
389
|
|
|
365
390
|
#### `mockNuxtImport`
|
|
366
391
|
|
|
@@ -533,7 +558,7 @@ This object accepts the following properties:
|
|
|
533
558
|
- `method`: (optional) HTTP method to match (e.g., 'GET', 'POST')
|
|
534
559
|
- `once`: (optional) if true, the handler will only be used for the first matching request and then automatically removed
|
|
535
560
|
|
|
536
|
-
> **Note**: If your requests in a component go to an external API, you can use `baseURL` and then make it empty using [Nuxt Environment Override Config](/docs/
|
|
561
|
+
> **Note**: If your requests in a component go to an external API, you can use `baseURL` and then make it empty using [Nuxt Environment Override Config](/docs/getting-started/configuration#environment-overrides) (`$test`) so all your requests will go to Nitro server.
|
|
537
562
|
|
|
538
563
|
#### Conflict with End-To-End Testing
|
|
539
564
|
|
|
@@ -654,6 +679,132 @@ If you prefer to use `@vue/test-utils` on its own for unit testing in Nuxt, and
|
|
|
654
679
|
|
|
655
680
|
Congratulations, you're all set to start unit testing with `@vue/test-utils` in Nuxt! Happy testing!
|
|
656
681
|
|
|
682
|
+
### Vitest Browser Mode
|
|
683
|
+
|
|
684
|
+
`@nuxt/test-utils` provides helpers for testing in [Vitest Browser Mode](https://vitest.dev/guide/browser/) via `@nuxt/test-utils/browser`.
|
|
685
|
+
|
|
686
|
+
#### Setup
|
|
687
|
+
|
|
688
|
+
1. Install Vitest browser packages and a browser provider:
|
|
689
|
+
|
|
690
|
+
::code-group{sync="pm"}
|
|
691
|
+
```bash [npm]
|
|
692
|
+
npm i --save-dev @vitest/browser-playwright
|
|
693
|
+
```
|
|
694
|
+
```bash [yarn]
|
|
695
|
+
yarn add --dev @vitest/browser-playwright
|
|
696
|
+
```
|
|
697
|
+
```bash [pnpm]
|
|
698
|
+
pnpm add -D @vitest/browser-playwright
|
|
699
|
+
```
|
|
700
|
+
```bash [bun]
|
|
701
|
+
bun add --dev @vitest/browser-playwright
|
|
702
|
+
```
|
|
703
|
+
::
|
|
704
|
+
|
|
705
|
+
2. Configure browser mode in your `vitest.config.ts`:
|
|
706
|
+
|
|
707
|
+
```ts twoslash
|
|
708
|
+
// @errors: 2307
|
|
709
|
+
// ---cut---
|
|
710
|
+
import { defineConfig } from 'vitest/config'
|
|
711
|
+
import { defineVitestProject } from '@nuxt/test-utils/config'
|
|
712
|
+
import { playwright } from '@vitest/browser-playwright'
|
|
713
|
+
|
|
714
|
+
export default defineConfig({
|
|
715
|
+
test: {
|
|
716
|
+
projects: [
|
|
717
|
+
await defineVitestProject({
|
|
718
|
+
test: {
|
|
719
|
+
name: 'browser',
|
|
720
|
+
include: ['test/browser/**/*.{test,spec}.ts'],
|
|
721
|
+
browser: {
|
|
722
|
+
enabled: true,
|
|
723
|
+
provider: playwright(),
|
|
724
|
+
instances: [{ browser: 'chromium' }],
|
|
725
|
+
},
|
|
726
|
+
// If you want to enable `page.render` and automatic cleanup, add this setup file
|
|
727
|
+
setupFiles: ['@nuxt/test-utils/browser'],
|
|
728
|
+
},
|
|
729
|
+
}),
|
|
730
|
+
],
|
|
731
|
+
},
|
|
732
|
+
})
|
|
733
|
+
```
|
|
734
|
+
|
|
735
|
+
::note
|
|
736
|
+
When adding `@nuxt/test-utils/browser` to `setupFiles`, if TypeScript does not automatically recognize the types for `page.render`, you can add `@nuxt/test-utils/browser` via `typescript.tsConfig.compilerOptions.types` in your `nuxt.config.ts`.
|
|
737
|
+
::
|
|
738
|
+
|
|
739
|
+
::note
|
|
740
|
+
If your Vitest browser mode tests live outside `test/nuxt/` (such as in `test/browser/`), see [Adding other test directories](#adding-other-test-directories) to add them to the TypeScript context.
|
|
741
|
+
::
|
|
742
|
+
|
|
743
|
+
#### Usage
|
|
744
|
+
|
|
745
|
+
You can use `page.render` from `vitest/browser`:
|
|
746
|
+
|
|
747
|
+
```ts twoslash [test/browser/components/MyCounter.nuxt.spec.ts]
|
|
748
|
+
// @noErrors
|
|
749
|
+
import type { Component } from 'vue'
|
|
750
|
+
|
|
751
|
+
declare module '#components' {
|
|
752
|
+
export const MyCounter: Component
|
|
753
|
+
}
|
|
754
|
+
// ---cut---
|
|
755
|
+
import { expect, it } from 'vitest'
|
|
756
|
+
import { page } from 'vitest/browser'
|
|
757
|
+
// If you added the setup file, the following import is not needed
|
|
758
|
+
import '@nuxt/test-utils/browser'
|
|
759
|
+
|
|
760
|
+
import { MyCounter } from '#components'
|
|
761
|
+
|
|
762
|
+
it('counter button increments the count', async () => {
|
|
763
|
+
const screen = await page.render(MyCounter)
|
|
764
|
+
await screen.getByRole('button', { name: 'Increment' }).click()
|
|
765
|
+
await expect.element(screen.getByText('Count: 1')).toBeVisible()
|
|
766
|
+
})
|
|
767
|
+
```
|
|
768
|
+
|
|
769
|
+
You can also import `render` directly from `@nuxt/test-utils/browser`:
|
|
770
|
+
|
|
771
|
+
```ts twoslash
|
|
772
|
+
// @noErrors
|
|
773
|
+
import type { Component } from 'vue'
|
|
774
|
+
|
|
775
|
+
declare module '#components' {
|
|
776
|
+
export const MyCounter: Component
|
|
777
|
+
}
|
|
778
|
+
// ---cut---
|
|
779
|
+
import { expect, it } from 'vitest'
|
|
780
|
+
import { render } from '@nuxt/test-utils/browser'
|
|
781
|
+
import { MyCounter } from '#components'
|
|
782
|
+
|
|
783
|
+
it('can render using the render helper', async () => {
|
|
784
|
+
const screen = await render(MyCounter)
|
|
785
|
+
await screen.getByRole('button', { name: 'Increment' }).click()
|
|
786
|
+
await expect.element(screen.getByText('Count: 1')).toBeVisible()
|
|
787
|
+
})
|
|
788
|
+
```
|
|
789
|
+
|
|
790
|
+
The options object accepts `@vue/test-utils` mount options (use `container` instead of `attachTo`) and the following properties:
|
|
791
|
+
|
|
792
|
+
- `route`: the initial route, or `false` to skip the initial route change (default `/`).
|
|
793
|
+
- `spy`: enable spy component setup state (default `false`). See the [`mountSuspended`](#mountsuspended) example above.
|
|
794
|
+
- `container`: custom `HTMLElement` container to render into (use this option instead of `@vue/test-utils` `attachTo`).
|
|
795
|
+
- `baseElement`: custom base `HTMLElement` (default `document.body`).
|
|
796
|
+
|
|
797
|
+
The return object includes the following properties:
|
|
798
|
+
|
|
799
|
+
- `container`: the container `HTMLElement` the component is rendered into.
|
|
800
|
+
- `baseElement`: the base `HTMLElement` (default `document.body`).
|
|
801
|
+
- `locator`: root element `Locator`.
|
|
802
|
+
- `setupState`: the return value of the component setup, mocked when the `spy` option is enabled.
|
|
803
|
+
- `debug()`: print formatted DOM to console.
|
|
804
|
+
- `unmount()`: unmount the component. Also records a `nuxt.unmount` trace mark.
|
|
805
|
+
- `emitted()`: get emitted events.
|
|
806
|
+
- `rerender(props)`: re-render the component with new props. Also records a `nuxt.rerender` trace mark.
|
|
807
|
+
|
|
657
808
|
## End-To-End Testing
|
|
658
809
|
|
|
659
810
|
For end-to-end testing, we support [Vitest](https://github.com/vitest-dev/vitest), [Jest](https://jestjs.io), [Cucumber](https://cucumber.io/) and [Playwright](https://playwright.dev/) as test runners.
|
|
@@ -719,7 +870,7 @@ Please use the options below for the `setup` method.
|
|
|
719
870
|
- Type: `number | undefined`
|
|
720
871
|
- Default: `undefined`
|
|
721
872
|
|
|
722
|
-
- `host`: If provided, a URL to use as the test target instead of building and running a new server. Useful for running "real" end-to-end tests against a deployed version of your application, or against an already running local server (which may provide a significant reduction in test execution timings). See the [target host end-to-end example below](/docs/
|
|
873
|
+
- `host`: If provided, a URL to use as the test target instead of building and running a new server. Useful for running "real" end-to-end tests against a deployed version of your application, or against an already running local server (which may provide a significant reduction in test execution timings). See the [target host end-to-end example below](/docs/getting-started/testing#target-host-end-to-end-example).
|
|
723
874
|
- Type: `string`
|
|
724
875
|
- Default: `undefined`
|
|
725
876
|
|
|
@@ -734,6 +885,13 @@ Please use the options below for the `setup` method.
|
|
|
734
885
|
- Type: `'vitest' | 'jest' | 'cucumber'`
|
|
735
886
|
- Default: `'vitest'`
|
|
736
887
|
|
|
888
|
+
- `logLevel`: Overrides the consola log level for the server subprocess. (use NUXT_TEST_LOG_LEVEL environment variable to override)
|
|
889
|
+
- Type: `number`
|
|
890
|
+
- Default: `1`
|
|
891
|
+
- `captureServerLogs`: Whether to capture server process output instead of inheriting stdio. When `true` (default), server stdout/stderr is suppressed from the console and accessible via `getServerLogs()`. Set to `false` to restore the old inherit-stdio behaviour (useful when debugging a test locally).
|
|
892
|
+
- Type: `boolean`
|
|
893
|
+
- Default: `true`
|
|
894
|
+
|
|
737
895
|
##### Target `host` end-to-end example
|
|
738
896
|
|
|
739
897
|
A common use-case for end-to-end testing is running the tests against a deployed application running in the same environment typically used for Production.
|
|
@@ -793,13 +951,34 @@ const pageUrl = url('/page')
|
|
|
793
951
|
// 'http://localhost:6840/page'
|
|
794
952
|
```
|
|
795
953
|
|
|
954
|
+
#### `getServerLogs()`
|
|
955
|
+
|
|
956
|
+
Returns the lines captured from the server subprocess's stdout/stderr since the last `startServer()` call (or `clearServerLogs()`). Only populated when `captureServerLogs` is `true` (the default).
|
|
957
|
+
|
|
958
|
+
```ts twoslash
|
|
959
|
+
import { expect, it, vi } from 'vitest'
|
|
960
|
+
import { $fetch, clearServerLogs, getServerLogs } from '@nuxt/test-utils/e2e'
|
|
961
|
+
|
|
962
|
+
it('captures console.log output from a server route', async () => {
|
|
963
|
+
clearServerLogs()
|
|
964
|
+
await $fetch('/api/log-test')
|
|
965
|
+
await vi.waitFor(() => {
|
|
966
|
+
expect(getServerLogs().some(line => line.includes('[test] server-log-marker'))).toBe(true)
|
|
967
|
+
})
|
|
968
|
+
})
|
|
969
|
+
```
|
|
970
|
+
|
|
971
|
+
#### `clearServerLogs()`
|
|
972
|
+
|
|
973
|
+
Clears the captured server log lines. Useful between requests when you want to assert only on the logs produced by a specific operation.
|
|
974
|
+
|
|
796
975
|
### Testing in a Browser
|
|
797
976
|
|
|
798
977
|
We provide built-in support using Playwright within `@nuxt/test-utils`, either programmatically or via the Playwright test runner.
|
|
799
978
|
|
|
800
979
|
#### `createPage(url)`
|
|
801
980
|
|
|
802
|
-
Within `vitest`, `jest` or `cucumber`, you can create a configured Playwright browser instance with `createPage`, and (optionally) point it at a path from the running server. You can find out more about the API methods available
|
|
981
|
+
Within `vitest`, `jest` or `cucumber`, you can create a configured Playwright browser instance with `createPage`, and (optionally) point it at a path from the running server. You can find out more about the API methods available in the [Playwright documentation](https://playwright.dev/docs/api/class-page).
|
|
803
982
|
|
|
804
983
|
```ts twoslash
|
|
805
984
|
import { createPage } from '@nuxt/test-utils/e2e'
|