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

Diff output discards assertion details #1065

Closed
bajtos opened this issue Dec 9, 2013 · 9 comments
Closed

Diff output discards assertion details #1065

bajtos opened this issue Dec 9, 2013 · 9 comments

Comments

@bajtos
Copy link

bajtos commented Dec 9, 2013

When a test failure is printed using the diff output, assertion details are discarded by mocha. This makes it more difficult to understand why the test failed without inspecting test source code. It is also inconsistent with the default (non-diff) algorithm.

expect(undefined, 'first name').to.equal('expected');
/* result:
AssertionError: first name: expected undefined to equal 'expected'
*/

expect('actual', 'surname').to.equal('expected');
/* result:
actual expected
"expectedactual"
*/

Update

Another bit of information that is stripped from the output is the stack trace, as pointed out in the mailing list (link).

expect(false).to.equal(true);
/* result:
  + expected - actual

  +true
  -false
*/

assert.equal(false, true);
/* result:
AssertionError: true == false
 at Context.<anonymous> (/private/tmp/asd/test.js:5:12)
 at Test.Runnable.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:211:32)
 [etc.]
*/
@bajtos
Copy link
Author

bajtos commented Dec 9, 2013

I made an attempt to fix this issue via #949, which was rejected.

How about this solution? Always print the assertion message, treat the diff as an extra, nice-to-have detail.

expect('actual', 'surname').to.equal('expected');
/* result:
AssertionError: surname: expected 'actual' to equal 'expected'
actual expected
"expectedactual"
*/

I am willing to contribute a patch once the Mocha team decides on what is the acceptable way to fix this bug.

@bajtos
Copy link
Author

bajtos commented Dec 11, 2013

I have updated the issue description to mention the missing stack trace.

This is my new proposal:

  • Optionally print the diff (it available)
  • Always print the message+stack trace
1) foo should bar:
    + expected - actual

    +'expected'
    -'actual'

    AssertionError: surname: expected 'actual' to equal 'expected'
     at Context.<anonymous> (/private/tmp/asd/test.js:5:12)
     at Test.Runnable.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:211:32)
     [etc.]

@travisjeffery what's your opinion?

@SheetJSDev
Copy link
Contributor

Also would like to see this added to the core assert:

var assert = require('assert');
assert.equal('foo', 'bar', 'baz'); // <-- 'baz' is lost

@bajtos
Copy link
Author

bajtos commented Dec 26, 2013

@SheetJSDev that's just another case of the same underlying problem. 'baz' is stored in AssertionError.message, which is not printed by the diff output.

@busticated
Copy link

+1 @bajtos

I just ran into this one as well.

expect(users.length).to.equal(666, 'unexpected user count - NOTE: remember to foo the bar!');
/* result:
   + expected - actual

  +4
  -5
*/

@bajtos
Copy link
Author

bajtos commented Jan 15, 2014

the situation was partially improved by 34116d5 in v1.14. See also #993 and #991.

@boneskull
Copy link
Contributor

closing as old

@bajtos
Copy link
Author

bajtos commented Apr 10, 2015

@boneskull I believe the problem was still not fixed :( Are you willing to accept a pull request from me that would fix the issue? The issue is old because I was waiting for input from Mocha maintainers on what's the acceptable solution, since my initial attempt to fix this issue via #949 was rejected.

@clns
Copy link

clns commented Mar 26, 2016

This is still a pretty big problem (with the default reporter at least), especially when using the chai library. For something like this:

expect(1).to.equal(2)

You get:
image

But if you use a description (e.g. expect(1, 'My description').to.equal(2)), you get:

image

And it gets even worse when using .not (e.g. expect(1, 'My description').to.not.equal(1)) since there's no diff to output so you will see just the description:
image

This happens because chai sets the AssertionError.message to My description: expected 1 to not equal 1 and mocha deletes everything except what is before : expected in lib/reporters/base.js.

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

No branches or pull requests

5 participants