Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve error.showDiff behavior for multiline strings #711

Closed
wants to merge 1 commit into from

Conversation

millermedeiros
Copy link

previously error.showDiff was conflicting with strings, so diff wasn't escaping invisible chars and also wasn't displaying line numbers.

now we make sure JSON.stringify() is only called if error.actual and/or error.expected aren't strings.

Before:

before

After:

after

previously `error.showDiff` was conflicting with strings, so diff wasn't
escaping invisible chars and also wasn't displaying line numbers.

now we make sure `JSON.stringify()` is only called if `error.actual` and/or
`error.expected` aren't strings.
@travisjeffery
Copy link
Contributor

This should be fixed by 3cb9295

@rstacruz
Copy link
Contributor

rstacruz commented Sep 1, 2014

I think this needs to be revisited. Right now (v1.21.4 @ August 2014), the before output is what gets shown. This PR will actually work. 👍

@rstacruz
Copy link
Contributor

rstacruz commented Sep 1, 2014

To further elucidate, the issue here is that comparing two Strings will always get them JSON.stringified (actually utils.stringify now), which will always make the strings single-line. Using diff.diffWithWords will not help.

The lines in question look like this in v1.21.4:

    if (err.showDiff && sameType(actual, expected)) {
      escape = false;
      err.actual = actual = utils.stringify(actual);
      err.expected = expected = utils.stringify(expected);
    }

/* ... later ... */

function sameType(a, b) {
  a = Object.prototype.toString.call(a); /* returns something like "[object Object]" or "[object String]" */
  b = Object.prototype.toString.call(b);
  return a == b;
}

...I can't help but feel like the use of sameType isn't warranted here. This PR replaces that with a check that will only use .stringify if either the expected or actual objects are NOT strings, which IMHO makes more sense.

@rstacruz
Copy link
Contributor

rstacruz commented Sep 1, 2014

Digging a bit further, the use of sameType() here is linked to #900. the typeof actual != 'string' || typeof expected != 'string' will fix the same issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants