I'm using jest
to test a component with a <Link>
from react-router v4.
I get a warning that <Link />
requires the context from a react-router <Router />
component.
How can I mock or provide a router context in my test? (Basically how do I resolve this warning?)
Link.test.js
import React from 'react'; import renderer from 'react-test-renderer'; import { Link } from 'react-router-dom'; test('Link matches snapshot', () => { const component = renderer.create( <Link to="#" /> ); let tree = component.toJSON(); expect(tree).toMatchSnapshot(); });
The warning when the test is run:
Warning: Failed context type: The context `router` is marked as required in `Link`, but its value is `undefined`.
Link.test.js import React from 'react'; import renderer from 'react-test-renderer'; import { Link } from 'react-router-dom'; test('Link matches snapshot', () => { const component = renderer. create( <Link to="#" /> ); let tree = component. toJSON(); expect(tree). toMatchSnapshot(); });
You can use the createMemoryHistory function and Router component to test it. Create a memory history with initial entries to simulate the current location, this way we don't rely on the real browser environment. After firing the click event, assert the pathname is changed correctly or not.
import { useRouter } from "next/router"; jest. mock("next/router", () => ({ useRouter: jest. fn(), })); And then use like this inside your test block.
Jest is a JavaScript testing framework that allows developers to run tests on JavaScript and TypeScript code and can be easily integrated with React JS. Open the package. json, and you will find that when you use create-react-app for creating a react project, it has default support for jest and react testing library.
You can wrap your component in the test with the StaticRouter to get the router context into your component:
import React from 'react'; import renderer from 'react-test-renderer'; import { Link } from 'react-router-dom'; import { StaticRouter } from 'react-router' test('Link matches snapshot', () => { const component = renderer.create( <StaticRouter location="someLocation" context={context}> <Link to="#" /> </StaticRouter> ); let tree = component.toJSON(); expect(tree).toMatchSnapshot(); });
Have a look at the react router docs about testing
I had the same issue and using StaticRouter
would still require the context
which needed more configuration to have it available in my test, so I ended up using the MemoryRouter
which worked very well and without any issues.
import React from 'react'; import renderer from 'react-test-renderer'; import { MemoryRouter } from 'react-router-dom'; // SampleComponent imports Link internally import SampleComponent from '../SampleComponent'; describe('SampleComponent', () => { test('should render', () => { const component = renderer .create( <MemoryRouter> <SampleComponent /> </MemoryRouter> ) .toJSON(); expect(component).toMatchSnapshot(); }); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With