Repository navigation
Replies: 1 comment
|
This happens because Vite's module graph ( When Vitest runs with The dependency chain looks like this: Because How to handle this cleanlyThe standard fix is to isolate your component tests from the global route tree:
import { createRouter, createMemoryHistory } from 'vue-router';
import { mount } from '@vue/test-utils';
import UserCard from './UserCard.vue';
const testRouter = createRouter({
history: createMemoryHistory(),
routes: [{ path: '/users/:id', component: UserCard }],
});
const wrapper = mount(UserCard, {
global: { plugins: [testRouter] },
});
vi.mock('vue-router', () => ({
useRoute: () => ({ params: { id: '123' } }),
useRouter: () => ({ push: vi.fn() }),
}));Keeping production router imports out of individual unit tests stops the module graph from linking every component change back to the entire test suite. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Description:
When using
--changedoption with Vue Router, all test files are detected as changed when modifying any component, even though the router uses lazy-loadedimports (() => import(...)).The dependency graph appears to consider lazy route imports as actual dependencies, causing the entire test suite to run.
Reproduction link
Steps to reproduce:
UserCard)pnpm vitest list --changed --filesOnlyrouter.js) and commitpnpm vitest list --changed --filesOnlyEnvironment
Vitest: 4.0.18
Vite: 7.3.1
Vue: 3.5.29
Vue Router: 5.0.3
@vue/test-utils: 2.4.6
OS: Linux (WSL Ubuntu)
Node: v24.13.0
Question
Why does Vitest's
--changedconsider imports in the router as dependencies? Is there a way to configure Vitest to ignore these transitive dependencies or to make--changedsmarter about component imports?Workaround
I created a Vite plugin to virtualize route component imports, which breaks the dependency chain for
--changeddetection.This feels like a hacky solution and I'm wondering if there's a more idiomatic way to solve this issue, or if Vitest could handle lazy imports differently out of the box.
All reactions