Categories
JavaScript Answers

How to test part of object using Jest and JavaScript?

Spread the love

Sometimes, we want to test part of object using Jest and JavaScript.

In this article, we’ll look at how to test part of object using Jest and JavaScript.

How to test part of object using Jest and JavaScript?

To test part of object using Jest and JavaScript, we can use the toMatchObject method.

For instance, we write

expect(parseTime("12:54")).toMatchObject({ hours: 12, minutes: 54 });

to call toMatchObject to check that the object returned by parseTime("12:54") has { hours: 12, minutes: 54 } in it.

Conclusion

To test part of object using Jest and JavaScript, we can use the toMatchObject method.

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 *