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.

.should('deep.equal') does not show a full diff comparison on failures

See original GitHub issue

Current behavior:

Cypress does not show enough information to know what is the difference when deep comparing complex objects.

Example:

cy
  .wrap({ foo1: { bar: { foo1: { bar: 1 } } }, foo2: { bar: 1 } })
  .should('deep.equal', { foo1: { bar: { foo1: { bar: 1 } } }, foo2: { bar: 2 } })

Result:

Screen Shot 2019-04-30 at 10 29 33 PM

Desired behavior:

Ideally, I’d like cypress to show a complete diff of the compared objects so we can easily spot the difference.

Versions

cypress@3.2.0

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:22
  • Comments:18 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
franzvezulicommented, Feb 26, 2022

@braveheart55 @tsvetan-ganev @gurudattgd04 If you are interested, here is a work-around that a friend of mine (@Bushwazi) came up with that we are using:

In our /support/index.js: we intercept and override our chai expect(expected).to.deep.equal(actual) lines across all of our tests

import { diff } from 'json-diff';

/*
 * Wraps/overwrites the "equal" method so that we can pretty print the diff
 * during errors only.
 *
 * The diffing library adds a "~" JSON key when looking at arrays, but it
 * doesn't affect anything else.
 */
chai.use(function (_chai, utils) {
  utils.overwriteMethod(chai.Assertion.prototype, 'equal', function (_super) {
    return function newEqual(str) {
      let obj = utils.flag(this, 'object');
      // "equal" has a "deep" flag it "deep" precedes it
      let deep = utils.flag(this, 'deep');

      try {
        _super.apply(this, arguments);
      } catch (error) {
        // If we do not have the "deep" flag, just throw the standard error
        if (deep !== true) {
          throw(error);
        }

        // If we do have the "deep" flag, use the diff module for highlight the diff
        // and then tidy it up a bit
        let diffObject = "expect objects to match:\n";
        diffObject += JSON.stringify(diff(str, obj), null, 4)
          .replace(/__old/g, "-")
          .replace(/__new/g, "+");
        throw(diffObject);
      }
    }
  });
});

Notes

  • This only gets printed during an assertion failure.
  • This uses this JSON diff library: https://www.npmjs.com/package/json-diff
  • You can get a prettier diff (colorized) if you use console.log() and check the console instead of in-line printing it in the test runner as well.

It will look like this:

image

I’ve also been following this issue for 3 years+.

I understand Cypress is geared more towards UI-testing than API-testing (For example, I don’t need a ViewPort if I’m just testing a JSON REST API, yet there is no flag to turn this off), but they really should start adding some quality of life changes for API-only testing.

It would be great if there was an API_ONLY_TESTING flag or something similar. I bet they can turn off a bunch of UI-only functionality – and improve Cypress performance and responsiveness if you are just testing a REST API.

5reactions
jennifer-shehanecommented, May 1, 2019

Showing a more detailed diff of objects in the error message is part of this larger issue, that is currently in progress: https://github.com/cypress-io/cypress/issues/3762 You can see an comment on our planned designs there.

Will leave this open as part of that epic.

You can also click on the Assert command in the Command Log to see a fully diff within your DevTools console.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there any way to see a diff on a deep equal assertion ...
It would be fantastic to actually see the differences instead of seeing: expected [ Array(3) ] to deeply equal [ Array(3) ]. I...
Read more >
Can't Cypress tell me what the exact difference is?
When I run a test via the Cypress GUI I get a report (left in the image) and it tells me that my...
Read more >
Comparing objects and sequences - Testfixtures
When using compare() frequently for your own complex objects, it can be beneficial to give more descriptive output when two objects don't compare...
Read more >
Assert | Node.js v19.3.0 Documentation
For example, assert.deepEqual() will behave like assert.deepStrictEqual() . In strict assertion mode, error messages for objects display a diff. In legacy ...
Read more >
API Reference
'bar', 'foo is not bar'); assert(Array. ... isNotOk('everything', 'this will fail'); assert. ... Assert that actual is not deeply equal to expected ....
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