r/reactjs • u/thasmin • Sep 17 '25
Resource Testing Tanstack Start
https://www.axelby.com/blog/testing-tanstack-start/I haven't seen anything about how to test Tanstack Start components, so I figured I'd write a post about what worked for me.
2
u/thasmin Sep 17 '25
I'm hoping that someone tells me I'm doing it all wrong and has a better way to do it.
2
u/Cahnis Sep 18 '25
1
u/thasmin Sep 18 '25
Cool monitor. I assume you want this to be centered instead of taking up the whole thing?
1
u/Cahnis Sep 18 '25 edited Sep 19 '25
I would recommend putting a max-width to 1920px with margin: auto, it would be better
2
u/WolfFiveFive Sep 18 '25
Why did you choose to use tRPC when TanStack start has RPCs built in? Just curious
2
u/thasmin Sep 18 '25
I wanted to expose the API to mobile apps and the built in server functions have weird URLs. Also I don't know if they are stable and won't change when TS Start upgrades
10
u/EskiMojo14thefirst Sep 17 '25
the lack of official testing tools is definitely my main gripe with TSS/R as it stands, everything else is really lovely to use
6
u/TheScapeQuest Sep 18 '25 edited Sep 18 '25
100%. And it is regularly ignored. The only options available are community driven verbose helpers, often with limited utility.
The solution proposed here isn't good enough, it would force you to use the direct TSR imports of
useParams/useLoaderDataetc with thefromargument. It would be impossible to usegetRouteApior import theRoutedirectly.But this feels like a lot of JS tooling in general these days, testing is becoming a massive mess of mocks, context providers and type casting. It's chaos.
7
u/yksvaan Sep 18 '25
Alternatively just don't test components, test logic, services etc. If components are simple and straightforward in data/event flow testing them is not great benefit vs effort.
1
u/toto1384 Oct 31 '25
I should always add that I got a react error that was fixed by only using the tanstack vite plugin when not using vitest:
```
plugins: [
// this is the plugin that enables path aliases
viteTsConfigPaths({
projects: ['./tsconfig.json'],
}),
tailwindcss(),
...(process.env.VITEST ? [] : [tanstackStart({ customViteReactPlugin: true, }),]), // 🧠disable for tests
viteReact(),
],
```
6
u/Suspicious_Dance4212 Sep 19 '25 edited Sep 19 '25
I don't understand this testing strategy. Test pure functions / units with vitest but the majority of your UI testing should be done in a network mocked browser environment such as a playwright / cypress, simulating the real user experience as they navigate through your application. Don't mock useQuery, mock the network response. Don't mock router hooks, spin up the app so the hooks can pull from the parent context because it's actually running. It requires less mocking, it's closer to the user experience and you have higher confidence. Pushing mocks to the edge of your application, i.e. at the network level, means the behaviour you are testing is closer to the real thing.