import { render, screen, waitFor } from '@testing-library/react';
import React from "react";
import { createMemoryRouter, RouterProvider, Outlet, createBrowserRouter } from 'react-router-dom';
const Layout = () => (
<div>
<header>Header</header>
<main>
<Outlet />
</main>
<footer>Footer</footer>
</div>
);
const routes = [
{
path: '/',
element: <Layout />,
children: [
{ path: '/', element: <>home</> },
{ path: 'about', element: <>about</> },
],
},
];
const router = createBrowserRouter(routes);
function App() {
return <RouterProvider router={router} />;
}
describe("sample code ", () => {
it("test home", async () => {
const router1 = createMemoryRouter(routes, { initialEntries: ['/about'] })
render(
<RouterProvider router={router1} />
);
expect(screen.getByText(/header/i)).toBeInTheDocument();
await waitFor(() => expect(screen.getByText(/about/i)).toBeInTheDocument());
expect(screen.getByText(/footer/i)).toBeInTheDocument();
})
})
I'm currently testing routes and nested routes using initial entries in React Testing Library (RTL). However, I'm encountering an issue where the Outlet component isn't functioning as expected in my tests, even though it works perfectly in the actual application.


BrowserRouter(viaApp) within a "legacy"MemoryRouter. You can't, and shouldn't, render routers within routers. What exactly are you trying to unit test there? Please edit to clarify that you are trying to accomplish and include a complete minimal reproducible example of the relevant code.routesfromcreateMemoryRouter(routes, { initialEntries: ['/about'] })in the testing code the sameroutesfrom theAppcomponent code? Or is this actually all just your unit test code? It's still unclear what exactly you are wanting to unit test here. On the surface it looks like you are trying to unit test the 3rd-party React-Router code, that it correctly matches a URL path and renders the appropriate routed component UI.