I'm using React-18.0.0 in my project and in the test file I'm getting an error something below
createRoot(...): Target container is not a DOM element.
My test file is :
import ReactDOM from 'react-dom/client';
import { render, screen } from "@testing-library/react";
import App from "./App";
const root = ReactDOM.createRoot(document.getElementById("root"));
test("renders learn react link", () => {
root.render(<App />);
const linkElement = screen.getByText(/Hello React/i);
expect(linkElement).toBeInTheDocument();
});
Your
test
usesroot.render(<App />)
while React'stesting-library
provides there ownrender
function to use inside atest
Retrieving the
root
isn't needed, and is causing the error you're showing.So, apply the following change:
Example of working
App.test.js
: