Categories
JavaScript Answers

How to check array equality ignoring element position in Jest?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *