Blog>
Snippets

Using Memory History for Test Environments

Provide an example of setting up memory history with TanStack Router for running tests or in environments where the DOM isn't available, like Node.js or React Native.
import { createMemoryRouter, RouterProvider } from 'tanstack-router';
Import necessary modules from tanstack-router to work with memory-based routing.
const memoryRouter = createMemoryRouter([{ path: '/', element: 'Home' },{ path: '/about', element: 'About' }]);
Create a memory router instance and define routes. In a testing environment, the elements can simply be strings or test components.
const App = () => (
  <RouterProvider router={memoryRouter} />
);
Create a React component, App, that utilizes the RouterProvider to pass the memoryRouter. This setup can be used in tests without rendering in a DOM environment.