Closed tomsdob closed 5 months ago
You can bypass this issue by creating a setup file for your tests and there you can add the mock for useRoute
auto-import.
But doing this will cause the following error:
TypeError: Cannot read properties of undefined (reading 'afterEach')
❯ node_modules/@nuxt/test-utils/dist/runtime/entry.mjs:7:14
5| const nuxtApp = useNuxtApp();
6| await nuxtApp.callHook("page:finish");
7| useRouter().afterEach(() => nuxtApp.callHook("page:finish"));
| ^
8| }
9| export {};
I've followed the instructions on the official documentation and nothing worked.
The error is not thrown since https://github.com/nuxt/test-utils/pull/726, but note that setting a vue-router mock useRoute
won't necessarily have the effect you want in a Nuxt app - as the auto-imported useRoute
is actually a Nuxt utility that has slightly different (but more accurate) logic within a Nuxt app.
Instead you can do this:
const wrapper = await mountSuspended(Index, {
route: '/123'
})
Environment
Build Modules: -
Reproduction
https://stackblitz.com/edit/github-vy6psx
Describe the bug
I get this error message whenever I try to mock
vue-router
after upgrading to3.9.0
. Before the merge ofnuxt-vitest
and@nuxt/text-utils
, the mock worked fine. I've tried tovi.resetModules()
and other alike functions, but none of them have helped.Additionally, if
@nuxt/test-utils/module
is present innuxt.config.ts
modules,npm run dev
throws the following error:Additional context
Tests can be ran with
npm run test
.vi.mock()
is located inpages/__tests__/index.spec.ts
.Logs
No response