Is there an Array equality matching function that ignores the position of an element in jest.js?

I get .toEqual () to check if all fields are equal for simple objects:

expect(
    {"key1":"pink  wool","key2":"diorite"}
).toEqual(
    {"key2":"diorite","key1":"pink wool"}
);

So it goes.

But for arrays, this is not the case:

expect(["ping wool", "diorite"]).toEqual(["diorite", "pink wool"]);

There seems to be no helper function that does this in jest docs, i.e. checks the equality of two arrays regardless of their element positions. Is it necessary to check each element in one array against all elements in another and vice versa? Or is there another way?

+4
source share
2 answers

, .sort() , :

expect(["ping wool", "diorite"].sort()).toEqual(["diorite", "pink wool"].sort());`

.

+4

QUnit deepEqual https://api.qunitjs.com/assert/deepEqual

toBeTruthy

expect(isEqual(1,2)).toBeTruthy()

0

Source: https://habr.com/ru/post/1658224/


All Articles