chai: expect(array).to.deep.include({...}) does not apply deep equality recursively
As the title says,
if I run expect(array).to.deep.include(obj) I would expect to apply deep equality to each of the property of obj.
But it does not behave like this, is it a bug?
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 24 (9 by maintainers)
This fails with assert on chai v4.1.0:
expect([ { id: 1, name: 'first'}, { id: 2, name: 'second'}]).to.deep.include({ name: 'first' })This is bug? How i can test if array include object with some specified properties?
upd: can be fixed with chai-subset plugin. Code is something like:
expect([ { id: 1, name: 'first'}, { id: 2, name: 'second'}]).to.containSubset([{ name: 'first' }])@deksden
chai-subsetplugin is what I was looking for 👍@lucasfcosta imo the default should be to search by object reference. I would then expect
assert.deepIncludeorshould.deep.includeto do a deep comparison, just likeequalsanddeepEqualsbehave.Hi @djom20, I think you could try to use a matcher or search for a plugin which would allow you to do that easily in a single assertion.
As a last resort you could also do:
If you’re checking for multiple items in the same array you can make that function curried:
@lucasfcosta that sounds not like desirable behavior to me. Indeed I was surprised that
[{}].should.not.contain({})fails, while[{}].indexOf({}).should.equal(-1)succeeds. If this is the intended behavior I think it should be documented with the includes method. Also in general it would be nice if a warning was issued if a non identity flag (likedeep) is set on a method that doesn’t support it. Looking at the documentation ofdeepit only says that it is used byequalandpropertybut it is (at least) also used bymembers(which doesn’t mention on what kind of target it can be used). Any reason not to support general ES6 iterables here?Whoops forgot to close this issue. Fixed via #761, will be released in 4.x (alpha release will be available on npm soon).
Another basic failure case for “include” is:
Ultimately, it’s using an ‘===’ on the value assertion.
I’ve tried various “deep” flags and other tricks (“contain”, “keys”, “members”) but to no avail. How can I make such a simple assertion (without using “property” as I don’t want the assertion subject to change - which is why I’m using “include” in the first place).
This doesn’t work for nested arrays either. I need to test whether an associative array contains some entry but the following fails:
What I find surprising is, that it works as expected using
include.deep.members:How do you do this with assert style? there is no
assert.deepInclude()…