-
Notifications
You must be signed in to change notification settings - Fork 47.7k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Allow ReactShallowRenderer to expose JSX for test debugging #4835
Comments
Yes investigating JSX diffs while using expect for example is currently a bit tricky. I always go to debugger. |
Rather than bake this into React, it seems like something a custom test/assertion reporter could do. |
There's one for jasmine: https://github.com/rogeriochaves/jasmine-react-diff, I am currently trying to understand how this could be ported to expect + mocha for instance (mjackson/expect#37) |
See also: https://www.npmjs.com/package/inspect-react-element
|
Awesome thanks for curating this @gaearon, will be useful to me, I am going to implement it in expect |
Hmm unexpected seems really nice, thanks a lot |
This looks fantastic - thanks so much! I will be investigating all of the above. 👍 |
So I did:
It was cool, hope this will be usable for a lot of other people. Since I have been testing with react shallow renderer, comparing using toEqualJSX is a joy. We will write a blog post soon about all this. |
this has been a pain point, thanks so much 👍 |
@juliankrispel share the ❤️ then please ;) |
you bet @vvo |
Jest is using https://github.com/facebook/jest/tree/master/packages/pretty-format which supports printing React elements. import prettyFormat from 'pretty-format';
const {ReactElement, ReactTestComponent} = prettyFormat.plugins;
// in a test
const renderer = // ...
// ...
const element = renderer.getRenderOutput();
const formatted = prettyFormat(element, {
plugins: [ReactElement],
printFunctionName: false,
});
/*
<button
onClick=[Function]
>
Hello World
</button>
*/ |
Yes, we just use jest snapshots nowadays :) |
Hi,
I've been making use of ReactShallowRenderer for testing. I really like it. You mention in your docs that you would appreciate the React community's feedback on how it should evolve; here's a suggestion!
The one problem I have is when it comes to investigating failing tests.
First a little digression. I'm just using vanilla Jasmine for my testing. When it comes to deep objects not matching up I've been using a custom matcher I wrote called
toEqualAsObject
:This matcher builds on jsondiffpatch to help you to isolate where the difference between 2 objects lie. This is really useful for general testing but doesn't help particularly when it comes to JSX testing like this:
The reason is, a subtle difference in JSX can result in a fairly different objects being constructed. Comparing them is not massively revealing and investigation can take some time. I've a suggestion: It would be really handy if you could extract a JSX representation of an object from the renderer output. What I'm imagining is the ability to do something like this:
Which would would produce a string like this:
This would allow the writing of a custom matcher to be used when comparing JSX which would, upon failure, use the
asJSX()
to report a helpful error message. I've spent more time than I would like digging through differences in object structure when investigating failing tests and think that a feature like this could be a real win.What do you think?
The text was updated successfully, but these errors were encountered: