-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Cannot unit test components which use <Link />
due to missing Router context
#4506
Comments
<Link />
due to missing Router context
Closing this issue as it's already tracked in #4488 |
Thanks for pointing this out. I did saw the discussion and even your repo but to be honest, I though it's more relevant to all cases, in which loader or actions need to be tested. Nevertheless, I gave it a try and yep, it does work for me. I assume with this setup I can now even test pages to some degree. |
Glad it's working for you! Yes you should be able to test everything jsdom supports, main limitations are browser navigations are best left to playwright/cypress and form submissions have a few bugs in jsdom. You can test the rendering of entire nested routes though as shown here. |
What version of Remix are you using?
1.7.4
Steps to Reproduce
Use test setup from Blues Stack:
Use Vitest
0.24.4
(perhaps Jest also) and write a simple unit test for a pure react component, which uses<Link />
component from@remix-run/react
.Current test:
I saw that one could pass additional config to
render
function with the actual router component as follows:The problem is that in this case
BrowserRouter
must be imported fromreact-router-dom
package, which we don't need in a Remix app. But even with this approach, after adding this package to dev dependency, it doesn't solve the issue. ImportingRemixBrowser
doesn't work either, but due to other errors.For some reason, the second approach (wrapping component with react router's
BrowserRouter
) seems to work after changing environment option invitest.config.ts
fromhappy-dom
tojsdom
.Expected Behavior
Test is green.
Actual Behavior
The text was updated successfully, but these errors were encountered: