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

Objects prematurely coerced into string primitives #2496

Closed
oliversalzburg opened this issue Sep 22, 2016 · 5 comments
Closed

Objects prematurely coerced into string primitives #2496

oliversalzburg opened this issue Sep 22, 2016 · 5 comments
Labels
type: bug a defect, confirmed by a maintainer

Comments

@oliversalzburg
Copy link
Contributor

This is regarding an issue I reported on the WebStorm tracker: https://youtrack.jetbrains.com/issue/WEB-23383

Differences between symbols that are not strictly equal, appear as being equal and are thus hard to read and confusing.

Here is a simplified version of a test where I noticed this behavior:

"use strict";

const chai           = require( "chai" );
const expect         = chai.expect;

chai.config.truncateThreshold = 0;

describe( "Strings", () => {
    it( "should correctly display differences", () => {
        expect( new String( "foo" ) ).to.equal( "foo" );
    } );
} );

Because new String( "foo" ) is not equal to the primitive "foo", the test will correctly fail. However, in the resulting output, the difference is not immediately apparent.

In this simple test, the AssertionError will pretty clearly identify the issue. However, in larger objects, this can be much harder to identify, especially if the truncation threshold is not raised.

@Munter
Copy link
Contributor

Munter commented Sep 22, 2016

How assertion libraries generate error messages is outside of the scope of Mocha. I suggest taking this up with chai or the relevant chai plugin you might be using

@Munter Munter closed this as completed Sep 22, 2016
@oliversalzburg
Copy link
Contributor Author

@Munter Apparently the behavior is related to https://github.com/mochajs/mocha/blob/master/lib/utils.js#L435

Also, @boneskull suggested to post this here

@papandreou
Copy link
Contributor

Quite reproducible:

it('should foo', function () {
    var err = new Error('oh no');
    err.actual = new String('foo');
    err.expected = 'foo';
    throw err;
});

Output:

$ mocha foo.js 


  1) should foo

  0 passing (8ms)
  1 failing

  1)  should foo:

      Error: oh no
      + expected - actual


      at Context.<anonymous> (foo.js:2:15)

@Munter Munter reopened this Sep 22, 2016
@boneskull
Copy link
Contributor

@Munter Yeah, the problem seems to be in our diffing code; Chai actually outputs the difference correctly. See original issue.

@boneskull boneskull added type: bug a defect, confirmed by a maintainer confirmed labels Sep 22, 2016
boneskull added a commit that referenced this issue Sep 22, 2016
- rename some exported functions/vars as to use locally
- update JSDoc comments
boneskull added a commit that referenced this issue Sep 27, 2016
- rename some exported functions/vars as to use locally
- update JSDoc comments
- includes IE7/8 support
@oliversalzburg
Copy link
Contributor Author

Does the fix also apply to new Number(42)? Because the message explicitly states "String".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug a defect, confirmed by a maintainer
Projects
None yet
Development

No branches or pull requests

4 participants