When writing a unit test in Jest, how can I test that an array contains exactly the expected values in any order?
In Chai, I can write:
const value = [1, 2, 3];
expect(value).to.have.members([2, 1, 3]);
What's the equivalent syntax in Jest?
When writing a unit test in Jest, how can I test that an array contains exactly the expected values in any order?
In Chai, I can write:
const value = [1, 2, 3];
expect(value).to.have.members([2, 1, 3]);
What's the equivalent syntax in Jest?
Another way is to use the custom matcher .toIncludeSameMembers()
from jest-community/jest-extended.
Example given from the README
test('passes when arrays match in a different order', () => {
expect([1, 2, 3]).toIncludeSameMembers([3, 1, 2]);
expect([{ foo: 'bar' }, { baz: 'qux' }]).toIncludeSameMembers([{ baz: 'qux' }, { foo: 'bar' }]);
});
It might not make sense to import a library just for one matcher but they have a lot of other useful matchers I've find useful.
Additional note, if you're using Typescript, you should import the types for the methods added to expect
with this line:
import 'jest-extended';
I would probably just check that the arrays were equal when sorted:
expect(value.sort()).toEqual([2, 1, 3].sort())
What about arrayContaining
expect(value).toEqual(expect.arrayContaining([2, 1, 3]));
arrayContaining
is similar to what I need. However, arrayContaining
allows actual
to have additional values that are not in expected
. I want to make sure that both arrays contain exactly the same elements, only potentially in different orders. –
Araxes Perhaps you could use the array.sort
method to line up the order in conjunction with the arrayContaining
method. You might also include a length test for good measure.
const value = [1, 2, 3];
expect(value).toHaveLength(3);
expect(value.sort()).toEqual(expect.arrayContaining(value.sort()));
arrayContaining
? –
Barrie A variation on the answer by spoonmeiser that uses the copying version of sort, toSorted().
expect(value.toSorted((a, b) => a - b)).toEqual([1, 2, 3])
© 2022 - 2024 — McMap. All rights reserved.