@nuxt/docs-nightly 4.4.0-29546548.717c5fbf → 4.4.0-29546812.157e42be
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.
|
@@ -300,6 +300,10 @@ it('can also mount an app', async () => {
|
|
|
300
300
|
})
|
|
301
301
|
```
|
|
302
302
|
|
|
303
|
+
The options object accepts `@vue/test-utils` mount options and the following properties:
|
|
304
|
+
|
|
305
|
+
- `route`: the initial route, or `false` to skip the initial route change (default `/`).
|
|
306
|
+
|
|
303
307
|
#### `renderSuspended`
|
|
304
308
|
|
|
305
309
|
`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.
|
|
@@ -352,6 +356,10 @@ it('can also render an app', async () => {
|
|
|
352
356
|
})
|
|
353
357
|
```
|
|
354
358
|
|
|
359
|
+
The options object accepts `@testing-library/vue` render options and the following properties:
|
|
360
|
+
|
|
361
|
+
- `route`: the initial route, or `false` to skip the initial route change (default `/`).
|
|
362
|
+
|
|
355
363
|
#### `mockNuxtImport`
|
|
356
364
|
|
|
357
365
|
`mockNuxtImport` allows you to mock Nuxt's auto import functionality. For example, to mock `useStorage`, you can do so like this:
|
|
@@ -368,6 +376,27 @@ mockNuxtImport('useStorage', () => {
|
|
|
368
376
|
// your tests here
|
|
369
377
|
```
|
|
370
378
|
|
|
379
|
+
You can explicitly type the mock for type safety, and use the original implementation passed to the factory function when mocking complex functionality.
|
|
380
|
+
|
|
381
|
+
```ts twoslash
|
|
382
|
+
import { mockNuxtImport } from '@nuxt/test-utils/runtime'
|
|
383
|
+
|
|
384
|
+
mockNuxtImport<typeof useStorage>('useStorage', (original) => {
|
|
385
|
+
return (...args) => {
|
|
386
|
+
return { ...original(...args), value: 'mocked storage' }
|
|
387
|
+
}
|
|
388
|
+
})
|
|
389
|
+
|
|
390
|
+
// or specify the target to mock
|
|
391
|
+
mockNuxtImport(useStorage, (original) => {
|
|
392
|
+
return (...args) => {
|
|
393
|
+
return { ...original(...args), value: 'mocked storage' }
|
|
394
|
+
}
|
|
395
|
+
})
|
|
396
|
+
|
|
397
|
+
// your tests here
|
|
398
|
+
```
|
|
399
|
+
|
|
371
400
|
::note
|
|
372
401
|
`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 [in the Vitest docs](https://vitest.dev/api/vi#vi-mock).
|
|
373
402
|
::
|
|
@@ -396,6 +425,25 @@ useStorageMock.mockImplementation(() => {
|
|
|
396
425
|
})
|
|
397
426
|
```
|
|
398
427
|
|
|
428
|
+
If you need to mock behavior only inside a test, you can also use the following approach.
|
|
429
|
+
|
|
430
|
+
```ts twoslash
|
|
431
|
+
import { beforeEach, vi } from 'vitest'
|
|
432
|
+
import { mockNuxtImport } from '@nuxt/test-utils/runtime'
|
|
433
|
+
|
|
434
|
+
mockNuxtImport(useRoute, original => vi.fn(original))
|
|
435
|
+
|
|
436
|
+
beforeEach(() => {
|
|
437
|
+
vi.resetAllMocks()
|
|
438
|
+
})
|
|
439
|
+
|
|
440
|
+
// Then, inside a test
|
|
441
|
+
const useRouteOriginal = vi.mocked(useRoute).getMockImplementation()!
|
|
442
|
+
vi.mocked(useRoute).mockImplementation(
|
|
443
|
+
(...args) => ({ ...useRouteOriginal(...args), path: '/mocked' }),
|
|
444
|
+
)
|
|
445
|
+
```
|
|
446
|
+
|
|
399
447
|
#### `mockComponent`
|
|
400
448
|
|
|
401
449
|
`mockComponent` allows you to mock Nuxt's component.
|
|
@@ -477,6 +525,12 @@ registerEndpoint('/test/', {
|
|
|
477
525
|
})
|
|
478
526
|
```
|
|
479
527
|
|
|
528
|
+
This object accepts the following properties:
|
|
529
|
+
|
|
530
|
+
- `handler`: the event handler function
|
|
531
|
+
- `method`: (optional) HTTP method to match (e.g., 'GET', 'POST')
|
|
532
|
+
- `once`: (optional) if true, the handler will only be used for the first matching request and then automatically removed
|
|
533
|
+
|
|
480
534
|
> **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/4.x/getting-started/configuration#environment-overrides) (`$test`) so all your requests will go to Nitro server.
|
|
481
535
|
|
|
482
536
|
#### Conflict with End-To-End Testing
|
|
@@ -61,6 +61,7 @@ When you set your `future.compatibilityVersion` to `5`, defaults throughout your
|
|
|
61
61
|
- **Vite Environment API**: Automatically enables the new [Vite Environment API](/docs/4.x/getting-started/upgrade#migration-to-vite-environment-api) for improved build configuration
|
|
62
62
|
- **Normalized Page Names**: Page component names will [match their route names](/docs/4.x/getting-started/upgrade#normalized-page-component-names) for consistent `<KeepAlive>` behavior
|
|
63
63
|
- **`clearNuxtState` resets to defaults**: `clearNuxtState` will [reset state to its initial value](/docs/4.x/getting-started/upgrade#respect-defaults-when-clearing-usestate) instead of setting it to `undefined`
|
|
64
|
+
- **Non-async `callHook`**: [`callHook` may return `void`](/docs/4.x/getting-started/upgrade#non-async-callhook) instead of always returning a `Promise`
|
|
64
65
|
- Other Nuxt 5 improvements and changes as they become available
|
|
65
66
|
|
|
66
67
|
::note
|
|
@@ -180,6 +181,52 @@ addVitePlugin(() => ({
|
|
|
180
181
|
Learn more about Vite's Environment API
|
|
181
182
|
::
|
|
182
183
|
|
|
184
|
+
### Non-Async `callHook`
|
|
185
|
+
|
|
186
|
+
🚦 **Impact Level**: Minimal
|
|
187
|
+
|
|
188
|
+
#### What Changed
|
|
189
|
+
|
|
190
|
+
With the upgrade to [hookable v6](https://github.com/unjs/hookable), `callHook` may now return `void` instead of always returning `Promise<void>`. This is a significant performance improvement that avoids unnecessary `Promise` allocations when there are no registered hooks or all hooks are synchronous.
|
|
191
|
+
|
|
192
|
+
By default (with `compatibilityVersion: 4`), Nuxt wraps `callHook` with `Promise.resolve()` so that existing `.then()` and `.catch()` chaining continues to work. With `compatibilityVersion: 5`, this wrapper is removed.
|
|
193
|
+
|
|
194
|
+
::tip
|
|
195
|
+
This affects both build-time Nuxt hooks (used by Nuxt modules) and runtime Nuxt hooks (which you might use in your application code).
|
|
196
|
+
::
|
|
197
|
+
|
|
198
|
+
#### Reasons for Change
|
|
199
|
+
|
|
200
|
+
Hookable v6's `callHook` is 20-40x faster because it avoids creating a `Promise` when one is not needed. This benefits applications with many hook call sites.
|
|
201
|
+
|
|
202
|
+
#### Migration Steps
|
|
203
|
+
|
|
204
|
+
If you or your modules use `callHook` with `.then()` or `.catch()` chaining, switch to `await`:
|
|
205
|
+
|
|
206
|
+
```diff
|
|
207
|
+
- nuxtApp.callHook('my:hook', data).then(() => { ... })
|
|
208
|
+
+ await nuxtApp.callHook('my:hook', data)
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
```diff
|
|
212
|
+
- nuxtApp.hooks.callHook('my:hook', data).catch(err => { ... })
|
|
213
|
+
+ try { await nuxtApp.hooks.callHook('my:hook', data) } catch (err) { ... }
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
::tip
|
|
217
|
+
You can test this feature early by setting `future.compatibilityVersion: 5` (see [Testing Nuxt 5](/docs/4.x/getting-started/upgrade#testing-nuxt-5)) or by enabling it explicitly with `experimental.asyncCallHook: false`.
|
|
218
|
+
::
|
|
219
|
+
|
|
220
|
+
Alternatively, you can ensure `callHook` always returns a `Promise` with:
|
|
221
|
+
|
|
222
|
+
```ts twoslash [nuxt.config.ts]
|
|
223
|
+
export default defineNuxtConfig({
|
|
224
|
+
experimental: {
|
|
225
|
+
asyncCallHook: true,
|
|
226
|
+
},
|
|
227
|
+
})
|
|
228
|
+
```
|
|
229
|
+
|
|
183
230
|
## Migrating to Nuxt 4
|
|
184
231
|
|
|
185
232
|
Nuxt 4 includes significant improvements and changes. This guide will help you migrate your existing Nuxt 3 application to Nuxt 4.
|
|
@@ -161,6 +161,7 @@ type AsyncDataRequestContext = {
|
|
|
161
161
|
|
|
162
162
|
type AsyncData<DataT, ErrorT> = {
|
|
163
163
|
data: Ref<DataT | undefined>
|
|
164
|
+
pending: Ref<boolean>
|
|
164
165
|
refresh: (opts?: AsyncDataExecuteOptions) => Promise<void>
|
|
165
166
|
execute: (opts?: AsyncDataExecuteOptions) => Promise<void>
|
|
166
167
|
clear: () => void
|
|
@@ -229,6 +230,7 @@ This only caches data when `experimental.payloadExtraction` in `nuxt.config` is
|
|
|
229
230
|
| `execute` | `(opts?: AsyncDataExecuteOptions) => Promise<void>` | Alias for `refresh`. |
|
|
230
231
|
| `error` | `Ref<ErrorT \| undefined>` | Error object if the data fetching failed. |
|
|
231
232
|
| `status` | `Ref<'idle' \| 'pending' \| 'success' \| 'error'>` | Status of the data request. See below for possible values. |
|
|
233
|
+
| `pending` | `Ref<boolean>` | Boolean flag indicating whether the current request is in progress. |
|
|
232
234
|
| `clear` | `() => void` | Resets `data` to `undefined` (or the value of `options.default()` if provided), `error` to `undefined`, set `status` to `idle`, and cancels any pending requests. |
|
|
233
235
|
|
|
234
236
|
### Status values
|
|
@@ -76,6 +76,7 @@ Returns the same `AsyncData` object as [`useFetch`](/docs/4.x/api/composables/us
|
|
|
76
76
|
| `execute` | `(opts?: AsyncDataExecuteOptions) => Promise<void>` | Alias for `refresh`. |
|
|
77
77
|
| `error` | `Ref<ErrorT \| undefined>` | Error object if the data fetching failed. |
|
|
78
78
|
| `status` | `Ref<'idle' \| 'pending' \| 'success' \| 'error'>` | Status of the data request. |
|
|
79
|
+
| `pending` | `Ref<boolean>` | Boolean flag indicating whether the current request is in progress. |
|
|
79
80
|
| `clear` | `() => void` | Resets `data` to `undefined`, `error` to `undefined`, sets `status` to `idle`, and cancels any pending requests. |
|
|
80
81
|
|
|
81
82
|
:read-more{to="/docs/4.x/api/composables/use-fetch#return-values"}
|