Chai: use of deep.include not working properly

Created on 31 May 2017  路  2Comments  路  Source: chaijs/chai

I thought I was doing something wrong, and I was... not adding .deep into the test, but even then I still have issues.

I've created a simple JsFiddle to see the problem:

https://jsfiddle.net/balexandre/4Loupnjk/5/

The original question is on SO#44288568.

Most helpful comment

Hey @balexandre thanks for the issue.

I'll add another alternative solution here. In addition to the SO post and @meeber's wonderful response.

You can use the .nested.include assertion. As our docs state the nested flag can be used to assert each key in a nested fashion, so that you can more easily test subsets. Example:

it("SHOULD 'includes' expected results", function() {
  expect(e)
    .that.is.an("object")
    .that.nested.include({
      "results.total_rejected_recipients": 0,
      "results.total_accepted_recipients": 1
    });
});

Given that we have a few workaround for this, and that chai-subset exists as a plugin; we don't wish to invalidate plugin authors work by consuming their features, and so I don't we will likely keep the existing patterns and not add any new ones. I'll close this one. Feel free to continue the discussion here though, and add any new ideas around this.

All 2 comments

@balexandre Thanks for the issue. I'm not sure that this behavior is a bug.

When the target is an object (like in your linked examples), .include asserts that the given object鈥檚 properties are a subset of the target鈥檚 properties. Even when the .deep flag is set, the .include algorithm doesn't recursively apply this logic to sub-properties, sub-sub-properties, etc. Instead, it performs an equality comparison (strict by default or deep if the flag is set) between each of the given properties and the corresponding property in the given object.

So in your fiddle examples, it's performing either a strict equality comparison or deep equality comparison between the results property in the given object and the results property in the target object. Since the results property in the target object contains an id property, and the results property in the given object doesn't, the equality comparison fails, regardless of strict or deep.

The following example, however, does pass:

var e = {
  "results":  {
    "total_rejected_recipients": 0,
    "total_accepted_recipients": 1,
    "id":"102618457586465882"
  }
};


it("SHOULD 'includes' expected results", function() {
  expect(e)
    .to.have.property("results")
    .that.is.an("object")
    .that.includes({
      "total_rejected_recipients": 0,
      "total_accepted_recipients": 1
    });
});

Hey @balexandre thanks for the issue.

I'll add another alternative solution here. In addition to the SO post and @meeber's wonderful response.

You can use the .nested.include assertion. As our docs state the nested flag can be used to assert each key in a nested fashion, so that you can more easily test subsets. Example:

it("SHOULD 'includes' expected results", function() {
  expect(e)
    .that.is.an("object")
    .that.nested.include({
      "results.total_rejected_recipients": 0,
      "results.total_accepted_recipients": 1
    });
});

Given that we have a few workaround for this, and that chai-subset exists as a plugin; we don't wish to invalidate plugin authors work by consuming their features, and so I don't we will likely keep the existing patterns and not add any new ones. I'll close this one. Feel free to continue the discussion here though, and add any new ideas around this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

domenic picture domenic  路  4Comments

leifhanack picture leifhanack  路  4Comments

liborbus picture liborbus  路  4Comments

kharandziuk picture kharandziuk  路  4Comments

xareelee picture xareelee  路  3Comments