expect(array).to.not.include(obj) failing with constructors
See original GitHub issueThis may be linked to https://github.com/chaijs/chai/issues/390, but I’m not sure so reporting this separately:
Environment:
- Chai version: 3.5.0
- Node version: 6.2.0
Test case:
function SomeConstructor() {}
const instance1 = new SomeConstructor();
const instance2 = new SomeConstructor();
expect(instance1).to.not.equal(instance2) // => passes as expected
const arr = [instance1];
expect(arr).to.include(instance1) // => passes as expected
expect(arr).to.not.include(instance2) // => fails; unexpected
As a workaround, I can change the constructor to:
function SomeConstructor() {
this.uid = Symbol();
}
and expect(arr).to.not.include(instance2)
will work fine.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:25 (23 by maintainers)
Top Results From Across the Web
Expect - Jest
When you're writing tests, you often need to check that values meet certain conditions. expect gives you access to a number of "matchers" ......
Read more >Expect / Should - Chai Assertion Library
This includes properties that are inherited and/or non-enumerable. Add .own earlier in the chain to exclude the target's inherited properties from the search....
Read more >How to check if an object is not an array? - Stack Overflow
Try something like this : obj.constructor.toString().indexOf("Array") != -1. or (even better) obj instanceof Array.
Read more >Expect · Jest
It will match received objects with properties that are not in the expected object. You can also pass an array of objects, in...
Read more >JavaScript array type check - “is array” vs object in-depth
In JavaScript, the Array constructor/prototype is not shared across windows/iframes. Therefore instanceof Array works, but only if you're ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ll likely knock this out over the weekend unless someone else chimes in wanting to do it.
Looked through the commit history. Here’s what happened:
.include
just used a simpleindexOf
test. Therefore, it used strict equality instead of deep..include
to also allow this type of assertion:expect({a: 1, b: 2}).to.include({a: 1});
. It accomplishes this by using the existing.property
assertion, aka, a strict comparison on the value of each key being tested for inclusion. (It was briefly mentioned how it’d be nice for a deep comparison to be possible, but not followed up on.)indexOf
like it did previously when testing for an object inside of an array, it was instead fixed in such a way that it now used deep equality instead of strict. There was no discussion or even casual acknowledgement of this difference in functionality; it was just accepted and merged.Based on the above, I think the current functionality is a bug. After all, the
.deep
flag did exist back when #244 was introduced, so I don’t think #244 would’ve been merged if it’d been realized that the comparison method changed from strict to deep in the process of fixing the #239 regression.This makes me feel a lot more comfortable about changing the current behavior. As I said before, the current functionality has always felt a bit strange to me, but I hadn’t looked into it deeply so I just kinda assumed it had been designed this way on purpose.
I propose the following (which I believe is in line with what has already been suggested):
indexOf
..property
to assert that the expected key(s) are present in an object, and that their values are strictly equal..property
assertion.indexOf
as it originally was.deep
flag support, which overrides the default functionality of both property and array inclusion in order to use deep equality instead of strict.