Skip to content
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

[Bug]: useNavigate is failing when running in react testing library on react-router v7 #12368

Closed
kelsin opened this issue Nov 24, 2024 · 5 comments
Labels

Comments

@kelsin
Copy link

kelsin commented Nov 24, 2024

What version of React Router are you using?

7.0.1

Steps to Reproduce

https://stackblitz.com/edit/vitejs-vite-9tur3x

I created this tiny example to show this error. This app just has two routes and a button to swap between them on each page using useNavigate. The app itself works with a memory router or a browser router.

The tests however don't run with either router. You can run the tests in the above environment with npm run test.

Here is a working version (all the same) on react-router v6.28.0: https://stackblitz.com/edit/vitejs-vite-sygeg8

Expected Behavior

The test is able to render the main page and read the text on it.

Actual Behavior

We get this error:
Error: Uncaught [Error: useNavigate() may be used only in the context of a <Router> component.]

The app doesn't render so the test doesn't pass.

Info

I saw this tonight while trying to update my react app from 6.28.0 to 7.0.1. This only occurs in tests in react testing library. My larger app behaves identically. This bug only happens in testing and regardless of if I use createMemoryRouter or createBrowserRouter.

@kelsin kelsin added the bug label Nov 24, 2024
@jessemyers-lettuce
Copy link

We see a similar error. It's not apparent using the dev server, only after a production build and deploy.

@timdorr
Copy link
Member

timdorr commented Nov 24, 2024

Simple fix. Change your RouterProvider import to this:

import { RouterProvider } from 'react-router';

@timdorr timdorr closed this as completed Nov 24, 2024
@kelsin
Copy link
Author

kelsin commented Nov 24, 2024

Simple fix. Change your RouterProvider import to this:

import { RouterProvider } from 'react-router';

Sounds good. This ties into my other points here: #12370

Should update https://reactrouter.com/upgrading/v6#upgrade-to-v7 as well since that's where I was told to use the current import.

@timdorr
Copy link
Member

timdorr commented Nov 24, 2024

Thanks, I added that now.

@rothsandro
Copy link

The tests run with jsdom so it's not a "not-DOM context" as mentioned in the docs. Importing from react-router breaks other use cases, e.g. when navigating with flushSync: true:

You provided the flushSync option to a router update, but you are not using the <RouterProvider> from react-router/dom so ReactDOM.flushSync() is unavailable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants