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.

Show word diffs in strings when they are properties on an object

See original GitHub issue

🚀 Feature Proposal

I have some snapshots which are pretty chunky strings of markdown

Screen Shot 2020-05-01 at 11 29 49 AM

I’d like to be able to more quickly see where the string differ! This is already supported when directly diffing strings (expect('a b').toEqual('a c') or expect('a c').toMatchSnapshot()), but not for strings within other structures.

Motivation

I’d like a clue where the difference starts

Example

Any snapshot

Pitch

It’s a nice to have! 😄

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
pedrottimarkcommented, May 4, 2020

Thank you for giving this realistic example of the need for substring differences.

For snapshots of serialized objects or elements, my tests of substring differences have been sadly divided almost equally between clear and confusing results.

Therefore, I have been working on non-snapshot deep equality assertions:

  • toEqual (transform comparison tree to omit strict changes, see step 1 below)
  • toHaveProperty
  • toMatchObject (transform comparison tree to omit irrelevant changes)
  • toStrictEqual

Here is an overdue progress update on the relevant comparison project. Over Thanksgiving weekend, it hit me that prettier provides a pattern for data-driven differences. I have adapted its intermediate description tree and printing loop for steps 2 and 3:

  1. compute comparison tree according to strict criteria with 11 node types:

    • 'changeCollection' for example, changes in array, object, map, set
    • 'changeConstructor' for strict deep equality
    • 'changeKey' for example, from className to class
    • 'changeString1' for one-line string
    • 'changeString2' for multiple-line strings
    • 'changeType' for example, number and string, or object and nullish
    • 'changeVal' for example, unequal primitive values
    • 'commonCollection' for example, no changes in array, object, map, set
    • 'commonVal' for example, equal primitive values
    • 'delete' for example, expected object has property but received does not
    • 'insert' for example, received object has property but expected does not
  2. build description tree

  3. print lines with colors for indent size and line width

Substring differences: baseline at left and improved at right:

9940 substring

A goal of the description is valid ECMAScript, if you omit the + received lines that are aligned under a - expected line

--no-expand option: baseline at left for 5 context lines and improved at right for 2 context units:

9940 context

The next major step is to decide on an interface for the description plugins which provides enough information to keep improving the --no-expand option in the future.

Your thoughts, questions, or concerns are welcome.

1reaction
jeysalcommented, May 2, 2020

Just tried it out, it works for snapshots that are strings, but not for string properties in other structures being snapshotted. @orta mind changing the issue to be about extending word diff support to strings nested in other structures (snapshot or toEqual doesn’t matter, not related to snapshots) and we can stick a help wanted label onto it?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Compare a String to a Key in a JavaScript Object
A better way is to test directly that the string is a property on the Object with Object.prototype.hasOwnProperty() . The .
Read more >
Compare-Object (Microsoft.PowerShell.Utility)
The Compare-Object cmdlet compares two sets of objects. One set of objects is the reference, and the other set of objects is the...
Read more >
git-diff Documentation - Git
Show a word diff, using the <mode> to delimit changed words. By default, words are delimited by whitespace; see --word-diff-regex below. The <mode>...
Read more >
Visualization: Column Chart - Google Developers
Like all Google charts, column charts display tooltips when the user hovers ... To specify properties of this object, you can use object...
Read more >
Object references and copying - The Modern JavaScript Tutorial
One of the fundamental differences of objects versus primitives is that objects are stored and copied “by reference”, whereas primitive ...
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