Testing
Automated router tests need a controlled starting location without changing the browser URL.
TanStack · Function
Creates a TanStack Router history whose navigation entries are stored in memory. Initialize it with initialEntries, then pass the history instance to a router.
createMemoryHistory builds a router history implementation whose entries live only in memory. Provide initialEntries to establish the starting URL, then pass the resulting history object to the Router constructor or configuration.
createMemoryHistory(options)| Name | Type | Description |
|---|---|---|
options | MemoryHistoryOptions | Configuration object containing the initial history entries, such as an initial URL. |
A memory-backed router history instance.
initialEntries to provide the starting URL for the history.import { createMemoryHistory, createRouter } from '@tanstack/react-router'
const history = createMemoryHistory({
initialEntries: ['/'],
})
const router = createRouter({ routeTree, history })
Creates an in-memory history and supplies it to a TanStack Router instance.
Automated router tests need a controlled starting location without changing the browser URL.
The application runs outside a browser and still needs client-style navigation.
Routes should change without exposing navigation through the browser URL.
createMemoryHistory provide?It creates a history type that keeps navigation entries in memory instead of using the browser's URL history. The returned history can be passed to createRouter as its history option.
Follow-up: Why is it useful in tests? It lets tests set deterministic starting entries without depending on a browser location or changing the real URL.
Pass an options object containing initialEntries, such as ['/'], to define the initial URL represented by the memory history. Then give the returned instance to the router configuration.
Follow-up: What would happen if the initial entry were a nested route? The router would begin from that path, subject to the route tree and its matching behavior.
Memory routing is appropriate when there is no browser environment or when components should not interact with the URL. Typical examples include tests and other non-browser execution contexts.
Follow-up: How does this differ conceptually from browser history? Browser history integrates with the browser URL, while memory history retains navigation state in memory.
createBrowserHistorycreateHashHistorycreateRouterRouterHistory