TanStackT
TanStack2y ago
7 replies
primary-violet

How to unit test individual components that rely on routerState?

I'm attempting to unit test a component that relies on useMatchRoute.

Unfortunately I'm met with
TypeError: Cannot read properties of null (reading '__store')
 ❯ Module.useRouterState node_modules/@tanstack/react-router/dist/esm/useRouterState.js:9:98
     10|   useRouterState
     11| };
     12| //# sourceMappingURL=useRouterState.js.map
       |                                                 ^
     13| 
 ❯ Outlet2 node_modules/@tanstack/react-router/src/Matches.tsx:307:41

Typically I would have a general wrapper that includes the Router with Routes, but this approach doesn't exactly work. I've referenced other Vitest rendering an empty div when RouterProvider is used, but that also doesn't work. Having the flexibility to test components as is would be nice without having to mock hooks that react-router provides
Discord
Discord is the easiest way to communicate over voice, video, and text. Chat, hang out, and stay close with your friends and communities.
Was this page helpful?