@nuxt/docs-nightly 5.0.0-29805631.e52272e5 → 5.0.0-29807902.5022deff
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/17.testing.md +162 -15
- package/package.json +1 -1
|
@@ -265,16 +265,15 @@ Under the hood, `mountSuspended` wraps `mount` from `@vue/test-utils`, so you ca
|
|
|
265
265
|
|
|
266
266
|
For example:
|
|
267
267
|
|
|
268
|
-
```ts twoslash
|
|
268
|
+
```ts twoslash [tests/components/SomeComponents.nuxt.spec.ts]
|
|
269
269
|
// @noErrors
|
|
270
|
-
import { expect, it } from 'vitest'
|
|
271
270
|
import type { Component } from 'vue'
|
|
272
271
|
|
|
273
272
|
declare module '#components' {
|
|
274
273
|
export const SomeComponent: Component
|
|
275
274
|
}
|
|
276
275
|
// ---cut---
|
|
277
|
-
|
|
276
|
+
import { expect, it } from 'vitest'
|
|
278
277
|
import { mountSuspended } from '@nuxt/test-utils/runtime'
|
|
279
278
|
import { SomeComponent } from '#components'
|
|
280
279
|
|
|
@@ -286,15 +285,11 @@ it('can mount some component', async () => {
|
|
|
286
285
|
})
|
|
287
286
|
```
|
|
288
287
|
|
|
289
|
-
```ts twoslash
|
|
290
|
-
// @noErrors
|
|
288
|
+
```ts twoslash [tests/App.nuxt.spec.ts]
|
|
291
289
|
import { expect, it } from 'vitest'
|
|
292
|
-
// ---cut---
|
|
293
|
-
// tests/components/SomeComponents.nuxt.spec.ts
|
|
294
290
|
import { mountSuspended } from '@nuxt/test-utils/runtime'
|
|
295
291
|
import App from '~/app.vue'
|
|
296
292
|
|
|
297
|
-
// tests/App.nuxt.spec.ts
|
|
298
293
|
it('can also mount an app', async () => {
|
|
299
294
|
const component = await mountSuspended(App, { route: '/test' })
|
|
300
295
|
expect(component.html()).toMatchInlineSnapshot(`
|
|
@@ -306,9 +301,34 @@ it('can also mount an app', async () => {
|
|
|
306
301
|
})
|
|
307
302
|
```
|
|
308
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
|
+
|
|
309
324
|
The options object accepts `@vue/test-utils` mount options and the following properties:
|
|
310
325
|
|
|
311
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.
|
|
312
332
|
|
|
313
333
|
#### `renderSuspended`
|
|
314
334
|
|
|
@@ -322,16 +342,15 @@ The passed in component will be rendered inside a `<div id="test-wrapper"></div>
|
|
|
322
342
|
|
|
323
343
|
Examples:
|
|
324
344
|
|
|
325
|
-
```ts twoslash
|
|
345
|
+
```ts twoslash [tests/components/SomeComponents.nuxt.spec.ts]
|
|
326
346
|
// @noErrors
|
|
327
|
-
import { expect, it } from 'vitest'
|
|
328
347
|
import type { Component } from 'vue'
|
|
329
348
|
|
|
330
349
|
declare module '#components' {
|
|
331
350
|
export const SomeComponent: Component
|
|
332
351
|
}
|
|
333
352
|
// ---cut---
|
|
334
|
-
|
|
353
|
+
import { expect, it } from 'vitest'
|
|
335
354
|
import { renderSuspended } from '@nuxt/test-utils/runtime'
|
|
336
355
|
import { SomeComponent } from '#components'
|
|
337
356
|
import { screen } from '@testing-library/vue'
|
|
@@ -342,11 +361,8 @@ it('can render some component', async () => {
|
|
|
342
361
|
})
|
|
343
362
|
```
|
|
344
363
|
|
|
345
|
-
```ts twoslash
|
|
346
|
-
// @noErrors
|
|
364
|
+
```ts twoslash [tests/App.nuxt.spec.ts]
|
|
347
365
|
import { expect, it } from 'vitest'
|
|
348
|
-
// ---cut---
|
|
349
|
-
// tests/App.nuxt.spec.ts
|
|
350
366
|
import { renderSuspended } from '@nuxt/test-utils/runtime'
|
|
351
367
|
import App from '~/app.vue'
|
|
352
368
|
|
|
@@ -365,6 +381,11 @@ it('can also render an app', async () => {
|
|
|
365
381
|
The options object accepts `@testing-library/vue` render options and the following properties:
|
|
366
382
|
|
|
367
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.
|
|
368
389
|
|
|
369
390
|
#### `mockNuxtImport`
|
|
370
391
|
|
|
@@ -658,6 +679,132 @@ If you prefer to use `@vue/test-utils` on its own for unit testing in Nuxt, and
|
|
|
658
679
|
|
|
659
680
|
Congratulations, you're all set to start unit testing with `@vue/test-utils` in Nuxt! Happy testing!
|
|
660
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
|
+
|
|
661
808
|
## End-To-End Testing
|
|
662
809
|
|
|
663
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.
|