question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
byronandersoncommented, Dec 4, 2018

@rickhanlonii I definitely think that is possible, seeing that there is a difference between the two:

const object = {foo: undefined}
"foo" in object // true
"bar" in object // false
object.hasOwnProperty("foo") // true
object.hasOwnProperty("bar") // false
Object.keys(object) // ["foo"]

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:

expect(recursivelyStripUndefinedKeys(actual)).toEqual(expected);

Or, more precisely for the case I had:

expect(actual).toBeJsonEquivalentTo(expected);
// expectation will pass if serializing `actual` with JSON.stringify and then deserializing it with JSON.parse will make it equal to `expected`

which, again, is in user land, so I will close this feature request.

Thanks for the feedback @rickhanlonii !

0reactions
github-actions[bot]commented, May 11, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

5 snippets to differentiate undefined and omitted values in ...
This post shows several ways to distinguish between the undefined and omitted variables in several situations. Suppose that in all these ...
Read more >
PVsyst 7 Release notes
- System: new tool that allows viewing the PV module strings and inverters in the PV system in the form of a circuit....
Read more >
The Clinical Importance of Assessing Tumor Hypoxia - NCBI
Tumor hypoxia is a well-established biological phenomenon that affects the curability of solid tumors, regardless of treatment modality.
Read more >
SoftMax Pro Software Formula Reference Guide
This document is broken into four main sections: This Introduction provides a general introduction to formulas. Operators on page 15 describes the different...
Read more >
Why No Reference Metrics for Image and Video Quality Lack ...
NR metric developers claim Pearson correlation values between ... “good quality” means to different users and how this impacts.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found