title: How do I mock react-router-dom using jest from a __mocks__ directory
body: <p>I have a test that is testing a provider, which is using react-router 6 <code>useLocation</code></p>
  <pre class="lang-json prettyprint-override"><code>&quot;react-router-dom&quot;: &quot;^6.0.0-beta.0&quot;,
  </code></pre>
  <p>It works fine when I mock in the file</p>
  <pre class="lang-js prettyprint-override"><code>import React from 'react';
  import { render } from '@testing-library/react';
  import Categories from './Categories';
  import mockCategories from '../../__mocks__/mockCategories';
  import ExpenditureProvider from '../../domains/Expenditure/ExpenditureProvider';
  
  jest.mock('react-router-dom', () =&gt; {
    const originalModule = jest.requireActual('react-router-dom');
  
    return {
      __esModule: true,
      ...originalModule,
      useNavigate: jest.fn(),
      useLocation: jest.fn().mockReturnValue({ pathname: '/'}),
    };
  });
  
  describe('Categories', () =&gt; {
    test('should render Categories component', () =&gt; {
      const categories = render(
        &lt;ExpenditureProvider&gt;
          &lt;Categories categories={mockCategories}/&gt;
        &lt;/ExpenditureProvider&gt;
        );
    });
  });
  </code></pre>
  <p>but when I try to put this a <code>__mocks__</code> directory, This is the error I get</p>
  <pre><code>Error: Uncaught [Error: useLocation() may be used only in the context of a &lt;Router&gt; component.]
  </code></pre>
  <pre class="lang-js prettyprint-override"><code>// __mocks__/react-router-dom.js
  
  const originalModule = jest.requireActual('react-router-dom');
  export default {
    __esModule: true,
    ...originalModule,
    useLocation: jest.fn().mockReturnValue({ pathname: '/'}),
    useNavigate: jest.fn()
  }
  
  </code></pre>
  <p>The mock file is in the root directory with node_modules</p>
  <pre><code>├── __mocks__
  │   └── react-router-dom.js
  ├── node_modules
  ├── src
  │ 
  </code></pre>
  <p>What I would like to do is only mock from the <code>__mocks__</code> directory because this mock is used in multiple tests</p>