-
-
Notifications
You must be signed in to change notification settings - Fork 10.6k
RouterProvider not working in tests (Vitest) #12512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I am literally having this same issue! Weird part is all the test work fine in local, once I load them to the CI/CD that's when I see if fail. |
In a similar case, adding package.json {
...
"dependencies": {
...
"react-router": "7.1.1",
"react-router-dom": "7.1.1",
}
} ...test.tsx import { createMemoryRouter, RouterProvider, useNavigate } from "react-router-dom"; |
@hermaneyd That's the old package and is only used for a smooth upgrade. |
Hey, I have the same issue upgrading one of my projects. I've just fixed it updating my imports to use in all cases It might be something on how vite treat the modules, but yes is very weird 🤔 I'm using the |
So I think the linked PR should fix our end of this and stop bundling
This usage of both CJS/ESM files is causing duplicate React contexts which is the source of the error. I'll keep digging but unsure if this is still something on our bundler end or something about the |
@brophdawg11 I just installed |
oh - weird. Let me try again in my clone - maybe I messed up my install. I was doing a lot of local copying around of built files and such but I thought I did a fresh npm install of the experimental at the end. |
Hm - yeah I'm still getting the error. If you pull my branch fresh and https://github.com/brophdawg11/repro.react-router-provider-vitest/tree/rr-experimental |
I don't get the original context error anymore with your branch. I get an error from testing library but that's just because of the missing clean up, adding The warning |
That's super odd - I still get it on a fresh clone of my fork branch. Going to check in with a few other team members and see what behavior they get and if we can figure out why it would still be failing for me |
I can confirm @rothsandro, I cloned @brophdawg11's repo, switched to the ![]() |
@brookslybrand Hm, weird. Could a different node/npm version be the problem? |
Looks like it depends on the node version, just tested it with different versions. |
Is this the same issue as #12785? |
@markdalgleish Hm not sure. The other issue mentions two conditions which don't apply here:
|
@markdalgleish I do think this is related to #12785. With the fix in #13497, the test no longer fails on node 22.11 and continues to fail on 22.12. I just confirmed against a fresh experimental release on my form branch: https://github.com/brophdawg11/repro.react-router-provider-vitest/tree/rr-experimental You can clone that and: > n 22.11 && npm ci && npm run test ## ✅
> n 22.12 && npm ci && npm run test ## 💥 Therefore, I'm going to merge my linked PR and close this ticket out as resolved and we can continue to track the node 22.12 issue in #12785. |
@markdalgleish To clarify - I think this bundling issue was standalone and caused breakages on node <22.12. Once we fixed that, I think we now exposed the same issue as #12785 where node 22.12 and above seem to end up with both CJS and ESM modules being loaded. |
🤖 Hello there, We just published version Thanks! |
I'm using React Router as a...
library
Reproduction
npm install
npm run test
There is a simple test that renders the
RouterProvider
with a memory router.We then click on a button that triggers a navigation with
flushSync: true
using theuseNavigate()
hook.System Info
Used Package Manager
npm
Expected Behavior
The test should run successfully and without error or warning.
Actual Behavior
The tests throws an error:
The upgrade guide mentions that for non-DOM the router provider should be imported from
react-router
instead ofreact-router/dom
. The repro uses Vitest with happy-dom so it's a DOM environment. There is a 2nd test that uses the router provider fromreact-router
. This solves the error from above but will log a warning to the console that the router provider fromreact-router/dom
should be used forflushSync
(skip the failing test to see the warning).The text was updated successfully, but these errors were encountered: