-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Enzyme matchers do not match correctly children of React Fragments #1799
Comments
Since |
I'm also curious if reversing the order of the assertions to be expect(wrapper).toIncludeText('Bar');
expect(wrapper).toIncludeText('Foo'); will cause the same assertion to fail, i.e. is this always failing on the second assertion or on the text of the second child? |
That’s also a very good question. There’s some issues with statefulness, shown in #1781, that may be related, if so. |
I'm able to reproduce this for |
I've pushed up failing test cases; the issue seems to be that the adapter's I'm not sure what the best option is - perhaps including the fragment in the tree and flattening it only where needed? or perhaps overloading nodeToHostNode to return an array, or better, a DocumentFragment, in this case? cc @madicap |
@ljharb, sorry for the delay! It seems to me that you have already identified the source of the issue, but I'll still reply here with what I can figure out in case that's useful:
Note that I am highly unexperienced with Enzyme yet, and it's likely something it misconfigured on our end, but for some reason this gives me
By adding: console.log('>' + wrapper.text() + '<'); I get:
Reversing the order of the assertions causes the Let me know if there is anything else I can test, or if there is a workaround we can apply for now (replacing the Thanks! 🙏 |
I don't think there's a workaround at the moment. |
@astorije I'll try to see what I can do about this over the weekend |
That's great to hear, thanks @madicap! |
Hey @madicap, did you end up being able to look into this? :) |
I'm pretty much having the same issue, happy to help with fixing it if you haven't already done so @madicap Additional info:
Seems to be affecting |
@ljharb I'll look into this and see if return |
I believe @madicap is still working on it; anything based on DocumentFragment would only work on mount. |
I believe the branch they have in progress will lay the foundation required to render arrays, yes. I'd prefer to wait until that lands before exploring that direction further. |
[Fix] `mount`/`shallow`: `.text`/`.html`: handle an array of nodes properly [enzyme-adapter-react-{16,16.1,16.2,16.3}] [New] Make nodeToHostNode handle arrays Fixes #1799
With Enzyme v3.9.0, I am now getting no text out of
@ljharb, is there anything I'm missing here? E.g. |
@astorije be sure your react adapter is updated to the latest, otherwise it won't be able to handle fragments. |
Yeah I thought about that initially, but all dependencies (including the react adapter) are up-to-date 🤷♂️ |
@astorije in that case, can you file a new issue, and fill out the entire issue template? |
Will do, now that I have a reproducible case. |
Now that we have react hooks, this is becoming more of an issue. We have a file with hooks and it has React.Fragment and we need to test more than just the first node in the fragment. Currently, hooks can only be tested with mount. The link provided above is a different problem/solution. In my ideal world, we would be able to use shallow to test hooks, at which point this bug becomes mute. The only place in our repos that we mount is for hooks. Is this something that is coming soon?
|
That’s up to Facebook; there’s an open issue for the shallow renderer to support useEffect. |
Since this is an issue still, can we either open this issue until it is fixed or point me to an issue I can watch for this fix? |
See #2086 and facebook/react#16168 |
Describe the bug
This is a dedicated issue corresponding to a comment I initially left at #1213 (comment).
When using React Fragments, it looks like only the first child is being picked up by the matchers.
To Reproduce
Assuming the following code:
And the following test:
Bar
simply does not get rendered:mountWithIntl
is a simple wrapper that adds anIntlProvider
(fromreact-intl
) to help us deal with i18n in our tests. If I remove it and replace with baremount
, I get:I get the same results if I swap the
<>...</>
shorthand with<React.Fragment>...</React.Fragment>
.However, if the code is:
then now the tests pass fine.
For what it's worth, the built code for
Foobar
(built withtsc
) looks like:Expected behavior
I'm very confused as Enzyme 3.5 has Fragment support. Do you see something just obviously wrong above?
I'm wondering if the TypeScript compilation doesn't compile into something that's causing #1178. Or I'm just doing something wrong...
Additional context
[email protected]
[email protected]
[email protected]
and[email protected]
(outside of this project I know)The text was updated successfully, but these errors were encountered: