I am running a create-react-app with react-16.8.6, and no modifications except for adding react-router to the mix. Now tests don't work.
After rolling things back, I found that the base test fails as soon as I import ANY part of the "react-router-dom" library. Any ideas whats going wrong?
Below is the App.js and App.test.js when I comment out the line:
import { Switch } from "react-router-dom";
the tests run without issue. When I return the line to the code I get the following error:
Test suite failed to run
Cannot find module 'react' from 'react-router-dom.js'
However, Jest was able to find:
'./App.css'
'./App.js'
'./App.test.js'
App.js
import React from "react";
import { Switch } from "react-router-dom";
import logo from "./logo.svg";
import "./App.css";
function App() {
return (
<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>
);
}
export default App;
app.test.js
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
it("renders without crashing", () => {
const div = document.createElement("div");
ReactDOM.render(<App />, div);
ReactDOM.unmountComponentAtNode(div);
});

react-router-dominto your test but whenreact-router-dom.jsitself tries to import from'react'it just fails to find a module. Maybe yourjestconfirguration(checkjestbranch in yourpackage.json) may bring additional details.