title: How to test react-router Links with Enzyme?
body: <p>I see quite a few similar questions to this, but all seem wildly outdated or incredibly convoluted.</p>
  
  <p>I have a React component which contains a React Router <code>Link</code> component, like so:</p>
  
  <pre><code>export default class Home extends Component {                                      
    render() {                                                                       
      return (                                                                       
        &lt;div className="Home"&gt;  
          &lt;Link to="/mission"&gt;Begin&lt;/Link&gt;                                           
        &lt;/div&gt;                                              
      );                                                                             
    }                                                                                
  }
  </code></pre>
  
  <p>Then, I am trying to test very simply that the <code>Home</code> component contains a <code>Link</code> component which has a <code>to=/mission</code> property, using Jest. So, as per the <a href="https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/guides/testing.md" rel="noreferrer">Testing React Router docs</a>, I tried many variations of tests, but this code most succinctly demonstrates what I am trying to achieve (example uses <a href="https://github.com/blainekasten/enzyme-matchers#tocontainreactreactinstanceobject" rel="noreferrer">jest-enzyme</a> for brevity):</p>
  
  <pre><code>it('includes link to Mission scene', () =&gt; {                                       
    const home = shallow(&lt;MemoryRouter&gt;&lt;Home /&gt;&lt;/MemoryRouter&gt;);                                                              
    expect(home).toContainReact(&lt;Link to="/mission"&gt;Begin&lt;/Link&gt;)                    
  });
  </code></pre>
  
  <p>Obviously, that code has several issues, not the least of which is the error:</p>
  
  <pre><code>Warning: Failed context type: The context `router` is marked as required in `Link`, but its value is `undefined`.
  </code></pre>
  
  <p>But notice the key bits: I want to <em>shallow</em> render (if possible) the <code>Home</code> component in order to test that component in isolation. At the same time, I am trying to wrap that in a <code>MemoryRouter</code> or somehow get the context in place so that I can test the <code>Link</code> components.</p>
  
  <p>Astute potential answerers will notice that this issue is actually going to prevent <em>all</em> isolated tests of that component (not just tests related to React Router), because I can't shallow render the component at all.</p>