What is sinon?
Sinon is a testing utility that provides functions for spies, stubs, and mocks, which are essential for behavior-driven development and testing in JavaScript. It works with any unit testing framework and is widely used for its versatile API and comprehensive feature set.
What are sinon's main functionalities?
Spies
Spies are functions that record their usage, such as how many times they were called, with what arguments, and what was returned. They can be used to wrap existing functions to add this tracking ability.
const sinon = require('sinon');
const myFunction = sinon.spy();
myFunction('Hello', 'World');
console.log(myFunction.calledOnce); // true
Stubs
Stubs are like spies, but they can replace the target function's behavior, either by returning a specific value or by throwing an exception. They are useful for controlling a function's behavior in a test.
const sinon = require('sinon');
const myObj = { myMethod: () => 'original' };
const stub = sinon.stub(myObj, 'myMethod').returns('stubbed');
console.log(myObj.myMethod()); // 'stubbed'
Mocks
Mocks are fake methods (like stubs) with pre-programmed behavior and expectations. They are used to assert that certain methods are called in certain ways.
const sinon = require('sinon');
const myObj = { myMethod: () => 'original' };
const mock = sinon.mock(myObj);
mock.expects('myMethod').once().returns('mocked');
console.log(myObj.myMethod()); // 'mocked'
mock.verify();
Other packages similar to sinon
jest
Jest is a complete testing framework that comes with spies, mocks, and stubs built-in. It is often compared to Sinon for its mocking capabilities, but Jest provides a more integrated experience with a test runner and assertion library included.
testdouble
Testdouble.js (npm package 'testdouble') is a minimalistic testing library that provides a similar API to Sinon for creating test doubles like stubs, mocks, and spies. It focuses on providing a more user-friendly experience with better error messages and simpler APIs.
chai-spies
Chai-spies is a plugin for the Chai assertion library that adds spy capabilities. While it doesn't offer as comprehensive a set of features as Sinon, it integrates well with Chai for developers who prefer that assertion style.
Sinon.JS
Standalone and test framework agnostic JavaScript test spies, stubs and mocks.
Installation
via npm (node package manager)
$ npm install sinon
via NuGet (package manager for Microsoft development platform)
Install-Package SinonJS
or install via git by cloning the repository and including sinon.js
in your project, as you would any other third party library.
Don't forget to include the parts of Sinon.JS that you want to use as well
(i.e. spy.js).
Usage
See the sinon project homepage
Goals
- No global pollution
- Easy to use
- Require minimal “integration”
- Easy to embed seamlessly with any testing framework
- Easily fake any interface
- Ship with ready-to-use fakes for XMLHttpRequest, timers and more
Contribute?
Pick an issue to fix, or pitch
new features. To avoid wasting your time, please ask for feedback on feature
suggestions either with an issue
or on the mailing list.
Run the tests
The Sinon.JS developer environment requires Node/NPM. Please make sure you have
Node installed, and install Sinon's dependencies:
$ npm install
On Node
$ npm test
In the browser
Open test/sinon.html
in a browser. To test against a built distribution, first
make sure you have a build (requires Ruby and Juicer):
$ ./build
Then open test/sinon-dist.html
in a browser.
If the build script is unable to find Juicer, try
$ ruby -rubygems build
Some tests needs working XHR to pass. To run the tests over an HTTP server, run
$ node_modules/http-server/bin/http-server
Then open localhost:8080/test/sinon.html
in a browser.
On Rhino
The Rhino tests are currently out of commission (pending update after switch to
Buster.JS for tests).