Introduction to Mocking Packages
Explore the fundamental concept of mocking external packages with Jest. Understand why mocking is necessary for third-party libraries, how to create mocks that conform to the package interface, and best practices for implementing them. This lesson helps you improve test reliability by handling external dependencies effectively.
We'll cover the following...
Why do we mock packages?
Open-source software is a vital part of any modern codebase. It allows us to gain robust features and support complex logic with as little as just a few lines of code. These same external libraries often don’t play nice with testing environments out of the box, though.
This won’t be true for all external packages, and each package should be considered individually. Entire frameworks like Koa and Angular are packaged as third-party code, and we don’t want to mock these. Additionally, some libraries might provide functionality that just doesn’t need to be mocked, such as the format or joi ...