-
-
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
Provide access to the "unrendered" element #2544
Comments
I'm not entirely sure I understand. enzyme doesn't support or endorse snapshot-based testing; it's brittle and leads to folks rubberstamping changes they shouldn't. The closest we offer is |
I agree with this sentiment but some of our (Emotion) users depend on that.
<Fragment>
<Noop />
<Greeting className="css-6ph63u-cases">
Hello
</Greeting>
</Fragment> It seems that it is basically stringifying the rendered nodes of the shallow wrapper. I'm after what is called "unrendered" in the codebase here.
The problem is that with the upcoming React 18 we must wrap our elements in a Fragment to keep the number of siblings and stuff matching between the server and the client. This avoids However, this affects serialized snapshots because we add additional elements to the rendered React tree. Those elements are purely internal and ideally, they would be hidden from the serialized snapshots. In the past, this wasn't an issue (even though there was a Fragment there and stuff on the server) because the snapshots are usually captured in the JSDOM environment (so where we previously were avoiding rendering those "internal" elements). |
The entire point of snapshot testing is to expose all those internals. Why would it be ideal to provide a fictional snapshot? Doesn't that defeat the entire alleged purpose of having snapshots in the first place? |
(also, note that enzyme doesn't yet support react 17 at all yet, so "enzyme and react 18" problems are a very long way off, since all enzyme users are supposed to stay on react 16 until there's official support) |
In this situation, it seems to me that people won't care about the library internals - they should be focused on their code and adding the fragment wrapper etc to the snapshot is just unnecessary noise.
We are currently supporting While my use case might not be that appealing to you, this could be used for different use cases as well. I'm not asking about something specific to the whole serialization stuff that I'm dealing with - it's just some background on why I'm requesting it. I understand that you might be hesitant about adding this - just trying to make a case for myself here 😉 |
Totally fine to ask :-) I'm still just trying to understand the value. The root element is the thing you're testing. Since you're testing it, you are the author of it, you already know all the internal implementation details, and all of those details are relevant. It sounds like this is a scenario where someone's making a snapshot of a component they didn't author, which is confusing to me. |
This is the case here. With smth like this: /** @jsx jsx */
<div css={{ color: 'red'}} /> the user is thinking about the The content of the wrapper component is irrelevant to the user though and, IMHO, there would be little to no value in printing it in the snapshot. |
A custom jsx pragma means you’re not using react rendering - which means you’d need a custom enzyme adapter. The react adapters only work with the react jsx transforms. |
Emotion is using jsx pragma with React - we forward calls to React.createElement: So we dont quite need custom enzyme adapters at all. |
right - but emotion's transform does different things than react's does, which is why you're seeing undesirable results in the enzyme debug tree. The only reason you wouldn't need a custom adapter is if the custom transform didn't do anything, which would defeat its purpose. |
Hm, I'm not sure if I totally understand the point here. Maybe we could provide a custom adapter and thus gain control over this. It seems like a lot of work for a very little gain though - and also a problem for the users because we'd have to educate them to use that custom adapter over the "default" one. Emotion works OK with the regular adapter just fine - functional-wise. There is just this minor issue with the serialization output. We can actually handle this by using some heuristics - I would just prefer not to rely on guessing there and to be able to implement a straightforward logic. |
Right, I do see the difficulty you're in. I suppose we could come up with a way to configure the standard adapters with a custom jsx transform - but then your users would still need to configure their adapters with something like Certainly changing the default tree representation wouldn't be feasible. Exposing the internal UNRENDERED value is doable, but I'm very hesitant to do it prior to having working React 17 and 18 adapters, because i have no idea what kinds of internal changes will be needed for enzyme to support react 18. |
Well, I've raised this mostly because I had a need for this and this would make my life slightly easier. It isn't something I'm desperate for (I've also implemented an ugly, ugly hack to make this work for me) so I'm OK with this just sitting around for now.
Fair game. If you ever get to work on those you might have this ticket back in your head - if you forget about it, that is fine too 😉 |
Sounds good :-) Happy to leave this open, and we can revisit it once there's a React 17 and 18 adapter. |
Is your feature request related to a problem? Please describe.
For serialization purposes (like when working with jest serializer) I'd like to omit some internal elements rendered by component:
Note that in the context of the serializer plugin I only have access to the
ShallowWrapper
's instance here. This wrapper renders smth like:And this is what I have direct access to on the
wrapper
. I would really like to serialize this as:To avoid handling this in a false-positive manner I really need to know what element has led to this render. This is available as "unrendered" internally but this is not exposed publicly.
Describe the solution you'd like
It would be sufficient if you'd be open to adding a method like
unrendered
(or similar) to the API.Describe alternatives you've considered
N/A
Additional context
N/A
The text was updated successfully, but these errors were encountered: