Ability to lowlight or omit a difference between undefined and omitted values in expectation failure diff
See original GitHub issue🚀 Feature Proposal
I would like for undefined properties of an object to be de-emphasized from the diff presented after an equality match failure.
Motivation
I have run into many properties being present, but set to undefined in the subject of an assertion, and do not wish to replicate them in the expected value. The default equality testing mechanism does not care about a key which is missing on one object and set to undefined in another, the difference between a missing key and the undefined key is visually highlighted, making me need to sort through visual clutter to get to the actual differences.
Example
When executing this failing test:
expect({one: "two", three: "four", five: undefined}).toEqual({one: "two", three: "broken"});
The output in the console would lowlight the difference between the two objects regarding the key five
being missing in one object and set to undefined explicitly in the other. The lowlight would perhaps be similar to how the one
key is grayed out, or perhaps the diff would even be omitted.
Pitch
I am uncertain that this needs to belong in the Jest core platform, simply because I’m not sure if the diff output is a result of the reporter or of some more intrinsic piece of Jest. If I’m wrong about it belonging here, let me know so I can better contribute in the future!
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (3 by maintainers)
@rickhanlonii I definitely think that is possible, seeing that there is a difference between the two:
My business just happened to frequently be adding these undefined values, which didn’t matter since JSON.stringify would later omit the key.
Having thought it through with a bit more distance from the problem I was having, I’ll revise my position to:
Since keys set to undefined behave differently (as illustrated in snippet above), if you don’t want them to be highlighted, strip them manually!
Something like:
Or, more precisely for the case I had:
which, again, is in user land, so I will close this feature request.
Thanks for the feedback @rickhanlonii !
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.