Skip to content
A simple-to-use tool for testing React components that use React Router v.4
Branch: master
Clone or download
Latest commit 942ba63 Oct 2, 2018


React Mock Router is a simple-to-use tool for testing React components that use React Router v4. It can be used in tests as a replacement for React Router's MemoryRouter and StaticRouter.


npm install react-mock-router --save-dev


The MockRouter component can receive any of the following arguments as props.

  • location
  • params
  • path
  • createHref
  • push
  • replace


React Mock Router takes mocked router props as arguments, passing them to children components.

import MockRouter from 'react-mock-router';

it('has a button that links to the todo edit page', () => {

  const testProps = {
    todo: {
      id: 1,
      title: "Title",
      body: "Body"
  const push = jest.fn();

  const todoItemWrapper = mount(
    <MockRouter push={push}>
      <Route render={(props) => (
        <TodoItem {...props} {...testProps}/>

  const editButton = todoItemWrapper.find('button').filterWhere(button =>

  editButton.simulate('click', { preventDefault() { } });


React Mock Router also provides the correct context for components being tested, so you won't recieve errors if those components use React Router components like Route or Link. If you're testing a component that isn't considered a route component (i.e. not rendered as part of a Route) but need to wrap it in a router, you can do the following.

import MockRouter from 'react-mock-router';

it('correctly defines the action when given a formType', () => {
  const todoFormWrapper = mount(
        <TodoFormContainer formType="new" />



This project is released under the MIT license.

You can’t perform that action at this time.