Vue Router Mock

2.0.2 · active · verified Sun Apr 19

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`.

Common errors

Warnings

Install

Imports

Quickstart

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.

import { VueRouterMock, createRouterMock, injectRouterMock } from 'vue-router-mock';
import { config } from '@vue/test-utils';
import { mount } from '@vue/test-utils';
import { defineComponent } from 'vue';

// Create a simple component to test
const MyComponent = defineComponent({
  template: `
    <div>
      <p>Current path: {{ $route.path }}</p>
      <button @click="$router.push('/about')">Go to About</button>
    </div>
  `
});

// Create one router per test file/suite setup
const router = createRouterMock();

beforeEach(() => {
  router.reset(); // Reset the router state before each test
  injectRouterMock(router); // Inject the mock router globally for the test
});

// Add properties to the @vue/test-utils wrapper (required for `wrapper.router`)
config.plugins.VueWrapper.install(VueRouterMock);

describe('MyComponent with vue-router-mock', () => {
  it('should display the current path and navigate', async () => {
    const wrapper = mount(MyComponent);

    // Initial state
    expect(wrapper.find('p').text()).toContain('/');
    expect(wrapper.router.path).toBe('/');

    // Simulate navigation
    await wrapper.find('button').trigger('click');
    
    // Assertions on the mock router
    expect(wrapper.router.push).toHaveBeenCalledTimes(1);
    expect(wrapper.router.push).toHaveBeenCalledWith('/about');

    // Assertions on component state after navigation (if it reacts to route change)
    // Note: for reactive updates, you might need await wrapper.vm.$nextTick()
    // depending on how the component consumes the route changes
    expect(wrapper.find('p').text()).toContain('/about');
  });
});

view raw JSON →