Expect several but not all true

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Expect several but not all true



Suppose I have an array arr = [true, false, false, true, false].
I wish to assert that exactly two values are true in this array. Obviously, I could do something like this (with lodash):


arr = [true, false, false, true, false]


Chai.expect(_.filter(arr, item => item == true)).to.have.length(2)



Is there a more Chai-y or Jest-y way to do this?





You could try something like expect(arr).toEqual(expect.arrayContaining([true, true]))
– Luca Kiebel
Aug 7 at 19:48


expect(arr).toEqual(expect.arrayContaining([true, true]))




1 Answer
1



Closest assertion methods are as follows:



Both options will not suit because of your expected values being a value type and functions above use === for comparison (it can be changed, at least in Chai, but still no difference for our case).


===


// Jest example
describe('arrayContaining', () =>
const expected = [true, true];

it('does not match: not enough `true` values received', () =>
expect([true, false, false]).not.toEqual(expect.arrayContaining(expected));
);

it('does not match: not enough toatl values received', () =>
expect([true]).not.toEqual(expect.arrayContaining(expected));
);
);



Both assertions above will fail, it means that these assertion rules/functions can not spot a difference between expected and actual values in the given case.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard