CheckOps home

Tools directory

S

CheckOps

Sinon.JS

Sinon.JS is a BSD-3-Clause library of standalone test spies, stubs and mocks for JavaScript, plus fake timers and fake XHR/servers, and works with any test framework.

Visit website

Latest public signals

Not refreshed yet. Refresh reads the public site and, when the entry has a GitHub repository, the latest release.

Visit Website

Introduction

Sinon.JS provides standalone test spies, stubs and mocks for JavaScript. Because it is framework-agnostic, it is a common companion to Mocha and Chai, and also works with Jasmine, AVA, node:test and others. It is one of the most widely used JavaScript testing utilities, with nearly 10,000 GitHub stars.

Spies record how a function was called; stubs replace functions with programmable behaviour (returns, resolves, throws, callsFake); mocks add pre-programmed expectations. Sandboxes (sinon.createSandbox() or the default sandbox with sinon.restore()) make cleanup easy. Fake timers, from the companion @sinonjs/fake-timers package, control setTimeout, Date and other clocks, and Sinon’s fake XHR and fake server help test browser code that makes HTTP requests. Assertion helpers integrate with Chai via sinon-chai.

The source code is at sinonjs/sinon on GitHub under the BSD-3-Clause licence, with about 9,800 stars; the most recent commit was on 29 October 2026.

Sinon’s fake XHR and fake server remain useful for legacy browser code.

Features

  1. Spies, stubs and mocks: Record calls, replace behaviour and verify expectations.
  2. Sandboxes: Restore all fakes in one call.
  3. Fake timers: Control time with sinon.useFakeTimers().
  4. Framework-agnostic: Works with Mocha, Jasmine, AVA, node:test and more.

Pros

  1. Free to use: Open source under the BSD-3-Clause licence, so the core tool costs nothing to use.
  2. Actively maintained: The repository had commits as recent as 29 October 2026.
  3. Community: About 9,800 GitHub stars.

Cons

  1. Redundant with Jest/Vitest: Those frameworks include their own mocking.
  2. No module mocking: Replacing imported ES modules requires extra tools.
npm install --save-dev sinon
import sinon from "sinon";
import assert from "node:assert/strict";

afterEach(() => sinon.restore());

it("retries once on failure", async () => {
  const fetchUser = sinon.stub();
  fetchUser.onFirstCall().rejects(new Error("timeout"));
  fetchUser.onSecondCall().resolves({ id: 1 });
  const user = await withRetry(fetchUser);
  assert.equal(user.id, 1);
  sinon.assert.calledTwice(fetchUser);
});

it("debounces", () => {
  const clock = sinon.useFakeTimers();
  const fn = sinon.spy();
  debounce(fn, 100)();
  clock.tick(100);
  sinon.assert.calledOnce(fn);
});

Resources:

Pricing

Sinon.JS is free and open source under the BSD-3-Clause licence.

  1. Mocha, Jasmine and node:test users who need test doubles.
  2. Teams testing timer-heavy code with fake timers.

Conclusion

Sinon.JS is the standard framework-agnostic test double library for JavaScript, especially valuable outside Jest and Vitest.

Alternatives

Other tools on CheckOps that cover similar ground. Open a page, or compare one with Sinon.JS side by side.