🧪
Testing6M+/wkBSD-3-Clause

sinon

Sinon.js provides standalone test spies, stubs, and mocks for JavaScript that work with any test framework. Spies record information about function calls — argu

Installation

npm
npm install -D sinon
yarn
yarn add -D sinon
pnpm
pnpm add -D sinon

Import

ESM
import sinon from 'sinon';

Quick Example

usage
import sinon from 'sinon';

const callback = sinon.spy();
myFunction(callback);
assert(callback.calledOnce);

const stub = sinon.stub().returns(42);
assert.equal(stub(), 42);

About sinon

Sinon.js provides standalone test spies, stubs, and mocks for JavaScript that work with any test framework. Spies record information about function calls — arguments, return values, this context, and exceptions thrown — without changing the function's behavior. Stubs are spies with pre-programmed behavior that replace the target function entirely, useful for controlling function output and preventing side effects like network calls or file system access during tests. Mocks combine spies and stubs with built-in expectations that are verified at the end of the test. Sinon also includes a fake timer system for controlling setTimeout, setInterval, Date, and requestAnimationFrame, fake XMLHttpRequest and server implementations for intercepting HTTP requests, and sandboxes for automatically restoring all fakes after each test. Sinon works with any assertion library and test runner — it is most commonly paired with Mocha and Chai but integrates well with Jest and Vitest too. The library's approach to test doubles follows the patterns described by Gerard Meszaros in xUnit Test Patterns, providing a comprehensive toolkit for isolating code under test from its dependencies.

Quick Facts

Packagesinon
CategoryTesting
Weekly Downloads6M+
LicenseBSD-3-Clause
Installnpm install -D sinon

Related Packages

Browse npm Packages by Category

Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.