Member-only story

How to Mock with SinonJS

Isolate with Spies/Stubs/Mocks

Ziga Petek
5 min readJun 6, 2023
Foto von Kari Shea auf Unsplash

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

Photo by Egor Myznik on Unsplash

Even though I am sure you can’t wait to see some examples, let us first talk about what spies, stubs and mocks are:

Spies

--

--

Ziga Petek
Ziga Petek

Written by Ziga Petek

Developer, writer and a pure genious ... I may have exagerated a little ... Author of https://divine-coding.com

No responses yet