Member-only story
How to Mock with SinonJS
Isolate with Spies/Stubs/Mocks
Testing is an important part of programming. It ensures that software works as intended, meets quality standards, and remains reliable over time. Testing not only helps detect and fix issues early in the development process, but also enhances software quality, boosts confidence in the codebase, and delivers a better user experience. To ensure the isolation of the code, you are testing, you will need to mock part of your application. You do not want to connect to your real database while testing code, do you? That’s where SinonJS comes into play.
tl;dr
SinonJS is a powerful JavaScript library that provides a range of testing utilities to help writing robust and reliable tests. In this article, I will dive into three key features of SinonJS: spies, mocks, and stubs. These tools allow you to simulate and control the behavior of functions and objects, making it easier to test your code in isolation.
The Theory
Even though I am sure you can’t wait to see some examples, let us first talk about what spies, stubs and mocks are: