-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
vue-router's useRoute is not correctly mocked #1918
Comments
Should be fixed by #1919 and vitejs/vite#9860 For now, you can manually add {
resolve: {
conditions: process.env.VITEST ? ['node'] : []
}
} |
Thanks. I can confirm this actually makes the mock work.
Any specific reason why this bug occurs? It has something to do with module resolution?
Verstuurd vanaf mijn iPhone
… Op 26 aug. 2022 om 08:59 heeft Vladimir ***@***.***> het volgende geschreven:
Should be fixed by #1918 and vitejs/vite#9860
For now, you can manually add node condition to your config:
{
resolve: {
conditions: process.env.VITEST ? ['node'] : []
}
}
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you authored the thread.
|
Yes. Vite uses |
For me this workaround doesn't work. |
Should be fixed in the next Vitest version. Also requires Vite 3.2. |
I'm running vitest 0.24.4 and vite 3.2.2, and yet the mock is not working. Is the issue really solved? Mocking with fn() gives "Cannot read properties of undefined (reading 'meta')" error when reading "route.meta.breadcrumbs": Mocking without fn() works in first test, but overrides mock definition in the next test executed: |
Yes, it is fixed. If it wasn't, your mocks would never be called. You have error in your test code. |
I have tried all sorts of cleaning/restoring functions before and after each test, but they don't do anything. Mock from the first test overrides the mock definition in the next test executed. At this point, we have to put each test in a separated file to make it work, which is very messy. Is it per design that mocking router once in the file, overrides all later router mocks? Any pointers would be very helpful. |
The easiest way to mock a route for different tests would be, in my opinion: import * as routerExports from 'vue-router'
const useRouteMock = vi.spyOn(routerExports, 'useRoute')
useRouteMock.mockReturnValue({ name: 'name1' })
useRouteMock.mockReturnValue({ name: 'name2' }) Or if you like import { useRoute } from 'vue-router'
vi.mock('vue-router', () => ({ useRoute: vi.fn() }))
vi.mocked(useRoute).mockReturnValue({ name: 'name1' })
vi.mocked(useRoute).mockReturnValue({ name: 'name2' }) |
Hello again,
This is the error I'm getting:
|
Please reopen this case, the bug hasn't been fixed. @sheremet-va Still getting:
|
Same issue. vitest version: ^0.24.5 |
Updated: I did manage to make it work with vi.mock
I'm facing the same issue as you. I was trying to spy the useRouter() but it trows: Also tried with vi.mock and vue-router-mock library, none of those options did work for me I'm using: |
works for me with
|
I got error when I used the first solution
|
I'm experiencing the same issue on version 0.31.4. See my example of the issue: /* vitest.config.mjs */
import { defineVitestConfig } from 'nuxt-vitest/config'
export default defineVitestConfig({
test: {
globals: true,
clearMocks: true,
restoreMocks: true,
threads: false,
testTimeout: 300000,
setupFiles: ['tests/unit.i18n.setup.ts', 'tests/unit.vuetify.setup.ts', 'tests/unit.router.setup.ts'],
deps: {
inline: ['element-plus', /@nuxt\/test-utils/],
},
ssr: {
noExternal: [/vue-i18n/, 'vuetify'],
}
}
}); /* unit.router.setup.ts */
import { vi } from 'vitest';
vi.mock('vue-router', async (importOriginal) => {
const mod: object = await importOriginal();
return {
...mod,
useRouter: vi.fn(() => ({ replace: vi.fn() })),
};
}); /* component.test.ts */
describe('v-select-locale', async () => {
const router = createRouterMock();
beforeEach(() => {
injectRouterMock(router);
});
injectRouterMock(router);
type ComponentProps = any;
type ComponentVariables = {
availableLocales: ComputedRef<SelectItem<string>[]>;
currentLocale: Ref<string>;
onChangeLocale: (locale: string) => void;
};
type ComponentWrapperType = VueWrapper<ComponentPublicInstance<ComponentProps, ComponentVariables>>;
const component: ComponentWrapperType = mount(VSelectLocale, {
global: {
stubs: ['v-list-item', 'v-select'],
},
});
it('should change the selected locale', () => {
// This works...., So the router is in the component for testing
expect(component.router).toBe(router);
// Error triggered here
component.vm.onChangeLocale('en');
});
}); error: FAIL tests/components/v-select-locale.test.ts > v-select-locale > should change the selected locale
TypeError: Cannot read properties of undefined (reading 'replace')
❯ Proxy.onChangeLocale components/v-select-locale.vue:63:10
61| */
62| function onChangeLocale(locale: string) {
63| router.replace(switchLocalePath(locale));
| ^
64| }
65|
❯ tests/components/v-select-locale.test.ts:48:5
I would expect that the setup file would mock the vue-router imports with |
I struggled with this while working on a Nuxt 3 project using Vitest, nuxt-vitest. While tinkering around I stumbled upon a combination that works for me.
My Here is the full
|
Describe the bug
When using vitest-suggested testing libraries, I can't successfully mock
useRoute
anduseRouter
from thevue-router
package.I'm using:
Any time I run a component test with a mocked
useRoute
, the mock returns undefined, and Vue warns about missing injections.Reproduction
See a minimal reproduction, using a fresh
yarn create vite
project here: https://github.com/hidde-jan/vitest-use-route-example/blob/main/src/components/__test__/HelloWorld.test.tsSystem Info
The text was updated successfully, but these errors were encountered: