router mock for tests and stories
What's the best way to have a mock implementation of the router? For example:
- we have components that we want to render in storybook, but they depend on
useParams()
. Ideally, we'd just wrap it in a Provider with a given path.
- we have cypress component tests, where we mount a component and then interact with it. It might e.g. click a link, but it shouldn't really navigate to that route (because the test is scoped to a component). Ideally, we'd set the navigate
method of the router to a spy.
In nextJs, we had next-router-mock
, a 3rd party package that would mock the router via a Provider. It worked okay (but not great). Is there anything for the TanStack Router? Thanks 🙏3 Replies
automatic-azure•17mo ago
Right now, I think the easiest thing is to create a minimal router with the route you need to render. For jest unit tests, I have a
renderWithTanStack
helper function to wrap the component under test. I pass in initialEntries
to set the current url in createMemoryHistory
and a route id. I parse out the route id and make routes using code based routes.
That code is still a work in progress. Right now you can theoritically pass in an array of route ids, but it won't work without some dedup logic to prevent routes with the same id/path being created.
You can see in the tests in the repo that they are also just creating routes using code based routing as well.sensitive-blueOP•17mo ago
oh that looks nice, thank you
conscious-sapphire•8mo ago
Small necro, but just wanted to say super thanks for this.
It plagued us a lot in our team with the testing in vitetest.