Mocking a JavaScript Class with Jest, two ways to make it easier
Jest is the most popular automated testing framework for JavaScript. It can be used both on the front end and back end with Node.js. Jest is a feature-rich, batteries included testing framework. Amongst other valuable features, the ability to mock dependencies out of the box is a useful one.
In this post, you will learn about the need for mocking in unit testing and the difference between dependency injection and mocking. You will also get acquainted with mocking in Jest. Furthermore, you will see an example script with two classes and mock the dependent class to test the other class with a full code example using module factory and Jest SpyOn. Let’s get rolling!
Source: Mocking a JavaScript Class with Jest, two ways to make it easier, an article by Geshan Manandhar.