3

I want to learn react testing library with react-router-dom. I created the app using npx create-react-app command. The resulting code already has an App.test.js in the parent folder:

import { render, screen } from '@testing-library/react';
import App from './App';

test('renders learn react link', () => {
  render(<App />);
  const linkElement = screen.getByText(/learn react/i);
  expect(linkElement).toBeInTheDocument();
});

When I run npm test, it passes successfully as expected. I installed React-Router-DOM with npm i react-router-dom and I wrapped the main App.js with the BrowserRouter component.

import { BrowserRouter } from 'react-router-dom'
import logo from './logo.svg';
import './App.css';

function App() {
  return (
    <BrowserRouter>
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <p>
            Edit <code>src/App.js</code> and save to reload.
          </p>
          <a
            className="App-link"
            href="https://reactjs.org"
            target="_blank"
            rel="noopener noreferrer"
          >
            Learn React
          </a>
        </header>
      </div>
    </BrowserRouter>
  );
}

export default App;

But now the test failed with the following error:

Cannot find module 'react-router-dom' from 'src/App.js'

screenshot of the failed test message

Why the unit test cannot detect react-router-dom, which has already properly installed?

Github repo of my code so far.

Note: I have already tested wrapped my component using MemoryRouter instead of BrowserRouter, but still the same error.

2
  • 1
    Rolling back to RRDv6 your code runs without issue. This seems to be more an issue with the way you've bootstrapped your project with Create-React-App, which is effectively deprecated, and how Jest is setup/configured. The RRD maintainers recommend using Vite. I noticed also you are using older versions of React-Testing-Library. A lot has also changed there with newer versions of React. I suspect you'll want to upgrade to modern versions of your testing dependencies. Commented Dec 6, 2024 at 6:47
  • FWIW here's a link to a running CodeSandbox demo stood up from scratch with your code copy/pasted in and it all works without issue. I think there's just something odd with the way you have created your project. Commented Dec 6, 2024 at 7:36

2 Answers 2

-1

Everything seems fine, just update the BrowseRouter import from this in App.js:

import { BrowserRouter } from 'react-router-dom'

to this:

import { BrowserRouter } from 'react-router';

-> Please make sure you have these 3 packages installed:

  • react-dom
  • react-router
  • react-router-dom

Hope this helps..

reference: https://www.npmjs.com/package/react-router-dom

Sign up to request clarification or add additional context in comments.

10 Comments

Not working, importing BrowserRouter from 'react-router' just changes the error message into ReferenceError: TextEncoder is not defined
@PetrandoRichard, the insutrciton was correct and actually solved your problem. Now you have another problem. You need to find the references to TextEncoder to see which package they are coming from and install that package.
@bman That is not necessarily true... it could simply be creating a new error that occurs earlier and masks the current error. One also could say to uninstall react-router-dom and delete that import and code and say it "fixed the problem". OP could take your advice and maybe fix this new error and be right back where they are now with the original error still.
@bman an actual solution that does not create new problem
@DrewReese I copied the code provided by OP into playground and did as I answered, and in my case, code worked without any error, so I posted the answer, if you are sure answer is wrong, I will be super happy to delete the answer, but maybe the OP is actually facing any other error! Atleast try not to be rude.
|
-1

I had the same problem and, as one commentator said, I downgraded react router to v6 with npm i react-router-dom@6. It helped me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.