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

Immutables nested in vanilla js objects are not compared properly #34

Closed
sebastiancarlsson opened this issue Nov 24, 2015 · 4 comments
Closed

Comments

@sebastiancarlsson
Copy link

Hi there, I'm working on unit tests for a Redux application and I'm trying to compare two so called "actions" which are objects that looks something like this:

action = {
    type: 'myType',
    list: myImmutableList
}
expectedAction = {
    type: 'myType',
    list: anIdenticalList
}

This happens:

console.log(action.type === expectedAction.type); // true
console.log(action.list.equals(expectedAction.list)); // true
expect(action.list).to.equal(expectedAction.list); // works
expect(action).to.equal(expectedAction); // throws AssertionError, as it should
// BUT
expect(action).to.deep.equal(expectedAction); // throws AssertionError, this is unexpected

I'm new to Immutable but so I'm not sure, but this looks like a bug to me? Thanks

@sebastiancarlsson
Copy link
Author

PS. If this is confirmed as a bug I'd be happy to give it a stab

@sebastiancarlsson sebastiancarlsson changed the title expect(something).to.deep.equal(somethingElse) fails unexpectedly Immutables nested in vanilla js objects are not compared properly Dec 1, 2015
@astorije
Copy link
Owner

Hi there,

It's not your case here because you are testing on regular JS objects, and in that case Chai's .deep.equal is used, not Chai Immutable's, but in the Chai Immutable world, the fact that .equal and .deep.equal act similar is normal, which is explained here in the doc. I wanted to point that out just in case.

As far as I can tell, this might be a bug indeed (or at least the outcome doesn't sound right): the value of type is the same, and if the 2 immutable lists have the same value then the assertion should pass.

Did you make sure to run chai.use(chaiImmutable); before? We've seen a similar case here. If not then yes, I'd be happy if you can run some more debugging :)

@jopek
Copy link

jopek commented Mar 8, 2016

Hi @sebastiancarlsson @astorije,

I also get an error... :

import {List} from 'immutable';
import chai, {expect} from 'chai';
import chaiImmutable from 'chai-immutable';
chai.use(chaiImmutable);

describe('REDUCER', () => {
  it('does things', () => {
    expect(
      List.of({pid: 1}, {pid: 2})
    ).to.equal(
      List.of({pid: 1}, {pid: 2})
    );
  });
});

results in this assertion error:

  REDUCER
     1) does things
  1 failing

  1) REDUCER does things:

      AssertionError: expected 'List [ [object Object], [object Object] ]' to equal 'List [ [object Object], [object Object] ]'
      + expected - actual

@astorije
Copy link
Owner

astorije commented Mar 9, 2016

Hi @jopek,

If you look at my comment above, as well as the documentation on the README and the linked issue, you will see that this is actually the expected behavior:

Immutable data structures should only contain other immutable data structures (unlike Arrays and Objects) to be considered immutable and properly work against .equal(). See this issue for more information.

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

3 participants