25 July 2017

Testing React components with Jest I got an error: "Warning: Failed context type: The context `router` is marked as required in `Link`, but its value is `undefined`.". I am using Link from react-router-dom in snapshot rendering with enzyme mount. The solution was to wrap my component with "BrowserRouter" in my test.

Source code viewer
  1. import { BrowserRouter } from 'react-router-dom'
  2.  
  3.  
  4. describe('<MyComponent />', () => {
  5. it('Snapshot render', () => {
  6. const wrapper = mount(
  7. <BrowserRouter>
  8. <MyCompontent t={jest.fn()} />
  9. </BrowserRouter>,
  10. );
  11.  
  12. expect(toJson(wrapper.render())).toMatchSnapshot();
  13. });
  14. });
Programming Language: ECMAScript