vue-router-mock is a testing utility designed for Vue 3 applications, specifically to facilitate unit and integration testing of components that interact with the official `vue-router`. Currently stable at version 2.0.2, the library maintains an active release cadence, providing minor updates for bug fixes and compatibility, such as the recent addition of `vue-router` v5 support. Its core purpose is to provide a mock `vue-router` instance that can be injected into components, enabling developers to spy on navigation calls (e.g., `push`, `replace`), simulate route changes, and assert router state without requiring a full browser environment or actual navigation. It differentiates itself from end-to-end testing tools by focusing on isolated scenarios, leading to more granular and faster test execution. The library integrates seamlessly with `@vue/test-utils` and offers flexible setup options for test environments like Jest or Vitest through functions such as `createRouterMock` and `injectRouterMock`.
npm install vue-router-mockVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to globally set up `vue-router-mock` for a test suite using `beforeEach`, inject a mock router, and then mount a component to test its router interactions, asserting on `wrapper.router` calls.
Refer to the official `CHANGELOG.md` for specific migration instructions when upgrading from v1.x to v2.x. Update your testing setup files and component interaction tests accordingly.
Use dedicated E2E testing frameworks like Cypress or Playwright with your actual `vue-router` configuration for full application flow tests.
Ensure your test setup file (e.g., `setupFilesAfterEnv` in Jest or `setupFiles` in Vitest) includes `config.plugins.VueWrapper.install(VueRouterMock)` after importing `VueRouterMock`.
If encountering import errors in Jest, try renaming your setup file to `.cjs` or converting its imports to `require()` syntax. Alternatively, ensure your Jest configuration is set up for ESM.
Ensure `injectRouterMock(router)` is called before mounting the component in your test, and that `config.plugins.VueWrapper.install(VueRouterMock)` is set up globally if accessing via `wrapper.router`.
Verify that `injectRouterMock(router)` is being called in your test setup (e.g., `beforeEach`) to ensure the mock router is provided to the components within the test scope.
Make sure to `import { config } from '@vue/test-utils'` and that the `config.plugins.VueWrapper.install(VueRouterMock)` line is executed after `config` is properly defined.