Sometimes, we want to check array equality ignoring element position in Jest.
In this article, we’ll look at how to check array equality ignoring element position in Jest.
How to check array equality ignoring element position in Jest?
To check array equality ignoring element position in Jest, we can call array sort
to sort the array before comparing them.
For instance,. we write:
expect(["wool", "down"].sort()).toEqual(["down", "wool"].sort());
to call sort
on both arrays before comparing them with toEqual
.
Conclusion
To check array equality ignoring element position in Jest, we can call array sort
to sort the array before comparing them.