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 install -D sinon
yarn add -D sinon
pnpm add -D sinon
Import
import sinon from 'sinon';
Quick Example
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
| Package | sinon |
| Category | Testing |
| Weekly Downloads | 6M+ |
| License | BSD-3-Clause |
| Install | npm install -D sinon |
Related Packages
Mocha is a feature-rich JavaScript test framework that runs on Node.js and in the browser, providing…
Chai is a BDD/TDD assertion library for Node.js and the browser that pairs with any JavaScript testi…
Jest is a delightful JavaScript testing framework with a focus on simplicity, created by Facebook. I…
Nock is an HTTP server mocking and expectations library for Node.js that intercepts outgoing HTTP re…
Mock Service Worker (MSW) is an API mocking library that uses the Service Worker API to intercept re…
Browse npm Packages by Category
Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.