You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
React Router has built in removal of server code when running a client build, which works fine when running the dev server or production builds.
I am having an issue when using Vitest browser mode to try and test the Component (aka the default export) for a route entry point. In this scenario, Vitest imports the file without the transformation, thus it keeps the server side imports in even though they will not be tested in this environment. Even though the tests don't call the loader and action exports, the tree shaking does not strip them so any imports that are server-side only (e.g. Prisma, session handling etc.) get imported which breaks the tests because they call Node-only functions. I only want to be able to test the Component in isolation, to test that it behaves as expected for given loaderData and actionData.
There are alternatives to test this:
Run the tests in Node using jsdom etc., but this means that browser mode can't be used which undermines the whole point of it
Split out the component into a separate file and call this from the route entry point. This is suboptimal as a key feature of RR7 is that the component and the backend functions can be in the same file.
Mock everything - but this seems like a poor way to handle it
I dug into the RR Vite plugin and saw that it does some transformation with the presence of ?__react-router-build-client-route, so I tried manually triggering this in my browser test case with
But this does not seem to trigger the server code removal e.g.
Error: PrismaClient is unable to run in this browser environment, or has been bundled for the browser (running in ``).
I've tried various configurations of the testTransformMode to toggle SSR on and off without luck. I tried forcing the test environment to "client" which is referenced in the Vite plugin source - doesn't work. I feel like there should be some way via config to trigger the correct mode for the RR Vite plugin, without reaching for the next step which would be some sort of custom Vite plugin which would reimplement this part of the RR Vite plugin.
Has anyone found a solution to testing React Router 7 routes with server-side imports in Vitest browser mode without splitting the component?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
React Router has built in removal of server code when running a client build, which works fine when running the dev server or production builds.
I am having an issue when using Vitest browser mode to try and test the
Component
(aka the default export) for a route entry point. In this scenario, Vitest imports the file without the transformation, thus it keeps the server side imports in even though they will not be tested in this environment. Even though the tests don't call theloader
andaction
exports, the tree shaking does not strip them so any imports that are server-side only (e.g. Prisma, session handling etc.) get imported which breaks the tests because they call Node-only functions. I only want to be able to test the Component in isolation, to test that it behaves as expected for givenloaderData
andactionData
.There are alternatives to test this:
I dug into the RR Vite plugin and saw that it does some transformation with the presence of
?__react-router-build-client-route
, so I tried manually triggering this in my browser test case withBut this does not seem to trigger the server code removal e.g.
I've tried various configurations of the
testTransformMode
to toggle SSR on and off without luck. I tried forcing the test environment to "client" which is referenced in the Vite plugin source - doesn't work. I feel like there should be some way via config to trigger the correct mode for the RR Vite plugin, without reaching for the next step which would be some sort of custom Vite plugin which would reimplement this part of the RR Vite plugin.Has anyone found a solution to testing React Router 7 routes with server-side imports in Vitest browser mode without splitting the component?
Beta Was this translation helpful? Give feedback.
All reactions