Comments (12)
So the mistake I was making is obvious in hindsight but was hard to spot. I was mounting a subset of my app that is normally (outside of tests) wrapped in a parent route. So when I mapped through those components/routes in the test, they weren't wrapped in their parent route and so their routes never matched any href from any of the <Link>
s. Once I realized that and mounted the top-level component with the parent routes (<Fragment>
s), the rest of the process was easy. Turns out, I also needed to set the jest testURL
for jsdom (otherwise it blows up when you try to push a route while on "about:blank").
Thank you for offering encouragement, by the way! I admire people who can behave that way in a context where it's far easier to be apathetic or annoyed 😄
from react-testing-library.
You can actually turn a codesandbox into a local project :)
from react-testing-library.
Works locally too :)
from react-testing-library.
Oh, yes, you definitely need jsdom for this library to work. If you're using Jest, you'll need to set the testEnvironment
to jest-environment-jsdom
.
I'm guessing that's the issue. Feel free to continue asking questions and folks can help when and where they can. But that's probably the problem. Good luck.
from react-testing-library.
Well, I'm at a loss. I updated the codesandbox to reflect this scenario and it works in the browser and locally. I'm going to try to strip down my failing example and discover what the problem is. Sorry for all the noise on this issue.
from react-testing-library.
I'm glad you worked it out! If you'd like to add an example to the tests that'd be welcome!
from react-testing-library.
Any chance you could try to reproduce this in a codesandbox like this one? Then we could probably help you better.
from react-testing-library.
No problem, I created one here. Sorry my initial issue was a little scant on details--I wasn't sure if this was the right repo to create it in.
I had been thinking that this may be due to running in node vs the browser before submitting the issue, and now I see that it works in codesandbox, which I think confirms those suspicions (it just bundles the code using its crazy magic and then executes that in the browser, right?). I'm going to make a standalone project out of that sandbox, and then I'll share it here.
from react-testing-library.
What! This makes no sense.
So now I'm wondering what the difference is between running tests via react-tests and running them in my project using jest.
I see this in the test script from the codesandbox project: "test": "react-scripts test --env=jsdom"
. I wonder if jsdom is what's making it work?
from react-testing-library.
On a sidenote, I'm once again blown away by how awesome codesandbox is 😄
from react-testing-library.
Thanks for your help so far! I tried your suggestion, and I still get issues, but I got closer to what the actual issue is. I'll need to revise my codesandbox and possibly re-open this if that's OK. The problem only happens if I return <Fragment>
s from a function within the render of a component. Here's a snippet of the kind of code that fails:
{pages.map(({ route, Component }) => (
<Fragment key={route} forRoute={route}>
<Component />
</Fragment>
))}
Each item in pages
is an object that just has a string ('route') and a component ('component'). Each one of those components is a connected component.
I'm going to make a new codesandbox and see if I can repro this now that I know more about the scenario that causes it.
from react-testing-library.
No worries. Keep at it. You can do it!
from react-testing-library.
Related Issues (20)
- ByRole have inconsistent results (getting h2 instead of button)
- ReactElement not assignable HOT 3
- Property 'toBeInTheDocument' does not exist on type 'JestMatchers<HTMLElement>' HOT 9
- queries returned by render are not scoped HOT 4
- Render function's typescript type excludes valid react component types
- Types are breaking HOT 12
- findBy* query fails but waitFor passes HOT 2
- Intial props not working when rendering a hook (or I've done something wrong.) HOT 5
- Snackbar not auto closing HOT 2
- Test results and "screen.debug" do not match
- Unexpected element not found when adding accessibilityElementsHidden or importantForAccessibility HOT 2
- SyntaxError: Cannot use import statement outside a module when using render or .tsx files HOT 2
- use rerender props in wrapper HOT 1
- Linting fails as "react-test-renderer" functions are interpreted as functions from "testing-library/react" HOT 2
- Calling render on a component that throws, results in 4 renders HOT 1
- React Testing library `unmount` is not working HOT 1
- Spurious `not wrapped in act` warnings from user-event, react, dom mismatch HOT 4
- Support for `onCaughtError` and `onUncaughtError` on root HOT 1
- Module parse failed: Unexpected token for the act-compat.js HOT 1
- asyncWrapper is not executed with act support HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-testing-library.