@nuxt/docs 0.0.0 → 3.17.0

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.
Files changed (224) hide show
  1. package/.navigation.yml +2 -0
  2. package/1.getting-started/.navigation.yml +3 -0
  3. package/1.getting-started/01.introduction.md +81 -0
  4. package/1.getting-started/02.installation.md +109 -0
  5. package/1.getting-started/03.configuration.md +226 -0
  6. package/1.getting-started/04.views.md +163 -0
  7. package/1.getting-started/05.assets.md +48 -0
  8. package/1.getting-started/06.styling.md +565 -0
  9. package/1.getting-started/07.routing.md +149 -0
  10. package/1.getting-started/08.seo-meta.md +360 -0
  11. package/1.getting-started/09.transitions.md +473 -0
  12. package/1.getting-started/10.data-fetching.md +795 -0
  13. package/1.getting-started/11.state-management.md +223 -0
  14. package/1.getting-started/12.error-handling.md +233 -0
  15. package/1.getting-started/13.server.md +94 -0
  16. package/1.getting-started/14.layers.md +92 -0
  17. package/1.getting-started/15.prerendering.md +194 -0
  18. package/1.getting-started/16.deployment.md +130 -0
  19. package/1.getting-started/17.testing.md +728 -0
  20. package/1.getting-started/18.upgrade.md +997 -0
  21. package/2.guide/.navigation.yml +2 -0
  22. package/2.guide/0.index.md +22 -0
  23. package/2.guide/1.concepts/.navigation.yml +3 -0
  24. package/2.guide/1.concepts/1.auto-imports.md +205 -0
  25. package/2.guide/1.concepts/10.nuxt-lifecycle.md +141 -0
  26. package/2.guide/1.concepts/2.vuejs-development.md +103 -0
  27. package/2.guide/1.concepts/3.rendering.md +255 -0
  28. package/2.guide/1.concepts/4.server-engine.md +62 -0
  29. package/2.guide/1.concepts/5.modules.md +48 -0
  30. package/2.guide/1.concepts/7.esm.md +299 -0
  31. package/2.guide/1.concepts/8.typescript.md +97 -0
  32. package/2.guide/1.concepts/9.code-style.md +22 -0
  33. package/2.guide/2.directory-structure/.navigation.yml +3 -0
  34. package/2.guide/2.directory-structure/0.nuxt.md +20 -0
  35. package/2.guide/2.directory-structure/0.output.md +18 -0
  36. package/2.guide/2.directory-structure/1.assets.md +16 -0
  37. package/2.guide/2.directory-structure/1.components.md +608 -0
  38. package/2.guide/2.directory-structure/1.composables.md +121 -0
  39. package/2.guide/2.directory-structure/1.content.md +64 -0
  40. package/2.guide/2.directory-structure/1.layouts.md +180 -0
  41. package/2.guide/2.directory-structure/1.middleware.md +209 -0
  42. package/2.guide/2.directory-structure/1.modules.md +66 -0
  43. package/2.guide/2.directory-structure/1.node_modules.md +12 -0
  44. package/2.guide/2.directory-structure/1.pages.md +440 -0
  45. package/2.guide/2.directory-structure/1.plugins.md +299 -0
  46. package/2.guide/2.directory-structure/1.public.md +27 -0
  47. package/2.guide/2.directory-structure/1.server.md +546 -0
  48. package/2.guide/2.directory-structure/1.shared.md +104 -0
  49. package/2.guide/2.directory-structure/1.utils.md +49 -0
  50. package/2.guide/2.directory-structure/2.env.md +75 -0
  51. package/2.guide/2.directory-structure/2.gitignore.md +37 -0
  52. package/2.guide/2.directory-structure/2.nuxtignore.md +36 -0
  53. package/2.guide/2.directory-structure/2.nuxtrc.md +50 -0
  54. package/2.guide/2.directory-structure/3.app-config.md +177 -0
  55. package/2.guide/2.directory-structure/3.app.md +72 -0
  56. package/2.guide/2.directory-structure/3.error.md +55 -0
  57. package/2.guide/2.directory-structure/3.nuxt-config.md +34 -0
  58. package/2.guide/2.directory-structure/3.package.md +32 -0
  59. package/2.guide/2.directory-structure/3.tsconfig.md +24 -0
  60. package/2.guide/3.going-further/.navigation.yml +3 -0
  61. package/2.guide/3.going-further/1.experimental-features.md +689 -0
  62. package/2.guide/3.going-further/1.features.md +103 -0
  63. package/2.guide/3.going-further/1.internals.md +81 -0
  64. package/2.guide/3.going-further/10.runtime-config.md +174 -0
  65. package/2.guide/3.going-further/11.nightly-release-channel.md +68 -0
  66. package/2.guide/3.going-further/2.hooks.md +98 -0
  67. package/2.guide/3.going-further/3.modules.md +811 -0
  68. package/2.guide/3.going-further/4.kit.md +51 -0
  69. package/2.guide/3.going-further/6.nuxt-app.md +64 -0
  70. package/2.guide/3.going-further/7.layers.md +227 -0
  71. package/2.guide/3.going-further/9.debugging.md +115 -0
  72. package/2.guide/3.going-further/index.md +4 -0
  73. package/2.guide/4.recipes/.navigation.yml +3 -0
  74. package/2.guide/4.recipes/1.custom-routing.md +181 -0
  75. package/2.guide/4.recipes/2.vite-plugin.md +65 -0
  76. package/2.guide/4.recipes/3.custom-usefetch.md +125 -0
  77. package/2.guide/4.recipes/4.sessions-and-authentication.md +203 -0
  78. package/3.api/.navigation.yml +3 -0
  79. package/3.api/1.components/.navigation.yml +3 -0
  80. package/3.api/1.components/1.client-only.md +76 -0
  81. package/3.api/1.components/1.dev-only.md +51 -0
  82. package/3.api/1.components/1.nuxt-client-fallback.md +80 -0
  83. package/3.api/1.components/10.nuxt-picture.md +27 -0
  84. package/3.api/1.components/11.teleports.md +40 -0
  85. package/3.api/1.components/12.nuxt-route-announcer.md +56 -0
  86. package/3.api/1.components/13.nuxt-time.md +173 -0
  87. package/3.api/1.components/2.nuxt-page.md +154 -0
  88. package/3.api/1.components/3.nuxt-layout.md +156 -0
  89. package/3.api/1.components/4.nuxt-link.md +322 -0
  90. package/3.api/1.components/5.nuxt-loading-indicator.md +50 -0
  91. package/3.api/1.components/6.nuxt-error-boundary.md +65 -0
  92. package/3.api/1.components/7.nuxt-welcome.md +25 -0
  93. package/3.api/1.components/8.nuxt-island.md +70 -0
  94. package/3.api/1.components/9.nuxt-img.md +43 -0
  95. package/3.api/2.composables/.navigation.yml +3 -0
  96. package/3.api/2.composables/on-prehydrate.md +60 -0
  97. package/3.api/2.composables/use-app-config.md +19 -0
  98. package/3.api/2.composables/use-async-data.md +212 -0
  99. package/3.api/2.composables/use-cookie.md +233 -0
  100. package/3.api/2.composables/use-error.md +32 -0
  101. package/3.api/2.composables/use-fetch.md +217 -0
  102. package/3.api/2.composables/use-head-safe.md +55 -0
  103. package/3.api/2.composables/use-head.md +69 -0
  104. package/3.api/2.composables/use-hydration.md +68 -0
  105. package/3.api/2.composables/use-lazy-async-data.md +47 -0
  106. package/3.api/2.composables/use-lazy-fetch.md +55 -0
  107. package/3.api/2.composables/use-loading-indicator.md +77 -0
  108. package/3.api/2.composables/use-nuxt-app.md +294 -0
  109. package/3.api/2.composables/use-nuxt-data.md +112 -0
  110. package/3.api/2.composables/use-preview-mode.md +118 -0
  111. package/3.api/2.composables/use-request-event.md +23 -0
  112. package/3.api/2.composables/use-request-fetch.md +52 -0
  113. package/3.api/2.composables/use-request-header.md +34 -0
  114. package/3.api/2.composables/use-request-headers.md +37 -0
  115. package/3.api/2.composables/use-request-url.md +41 -0
  116. package/3.api/2.composables/use-response-header.md +48 -0
  117. package/3.api/2.composables/use-route-announcer.md +60 -0
  118. package/3.api/2.composables/use-route.md +52 -0
  119. package/3.api/2.composables/use-router.md +92 -0
  120. package/3.api/2.composables/use-runtime-config.md +142 -0
  121. package/3.api/2.composables/use-runtime-hook.md +43 -0
  122. package/3.api/2.composables/use-seo-meta.md +80 -0
  123. package/3.api/2.composables/use-server-seo-meta.md +27 -0
  124. package/3.api/2.composables/use-state.md +48 -0
  125. package/3.api/3.utils/$fetch.md +98 -0
  126. package/3.api/3.utils/.navigation.yml +3 -0
  127. package/3.api/3.utils/abort-navigation.md +73 -0
  128. package/3.api/3.utils/add-route-middleware.md +88 -0
  129. package/3.api/3.utils/call-once.md +92 -0
  130. package/3.api/3.utils/clear-error.md +29 -0
  131. package/3.api/3.utils/clear-nuxt-data.md +23 -0
  132. package/3.api/3.utils/clear-nuxt-state.md +23 -0
  133. package/3.api/3.utils/create-error.md +55 -0
  134. package/3.api/3.utils/define-nuxt-component.md +53 -0
  135. package/3.api/3.utils/define-nuxt-route-middleware.md +67 -0
  136. package/3.api/3.utils/define-page-meta.md +234 -0
  137. package/3.api/3.utils/define-route-rules.md +52 -0
  138. package/3.api/3.utils/navigate-to.md +230 -0
  139. package/3.api/3.utils/on-before-route-leave.md +11 -0
  140. package/3.api/3.utils/on-before-route-update.md +11 -0
  141. package/3.api/3.utils/on-nuxt-ready.md +25 -0
  142. package/3.api/3.utils/prefetch-components.md +28 -0
  143. package/3.api/3.utils/preload-components.md +23 -0
  144. package/3.api/3.utils/preload-route-components.md +41 -0
  145. package/3.api/3.utils/prerender-routes.md +46 -0
  146. package/3.api/3.utils/refresh-cookie.md +46 -0
  147. package/3.api/3.utils/refresh-nuxt-data.md +91 -0
  148. package/3.api/3.utils/reload-nuxt-app.md +74 -0
  149. package/3.api/3.utils/set-page-layout.md +24 -0
  150. package/3.api/3.utils/set-response-status.md +36 -0
  151. package/3.api/3.utils/show-error.md +31 -0
  152. package/3.api/3.utils/update-app-config.md +27 -0
  153. package/3.api/4.commands/.navigation.yml +3 -0
  154. package/3.api/4.commands/add.md +112 -0
  155. package/3.api/4.commands/analyze.md +41 -0
  156. package/3.api/4.commands/build-module.md +42 -0
  157. package/3.api/4.commands/build.md +46 -0
  158. package/3.api/4.commands/cleanup.md +38 -0
  159. package/3.api/4.commands/dev.md +59 -0
  160. package/3.api/4.commands/devtools.md +38 -0
  161. package/3.api/4.commands/generate.md +41 -0
  162. package/3.api/4.commands/info.md +33 -0
  163. package/3.api/4.commands/init.md +46 -0
  164. package/3.api/4.commands/module.md +84 -0
  165. package/3.api/4.commands/prepare.md +36 -0
  166. package/3.api/4.commands/preview.md +43 -0
  167. package/3.api/4.commands/typecheck.md +42 -0
  168. package/3.api/4.commands/upgrade.md +37 -0
  169. package/3.api/5.kit/.navigation.yml +3 -0
  170. package/3.api/5.kit/1.modules.md +172 -0
  171. package/3.api/5.kit/10.runtime-config.md +27 -0
  172. package/3.api/5.kit/10.templates.md +283 -0
  173. package/3.api/5.kit/11.nitro.md +409 -0
  174. package/3.api/5.kit/12.resolving.md +268 -0
  175. package/3.api/5.kit/13.logging.md +65 -0
  176. package/3.api/5.kit/14.builder.md +491 -0
  177. package/3.api/5.kit/15.examples.md +41 -0
  178. package/3.api/5.kit/2.programmatic.md +125 -0
  179. package/3.api/5.kit/3.compatibility.md +230 -0
  180. package/3.api/5.kit/4.autoimports.md +144 -0
  181. package/3.api/5.kit/5.components.md +127 -0
  182. package/3.api/5.kit/6.context.md +130 -0
  183. package/3.api/5.kit/7.pages.md +295 -0
  184. package/3.api/5.kit/8.layout.md +80 -0
  185. package/3.api/5.kit/9.plugins.md +263 -0
  186. package/3.api/6.advanced/.navigation.yml +1 -0
  187. package/3.api/6.advanced/1.hooks.md +105 -0
  188. package/3.api/6.advanced/2.import-meta.md +60 -0
  189. package/3.api/6.nuxt-config.md +12 -0
  190. package/3.api/index.md +31 -0
  191. package/5.community/.navigation.yml +3 -0
  192. package/5.community/2.getting-help.md +48 -0
  193. package/5.community/3.reporting-bugs.md +50 -0
  194. package/5.community/4.contribution.md +205 -0
  195. package/5.community/5.framework-contribution.md +142 -0
  196. package/5.community/6.roadmap.md +79 -0
  197. package/5.community/7.changelog.md +92 -0
  198. package/6.bridge/.navigation.yml +3 -0
  199. package/6.bridge/1.overview.md +137 -0
  200. package/6.bridge/10.configuration.md +96 -0
  201. package/6.bridge/2.typescript.md +46 -0
  202. package/6.bridge/3.bridge-composition-api.md +132 -0
  203. package/6.bridge/4.plugins-and-middleware.md +65 -0
  204. package/6.bridge/5.nuxt3-compatible-api.md +204 -0
  205. package/6.bridge/6.meta.md +117 -0
  206. package/6.bridge/7.runtime-config.md +38 -0
  207. package/6.bridge/8.nitro.md +102 -0
  208. package/6.bridge/9.vite.md +37 -0
  209. package/7.migration/.navigation.yml +3 -0
  210. package/7.migration/1.overview.md +24 -0
  211. package/7.migration/10.bundling.md +28 -0
  212. package/7.migration/11.server.md +17 -0
  213. package/7.migration/2.configuration.md +240 -0
  214. package/7.migration/20.module-authors.md +94 -0
  215. package/7.migration/3.auto-imports.md +18 -0
  216. package/7.migration/4.meta.md +127 -0
  217. package/7.migration/5.plugins-and-middleware.md +80 -0
  218. package/7.migration/6.pages-and-layouts.md +233 -0
  219. package/7.migration/7.component-options.md +156 -0
  220. package/7.migration/8.runtime-config.md +58 -0
  221. package/LICENSE +21 -0
  222. package/README.md +11 -0
  223. package/package.json +16 -4
  224. package/dist/.gitkeep +0 -0
@@ -0,0 +1,728 @@
1
+ ---
2
+ title: Testing
3
+ description: How to test your Nuxt application.
4
+ navigation.icon: i-lucide-circle-check
5
+ ---
6
+
7
+ ::tip
8
+ If you are a module author, you can find more specific information in the [Module Author's guide](/docs/guide/going-further/modules#testing).
9
+ ::
10
+
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.
12
+
13
+ :video-accordion{title="Watch a video from Alexander Lichter about getting started with @nuxt/test-utils" videoId="yGzwk9xi9gU"}
14
+
15
+ ## Installation
16
+
17
+ In order to allow you to manage your other testing dependencies, `@nuxt/test-utils` ships with various optional peer dependencies. For example:
18
+
19
+ - you can choose between `happy-dom` and `jsdom` for a runtime Nuxt environment
20
+ - you can choose between `vitest`, `cucumber`, `jest` and `playwright` for end-to-end test runners
21
+ - `playwright-core` is only required if you wish to use the built-in browser testing utilities (and are not using `@playwright/test` as your test runner)
22
+
23
+ ::code-group{sync="pm"}
24
+ ```bash [npm]
25
+ npm i --save-dev @nuxt/test-utils vitest @vue/test-utils happy-dom playwright-core
26
+ ```
27
+ ```bash [yarn]
28
+ yarn add --dev @nuxt/test-utils vitest @vue/test-utils happy-dom playwright-core
29
+ ```
30
+ ```bash [pnpm]
31
+ pnpm add -D @nuxt/test-utils vitest @vue/test-utils happy-dom playwright-core
32
+ ```
33
+ ```bash [bun]
34
+ bun add --dev @nuxt/test-utils vitest @vue/test-utils happy-dom playwright-core
35
+ ```
36
+ ::
37
+
38
+ ## Unit Testing
39
+
40
+ We currently ship an environment for unit testing code that needs a [Nuxt](https://nuxt.com) runtime environment. It currently _only has support for `vitest`_ (although contribution to add other runtimes would be welcome).
41
+
42
+ ### Setup
43
+
44
+ 1. Add `@nuxt/test-utils/module` to your `nuxt.config` file (optional). It adds a Vitest integration to your Nuxt DevTools which supports running your unit tests in development.
45
+
46
+ ```ts twoslash
47
+ export default defineNuxtConfig({
48
+ modules: [
49
+ '@nuxt/test-utils/module'
50
+ ]
51
+ })
52
+ ```
53
+
54
+ 2. Create a `vitest.config.ts` with the following content:
55
+
56
+ ```ts twoslash
57
+ import { defineVitestConfig } from '@nuxt/test-utils/config'
58
+
59
+ export default defineVitestConfig({
60
+ // any custom Vitest config you require
61
+ })
62
+ ```
63
+
64
+ ::tip
65
+ 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.
66
+ > ie. `vitest.config.m{ts,js}`.
67
+ ::
68
+
69
+ ::tip
70
+ It is possible to set environment variables for testing by using the `.env.test` file.
71
+ ::
72
+
73
+ ### Using a Nuxt Runtime Environment
74
+
75
+ By default, `@nuxt/test-utils` will not change your default Vitest environment, so you can do fine-grained opt-in and run Nuxt tests together with other unit tests.
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.
78
+
79
+ ```ts twoslash
80
+ // @vitest-environment nuxt
81
+ import { test } from 'vitest'
82
+
83
+ test('my test', () => {
84
+ // ... test with Nuxt environment!
85
+ })
86
+ ```
87
+
88
+ You can alternatively set `environment: 'nuxt'` in your Vitest configuration to enable the Nuxt environment for **all tests**.
89
+
90
+ ```ts twoslash
91
+ // vitest.config.ts
92
+ import { fileURLToPath } from 'node:url'
93
+ import { defineVitestConfig } from '@nuxt/test-utils/config'
94
+
95
+ export default defineVitestConfig({
96
+ test: {
97
+ environment: 'nuxt',
98
+ // you can optionally set Nuxt-specific environment options
99
+ // environmentOptions: {
100
+ // nuxt: {
101
+ // rootDir: fileURLToPath(new URL('./playground', import.meta.url)),
102
+ // domEnvironment: 'happy-dom', // 'happy-dom' (default) or 'jsdom'
103
+ // overrides: {
104
+ // // other Nuxt config you want to pass
105
+ // }
106
+ // }
107
+ // }
108
+ }
109
+ })
110
+ ```
111
+
112
+ If you have set `environment: 'nuxt'` by default, you can then opt _out_ of the [default environment](https://vitest.dev/guide/environment.html#test-environment) per test file as needed.
113
+
114
+ ```ts twoslash
115
+ // @vitest-environment node
116
+ import { test } from 'vitest'
117
+
118
+ test('my test', () => {
119
+ // ... test without Nuxt environment!
120
+ })
121
+ ```
122
+
123
+ ::warning
124
+ 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
+
126
+ This means you should take particular care not to mutate the global state in your tests (or, if you need to, to reset it afterwards).
127
+ ::
128
+
129
+ ### 🎭 Built-In Mocks
130
+
131
+ `@nuxt/test-utils` provides some built-in mocks for the DOM environment.
132
+
133
+ #### `intersectionObserver`
134
+
135
+ Default `true`, creates a dummy class without any functionality for the IntersectionObserver API
136
+
137
+ #### `indexedDB`
138
+
139
+ Default `false`, uses [`fake-indexeddb`](https://github.com/dumbmatter/fakeIndexedDB) to create a functional mock of the IndexedDB API
140
+
141
+ These can be configured in the `environmentOptions` section of your `vitest.config.ts` file:
142
+
143
+ ```ts twoslash
144
+ import { defineVitestConfig } from '@nuxt/test-utils/config'
145
+
146
+ export default defineVitestConfig({
147
+ test: {
148
+ environmentOptions: {
149
+ nuxt: {
150
+ mock: {
151
+ intersectionObserver: true,
152
+ indexedDb: true,
153
+ }
154
+ }
155
+ }
156
+ }
157
+ })
158
+ ```
159
+
160
+ ### 🛠️ Helpers
161
+
162
+ `@nuxt/test-utils` provides a number of helpers to make testing Nuxt apps easier.
163
+
164
+ #### `mountSuspended`
165
+
166
+ `mountSuspended` allows you to mount any Vue component within the Nuxt environment, allowing async setup and access to injections from your Nuxt plugins.
167
+
168
+ ::note
169
+ Under the hood, `mountSuspended` wraps `mount` from `@vue/test-utils`, so you can check out [the Vue Test Utils documentation](https://test-utils.vuejs.org/guide/) for more on the options you can pass, and how to use this utility.
170
+ ::
171
+
172
+ For example:
173
+
174
+ ```ts twoslash
175
+ // @noErrors
176
+ import { it, expect } from 'vitest'
177
+ import type { Component } from 'vue'
178
+ declare module '#components' {
179
+ export const SomeComponent: Component
180
+ }
181
+ // ---cut---
182
+ // tests/components/SomeComponents.nuxt.spec.ts
183
+ import { mountSuspended } from '@nuxt/test-utils/runtime'
184
+ import { SomeComponent } from '#components'
185
+
186
+ it('can mount some component', async () => {
187
+ const component = await mountSuspended(SomeComponent)
188
+ expect(component.text()).toMatchInlineSnapshot(
189
+ '"This is an auto-imported component"'
190
+ )
191
+ })
192
+
193
+ ```
194
+
195
+ ```ts twoslash
196
+ // @noErrors
197
+ import { it, expect } from 'vitest'
198
+ // ---cut---
199
+ // tests/components/SomeComponents.nuxt.spec.ts
200
+ import { mountSuspended } from '@nuxt/test-utils/runtime'
201
+ import App from '~/app.vue'
202
+
203
+ // tests/App.nuxt.spec.ts
204
+ it('can also mount an app', async () => {
205
+ const component = await mountSuspended(App, { route: '/test' })
206
+ expect(component.html()).toMatchInlineSnapshot(`
207
+ "<div>This is an auto-imported component</div>
208
+ <div> I am a global component </div>
209
+ <div>/</div>
210
+ <a href="/test"> Test link </a>"
211
+ `)
212
+ })
213
+ ```
214
+
215
+ #### `renderSuspended`
216
+
217
+ `renderSuspended` allows you to render any Vue component within the Nuxt environment using `@testing-library/vue`, allowing async setup and access to injections from your Nuxt plugins.
218
+
219
+ This should be used together with utilities from Testing Library, e.g. `screen` and `fireEvent`. Install [@testing-library/vue](https://testing-library.com/docs/vue-testing-library/intro) in your project to use these.
220
+
221
+ Additionally, Testing Library also relies on testing globals for cleanup. You should turn these on in your [Vitest config](https://vitest.dev/config/#globals).
222
+
223
+ The passed in component will be rendered inside a `<div id="test-wrapper"></div>`.
224
+
225
+ Examples:
226
+
227
+ ```ts twoslash
228
+ // @noErrors
229
+ import { it, expect } from 'vitest'
230
+ import type { Component } from 'vue'
231
+ declare module '#components' {
232
+ export const SomeComponent: Component
233
+ }
234
+ // ---cut---
235
+ // tests/components/SomeComponents.nuxt.spec.ts
236
+ import { renderSuspended } from '@nuxt/test-utils/runtime'
237
+ import { SomeComponent } from '#components'
238
+ import { screen } from '@testing-library/vue'
239
+
240
+ it('can render some component', async () => {
241
+ await renderSuspended(SomeComponent)
242
+ expect(screen.getByText('This is an auto-imported component')).toBeDefined()
243
+ })
244
+ ```
245
+
246
+ ```ts twoslash
247
+ // @noErrors
248
+ import { it, expect } from 'vitest'
249
+ // ---cut---
250
+ // tests/App.nuxt.spec.ts
251
+ import { renderSuspended } from '@nuxt/test-utils/runtime'
252
+ import App from '~/app.vue'
253
+
254
+ it('can also render an app', async () => {
255
+ const html = await renderSuspended(App, { route: '/test' })
256
+ expect(html).toMatchInlineSnapshot(`
257
+ "<div id="test-wrapper">
258
+ <div>This is an auto-imported component</div>
259
+ <div> I am a global component </div>
260
+ <div>Index page</div><a href="/test"> Test link </a>
261
+ </div>"
262
+ `)
263
+ })
264
+ ```
265
+
266
+ #### `mockNuxtImport`
267
+
268
+ `mockNuxtImport` allows you to mock Nuxt's auto import functionality. For example, to mock `useStorage`, you can do so like this:
269
+
270
+ ```ts twoslash
271
+ import { mockNuxtImport } from '@nuxt/test-utils/runtime'
272
+
273
+ mockNuxtImport('useStorage', () => {
274
+ return () => {
275
+ return { value: 'mocked storage' }
276
+ }
277
+ })
278
+
279
+ // your tests here
280
+ ```
281
+
282
+ ::note
283
+ `mockNuxtImport` can only be used once per mocked import per test file. It is actually a macro that gets transformed to `vi.mock` and `vi.mock` is hoisted, as described [here](https://vitest.dev/api/vi.html#vi-mock).
284
+ ::
285
+
286
+ If you need to mock a Nuxt import and provide different implementations between tests, you can do it by creating and exposing your mocks using [`vi.hoisted`](https://vitest.dev/api/vi.html#vi-hoisted), and then use those mocks in `mockNuxtImport`. You then have access to the mocked imports, and can change the implementation between tests. Be careful to [restore mocks](https://vitest.dev/api/mock.html#mockrestore) before or after each test to undo mock state changes between runs.
287
+
288
+ ```ts twoslash
289
+ import { vi } from 'vitest'
290
+ import { mockNuxtImport } from '@nuxt/test-utils/runtime'
291
+
292
+ const { useStorageMock } = vi.hoisted(() => {
293
+ return {
294
+ useStorageMock: vi.fn(() => {
295
+ return { value: 'mocked storage'}
296
+ })
297
+ }
298
+ })
299
+
300
+ mockNuxtImport('useStorage', () => {
301
+ return useStorageMock
302
+ })
303
+
304
+ // Then, inside a test
305
+ useStorageMock.mockImplementation(() => {
306
+ return { value: 'something else' }
307
+ })
308
+ ```
309
+
310
+ #### `mockComponent`
311
+
312
+ `mockComponent` allows you to mock Nuxt's component.
313
+ The first argument can be the component name in PascalCase, or the relative path of the component.
314
+ The second argument is a factory function that returns the mocked component.
315
+
316
+ For example, to mock `MyComponent`, you can:
317
+
318
+ ```ts twoslash
319
+ import { mockComponent } from '@nuxt/test-utils/runtime'
320
+
321
+ mockComponent('MyComponent', {
322
+ props: {
323
+ value: String
324
+ },
325
+ setup(props) {
326
+ // ...
327
+ }
328
+ })
329
+
330
+ // relative path or alias also works
331
+ mockComponent('~/components/my-component.vue', async () => {
332
+ // or a factory function
333
+ return defineComponent({
334
+ setup(props) {
335
+ // ...
336
+ }
337
+ })
338
+ })
339
+
340
+ // or you can use SFC for redirecting to a mock component
341
+ mockComponent('MyComponent', () => import('./MockComponent.vue'))
342
+
343
+ // your tests here
344
+ ```
345
+
346
+ > **Note**: You can't reference local variables in the factory function since they are hoisted. If you need to access Vue APIs or other variables, you need to import them in your factory function.
347
+
348
+ ```ts twoslash
349
+ import { mockComponent } from '@nuxt/test-utils/runtime'
350
+
351
+ mockComponent('MyComponent', async () => {
352
+ const { ref, h } = await import('vue')
353
+
354
+ return defineComponent({
355
+ setup(props) {
356
+ const counter = ref(0)
357
+ return () => h('div', null, counter.value)
358
+ }
359
+ })
360
+ })
361
+ ```
362
+
363
+ #### `registerEndpoint`
364
+
365
+ `registerEndpoint` allows you create Nitro endpoint that returns mocked data. It can come in handy if you want to test a component that makes requests to API to display some data.
366
+
367
+ The first argument is the endpoint name (e.g. `/test/`).
368
+ The second argument is a factory function that returns the mocked data.
369
+
370
+ For example, to mock `/test/` endpoint, you can do:
371
+
372
+ ```ts twoslash
373
+ import { registerEndpoint } from '@nuxt/test-utils/runtime'
374
+
375
+ registerEndpoint('/test/', () => ({
376
+ test: 'test-field'
377
+ }))
378
+ ```
379
+
380
+ By default, your request will be made using the `GET` method. You may use another method by setting an object as the second argument instead of a function.
381
+
382
+ ```ts twoslash
383
+ import { registerEndpoint } from '@nuxt/test-utils/runtime'
384
+
385
+ registerEndpoint('/test/', {
386
+ method: 'POST',
387
+ handler: () => ({ test: 'test-field' })
388
+ })
389
+ ```
390
+
391
+ > **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.
392
+
393
+ #### Conflict with End-To-End Testing
394
+
395
+ `@nuxt/test-utils/runtime` and `@nuxt/test-utils/e2e` need to run in different testing environments and so can't be used in the same file.
396
+
397
+ If you would like to use both the end-to-end and unit testing functionality of `@nuxt/test-utils`, you can split your tests into separate files. You then either specify a test environment per-file with the special `// @vitest-environment nuxt` comment, or name your runtime unit test files with the `.nuxt.spec.ts` extension.
398
+
399
+ `app.nuxt.spec.ts`
400
+
401
+ ```ts twoslash
402
+ import { mockNuxtImport } from '@nuxt/test-utils/runtime'
403
+
404
+ mockNuxtImport('useStorage', () => {
405
+ return () => {
406
+ return { value: 'mocked storage' }
407
+ }
408
+ })
409
+
410
+ ```
411
+
412
+ `app.e2e.spec.ts`
413
+
414
+ ```ts twoslash
415
+ import { setup, $fetch } from '@nuxt/test-utils/e2e'
416
+
417
+ await setup({
418
+ setupTimeout: 10000,
419
+ })
420
+
421
+ // ...
422
+ ```
423
+
424
+ ### Using `@vue/test-utils`
425
+
426
+ If you prefer to use `@vue/test-utils` on its own for unit testing in Nuxt, and you are only testing components which do not rely on Nuxt composables, auto-imports or context, you can follow these steps to set it up.
427
+
428
+ 1. Install the needed dependencies
429
+
430
+ ::code-group{sync="pm"}
431
+ ```bash [npm]
432
+ npm i --save-dev vitest @vue/test-utils happy-dom @vitejs/plugin-vue
433
+ ```
434
+ ```bash [yarn]
435
+ yarn add --dev vitest @vue/test-utils happy-dom @vitejs/plugin-vue
436
+ ```
437
+ ```bash [pnpm]
438
+ pnpm add -D vitest @vue/test-utils happy-dom @vitejs/plugin-vue
439
+ ```
440
+ ```bash [bun]
441
+ bun add --dev vitest @vue/test-utils happy-dom @vitejs/plugin-vue
442
+ ```
443
+ ::
444
+
445
+ 2. Create a `vitest.config.ts` with the following content:
446
+
447
+ ```ts twoslash
448
+ import { defineConfig } from 'vitest/config'
449
+ import vue from '@vitejs/plugin-vue'
450
+
451
+ export default defineConfig({
452
+ plugins: [vue()],
453
+ test: {
454
+ environment: 'happy-dom',
455
+ },
456
+ });
457
+ ```
458
+
459
+ 3. Add a new command for test in your `package.json`
460
+
461
+ ```json
462
+ "scripts": {
463
+ "build": "nuxt build",
464
+ "dev": "nuxt dev",
465
+ ...
466
+ "test": "vitest"
467
+ },
468
+ ```
469
+
470
+ 4. Create a simple `<HelloWorld>` component `components/HelloWorld.vue` with the following content:
471
+
472
+ ```vue
473
+ <template>
474
+ <p>Hello world</p>
475
+ </template>
476
+ ```
477
+
478
+ 5. Create a simple unit test for this newly created component `~/components/HelloWorld.spec.ts`
479
+
480
+ ```ts twoslash
481
+ import { describe, it, expect } from 'vitest'
482
+ import { mount } from '@vue/test-utils'
483
+
484
+ import HelloWorld from './HelloWorld.vue'
485
+
486
+ describe('HelloWorld', () => {
487
+ it('component renders Hello world properly', () => {
488
+ const wrapper = mount(HelloWorld)
489
+ expect(wrapper.text()).toContain('Hello world')
490
+ })
491
+ })
492
+ ```
493
+
494
+ 6. Run vitest command
495
+
496
+ ::code-group{sync="pm"}
497
+ ```bash [npm]
498
+ npm run test
499
+ ```
500
+ ```bash [yarn]
501
+ yarn test
502
+ ```
503
+ ```bash [pnpm]
504
+ pnpm run test
505
+ ```
506
+ ```bash [bun]
507
+ bun run test
508
+ ```
509
+ ::
510
+
511
+ Congratulations, you're all set to start unit testing with `@vue/test-utils` in Nuxt! Happy testing!
512
+
513
+ ## End-To-End Testing
514
+
515
+ 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.
516
+
517
+ ### Setup
518
+
519
+ In each `describe` block where you are taking advantage of the `@nuxt/test-utils/e2e` helper methods, you will need to set up the test context before beginning.
520
+
521
+ ```ts twoslash [test/my-test.spec.ts]
522
+ import { describe, test } from 'vitest'
523
+ import { setup, $fetch } from '@nuxt/test-utils/e2e'
524
+
525
+ describe('My test', async () => {
526
+ await setup({
527
+ // test context options
528
+ })
529
+
530
+ test('my test', () => {
531
+ // ...
532
+ })
533
+ })
534
+ ```
535
+
536
+ Behind the scenes, `setup` performs a number of tasks in `beforeAll`, `beforeEach`, `afterEach` and `afterAll` to set up the Nuxt test environment correctly.
537
+
538
+ Please use the options below for the `setup` method.
539
+
540
+ #### Nuxt Config
541
+
542
+ - `rootDir`: Path to a directory with a Nuxt app to be put under test.
543
+ - Type: `string`
544
+ - Default: `'.'`
545
+ - `configFile`: Name of the configuration file.
546
+ - Type: `string`
547
+ - Default: `'nuxt.config'`
548
+
549
+ <!--
550
+ - `config`: Object with configuration overrides.
551
+ - Type: `NuxtConfig`
552
+ - Default: `{}` -->
553
+
554
+ #### Timings
555
+
556
+ - `setupTimeout`: The amount of time (in milliseconds) to allow for `setupTest` to complete its work (which could include building or generating files for a Nuxt application, depending on the options that are passed).
557
+ - Type: `number`
558
+ - Default: `60000`
559
+
560
+ #### Features
561
+
562
+ - `build`: Whether to run a separate build step.
563
+ - Type: `boolean`
564
+ - Default: `true` (`false` if `browser` or `server` is disabled, or if a `host` is provided)
565
+
566
+ - `server`: Whether to launch a server to respond to requests in the test suite.
567
+ - Type: `boolean`
568
+ - Default: `true` (`false` if a `host` is provided)
569
+
570
+ - `port`: If provided, set the launched test server port to the value.
571
+ - Type: `number | undefined`
572
+ - Default: `undefined`
573
+
574
+ - `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](#target-host-end-to-end-example).
575
+ - Type: `string`
576
+ - Default: `undefined`
577
+
578
+ - `browser`: Under the hood, Nuxt test utils uses [`playwright`](https://playwright.dev) to carry out browser testing. If this option is set, a browser will be launched and can be controlled in the subsequent test suite.
579
+ - Type: `boolean`
580
+ - Default: `false`
581
+ - `browserOptions`
582
+ - Type: `object` with the following properties
583
+ - `type`: The type of browser to launch - either `chromium`, `firefox` or `webkit`
584
+ - `launch`: `object` of options that will be passed to playwright when launching the browser. See [full API reference](https://playwright.dev/docs/api/class-browsertype#browser-type-launch).
585
+ - `runner`: Specify the runner for the test suite. Currently, [Vitest](https://vitest.dev) is recommended.
586
+ - Type: `'vitest' | 'jest' | 'cucumber'`
587
+ - Default: `'vitest'`
588
+
589
+ ##### Target `host` end-to-end example
590
+
591
+ 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.
592
+
593
+ For local development or automated deploy pipelines, testing against a separate local server can be more efficient and is typically faster than allowing the test framework to rebuild between tests.
594
+
595
+ To utilize a separate target host for end-to-end tests, simply provide the `host` property of the `setup` function with the desired URL.
596
+
597
+ ```ts twoslash
598
+ import { setup, createPage } from '@nuxt/test-utils/e2e'
599
+ import { describe, it, expect } from 'vitest'
600
+
601
+ describe('login page', async () => {
602
+ await setup({
603
+ host: 'http://localhost:8787',
604
+ })
605
+
606
+ it('displays the email and password fields', async () => {
607
+ const page = await createPage('/login')
608
+ expect(await page.getByTestId('email').isVisible()).toBe(true)
609
+ expect(await page.getByTestId('password').isVisible()).toBe(true)
610
+ })
611
+ })
612
+ ```
613
+
614
+ ### APIs
615
+
616
+ #### `$fetch(url)`
617
+
618
+ Get the HTML of a server-rendered page.
619
+
620
+ ```ts twoslash
621
+ import { $fetch } from '@nuxt/test-utils/e2e'
622
+
623
+ const html = await $fetch('/')
624
+ ```
625
+
626
+ #### `fetch(url)`
627
+
628
+ Get the response of a server-rendered page.
629
+
630
+ ```ts twoslash
631
+ import { fetch } from '@nuxt/test-utils/e2e'
632
+
633
+ const res = await fetch('/')
634
+ const { body, headers } = res
635
+ ```
636
+
637
+ #### `url(path)`
638
+
639
+ Get the full URL for a given page (including the port the test server is running on.)
640
+
641
+ ```ts twoslash
642
+ import { url } from '@nuxt/test-utils/e2e'
643
+
644
+ const pageUrl = url('/page')
645
+ // 'http://localhost:6840/page'
646
+ ```
647
+
648
+ ### Testing in a Browser
649
+
650
+ We provide built-in support using Playwright within `@nuxt/test-utils`, either programmatically or via the Playwright test runner.
651
+
652
+ #### `createPage(url)`
653
+
654
+ 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 from [in the Playwright documentation](https://playwright.dev/docs/api/class-page).
655
+
656
+ ```ts twoslash
657
+ import { createPage } from '@nuxt/test-utils/e2e'
658
+
659
+ const page = await createPage('/page')
660
+ // you can access all the Playwright APIs from the `page` variable
661
+ ```
662
+
663
+ #### Testing with Playwright Test Runner
664
+
665
+ We also provide first-class support for testing Nuxt within [the Playwright test runner](https://playwright.dev/docs/intro).
666
+
667
+ ::code-group{sync="pm"}
668
+ ```bash [npm]
669
+ npm i --save-dev @playwright/test @nuxt/test-utils
670
+ ```
671
+ ```bash [yarn]
672
+ yarn add --dev @playwright/test @nuxt/test-utils
673
+ ```
674
+ ```bash [pnpm]
675
+ pnpm add -D @playwright/test @nuxt/test-utils
676
+ ```
677
+ ```bash [bun]
678
+ bun add --dev @playwright/test @nuxt/test-utils
679
+ ```
680
+ ::
681
+
682
+ You can provide global Nuxt configuration, with the same configuration details as the `setup()` function mentioned earlier in this section.
683
+
684
+ ```ts [playwright.config.ts]
685
+ import { fileURLToPath } from 'node:url'
686
+ import { defineConfig, devices } from '@playwright/test'
687
+ import type { ConfigOptions } from '@nuxt/test-utils/playwright'
688
+
689
+ export default defineConfig<ConfigOptions>({
690
+ use: {
691
+ nuxt: {
692
+ rootDir: fileURLToPath(new URL('.', import.meta.url))
693
+ }
694
+ },
695
+ // ...
696
+ })
697
+ ```
698
+
699
+ ::read-more{title="See full example config" to="https://github.com/nuxt/test-utils/blob/main/examples/app-playwright/playwright.config.ts" target="_blank"}
700
+ ::
701
+
702
+ Your test file should then use `expect` and `test` directly from `@nuxt/test-utils/playwright`:
703
+
704
+ ```ts [tests/example.test.ts]
705
+ import { expect, test } from '@nuxt/test-utils/playwright'
706
+
707
+ test('test', async ({ page, goto }) => {
708
+ await goto('/', { waitUntil: 'hydration' })
709
+ await expect(page.getByRole('heading')).toHaveText('Welcome to Playwright!')
710
+ })
711
+ ```
712
+
713
+ You can alternatively configure your Nuxt server directly within your test file:
714
+
715
+ ```ts [tests/example.test.ts]
716
+ import { expect, test } from '@nuxt/test-utils/playwright'
717
+
718
+ test.use({
719
+ nuxt: {
720
+ rootDir: fileURLToPath(new URL('..', import.meta.url))
721
+ }
722
+ })
723
+
724
+ test('test', async ({ page, goto }) => {
725
+ await goto('/', { waitUntil: 'hydration' })
726
+ await expect(page.getByRole('heading')).toHaveText('Welcome to Playwright!')
727
+ })
728
+ ```