How to test a React component that uses context like in react-router 2.0 with Jest 0.8.x
<p>I had this problem in the past while using older versions of react-router which I solved using: <a href="https://github.com/reactjs/react-router/blob/57543eb41ce45b994a29792d77c86cc10b51eac9/docs/guides/testing.md#stubroutercontext" rel="nofollow">stubRouterContext</a> + a hacky way to access the component instance (using refs: <a href="https://github.com/reactjs/react-router/issues/1140#issuecomment-113174774" rel="nofollow">https://github.com/reactjs/react-router/issues/1140#issuecomment-113174774</a>)</p>

<p>I thought this would improve in the future but I am hitting the same wall with react-router 2.0 (I am not saying this is a problem with react-router but since it uses context, it affects my tests). So, I have a component that uses context to push new state into the url <code>this.context.router.push(...)</code> which is the way to go now 
<a href="https://github.com/reactjs/react-router/blob/master/upgrade-guides/v2.0.0.md#programmatic-navigation" rel="nofollow">https://github.com/reactjs/react-router/blob/master/upgrade-guides/v2.0.0.md#programmatic-navigation</a></p>

<p>I am telling <code>jest.dontMock('react-router')</code> but my test will fail with:</p>

<p><code>
TypeError: Cannot read property 'push' of undefined
</code></p>

<p>This happens because the instance returned by <code>TestUtils.renderIntoDocument</code> will have:</p>

<p><code>
context: Object { router: undefined }
</code></p>

<p>Now, what is the real problem here? Is it Jest? I'm pretty sure I am not the only one who encounters this and since <a href="https://github.com/reactjs/react-router/blob/57543eb41ce45b994a29792d77c86cc10b51eac9/docs/guides/testing.md#stubroutercontext" rel="nofollow">stubRouterContext</a> it's not in the <a href="https://github.com/reactjs/react-router/blob/master/docs/guides/testing.md" rel="nofollow">official docs</a> of react-router anymore, is there any broad accepted solution for this?</p>

<p>How would I make the test to work? Which is basically having the correct context and being able to access everything from the component instance returned by <code>TestUtils.renderIntoDocument</code>.</p>

<p>I am using react 0.14.7, jest-cli 0.8.2 and react-router 2.0.</p>