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

showDiff: don’t stringify strings + tests #1477

Merged
merged 1 commit into from
Dec 21, 2014

Conversation

marcello3d
Copy link
Contributor

The showDiff feature is stringifying strings as JSON, which flattens multi-line text with \n and surrounds them with quotes.

I refactored the code slightly to only call utils.stringify if the actual/expected values are not strings.

You can see the bug by running mocha on the following .js file:

var test = require('mocha').it
var assert = require('assert')

assert.AssertionError.prototype.showDiff = true

test('compare multiline strings', function(){
    assert.equal("foo\ncar", "foo\nbar");
});

Current (mocha 2.0.1) output:

  1)  compare multiline strings:

      AssertionError: "foo\ncar" == "foo\nbar"
      + expected - actual

      +"foo\nbar"
      -"foo\ncar"

After patch:

  1)  compare multiline strings:

      AssertionError: "foo\ncar" == "foo\nbar"
      + expected - actual

       foo
      +bar
      -car  

@dasilvacontin
Copy link
Contributor

I've seen PRs for this already, although they didn't have tests (and that was probably the reason they aren't merged already).

I'll take a look at the PR later today.


// actual / expected diff
if (err.showDiff && 'string' == typeof actual && 'string' == typeof expected) {
if ('string' !== typeof actual) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marcello3d Why was err.showDiff removed from the conditional?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not, err.showDiff was being checked twice before, and only once now. It's easier to see in the side-by-side diff.

@boneskull
Copy link
Contributor

good, thx

boneskull pushed a commit that referenced this pull request Dec 21, 2014
showDiff: don’t stringify strings + tests
@boneskull boneskull merged commit e5663c9 into mochajs:master Dec 21, 2014
@dasilvacontin
Copy link
Contributor

@boneskull Maybe it would be a good idea to bump, since this bug was causing problems to a few people.

@marcello3d marcello3d deleted the showdiff-stringify branch December 21, 2014 20:08
@boneskull
Copy link
Contributor

@dasilvacontin Yeah I want to bump soon, no time right now though.

@dasilvacontin
Copy link
Contributor

@boneskull, sure. I could take care of it, if you are interested.

@marcello3d
Copy link
Contributor Author

Thanks y'all. :-)

Getting back into mocha (hadn't tried it since 1.13.0), lots of nice touches.

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