Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Jest to test a Link from react-router v4

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`. 
like image 777
Don P Avatar asked May 03 '17 23:05

Don P


People also ask

How do I test a link in React?

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(); });

How do I test a route in Jest?

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.

How do I test my Router push with Jest React?

import { useRouter } from "next/router"; jest. mock("next/router", () => ({ useRouter: jest. fn(), })); And then use like this inside your test block.

Can you test React with Jest?

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.


2 Answers

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

like image 91
Andreas Köberle Avatar answered Sep 27 '22 23:09

Andreas Köberle


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();   }); }); 
like image 32
Mahdi Avatar answered Sep 27 '22 23:09

Mahdi