Registry / testing / sinon
library0.1.1jsnpmunverified

Sinon.js is a widely used, standalone, and test framework-agnostic JavaScript library providing test spies, stubs, and mocks for robust unit testing. The current stable version is 21.1.2. It maintains a consistent release cadence, frequently publishing updates and bug fixes across major versions. Key differentiators include its non-global pollution approach, ease of integration with any testing framework (like Mocha, Jest, or QUnit), and built-in fakes for browser APIs such as timers (setTimeout, setInterval) and XMLHttpRequest. It is designed to be easy to use and requires minimal setup, allowing developers to isolate and test specific units of code effectively by controlling their dependencies and behavior.

npm install sinon
INSTALL
IMPORT
SIG · SINON
S
sinon
testingjavascriptv0.1.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

sinon
import sinon from 'sinon';
const sinon = require('sinon');
While CommonJS `require` works, ESM `import` is the recommended modern approach. Sinon exports a default object containing all its utilities.
stub
import sinon from 'sinon'; sinon.stub(obj, 'method');
import { stub } from 'sinon';
Sinon's core utilities like `stub`, `spy`, `mock`, `useFakeTimers` are properties of the default `sinon` object, not direct named exports from the package root.
useFakeTimers
import sinon from 'sinon'; const clock = sinon.useFakeTimers();
Access `useFakeTimers` via the default imported `sinon` object. It's crucial to call `clock.restore()` after tests to prevent global state pollution.

Demonstrates stubbing an asynchronous dependency's method and verifying its interaction for isolated unit testing.

import sinon from 'sinon'; // Simulate an external API dependency class ExternalAPI { async getData(): Promise<string> { // In a real application, this would make an actual network call return Promise.resolve('Real Data From Server'); } } // Our code under test that depends on ExternalAPI class MyService { constructor(private api: ExternalAPI) {} async processData(): Promise<string> { const data = await this.api.getData(); return data.toUpperCase(); } } async function runExample() { const api = new ExternalAPI(); const service = new MyService(api); // Create a stub for the API's getData method const getDataStub = sinon.stub(api, 'getData'); // Configure the stub to return a predictable, mock value getDataStub.resolves('Mocked Data'); // Call the method under test, which now uses the stubbed getData const result = await service.processData(); console.log('Processed Result:', result); // Expected output: MOCKED DATA console.log('getData was called once:', getDataStub.calledOnce); // Expected output: true // Restore the original method to clean up after the test getDataStub.restore(); } runExample().catch(console.error);
Debug
Known issues
breakingSinon v3.0.0 removed several previously deprecated exports. Additionally, `fakeXhr`, `fakeServer`, and `fakeServerWithClock` functionalities were extracted into the `nise` package, though they were re-imported into Sinon's top-level API. Direct access to these modules via their old, internal paths may break.
fix
Consult the v3.0.0 migration guide on sinonjs.org for full details. For most users, continue to access functionalities like `sinon.useFakeXMLHttpRequest()` via the top-level `sinon` object.
affects: >=3.0.0
deprecatedThe `spy.reset()` method was deprecated in favor of `spy.resetHistory()` starting from v4.1.4. While `reset()` still works in current versions (21.x.x), it emits a deprecation warning and may be removed in future major releases.
fix
Update your code to use `spy.resetHistory()` for clearing call history, arguments, and return values of a spy or stub. Use `spy.restore()` for reverting the original method.
affects: >=4.1.4
gotchaVersion 4.1.5 contained a bug where `sinon.useFakeServer()` could return an unexpected server type, leading to incorrect test behavior. This issue was promptly addressed in the following patch release.
fix
Ensure your project is using Sinon.js v4.1.6 or a later version to avoid issues with `sinon.useFakeServer()`.
affects: 4.1.5
gotchaFailure to call `clock.restore()` after `sinon.useFakeTimers()` can lead to global state pollution, causing unexpected behavior in subsequent tests or other parts of the application that rely on real timer functions or the global `Date` object.
fix
Always ensure `clock.restore()` is called in an `afterEach` or `after` hook within your test suite to revert global timers and `Date` to their original implementations.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: sinon.stub is not a function
This error typically occurs when attempting to destructure `stub` as a named import (e.g., `import { stub } from 'sinon';`) or when `sinon` itself is not correctly imported.
fix
Ensure Sinon is imported as a default export: `import sinon from 'sinon';` (ESM) or `const sinon = require('sinon');` (CommonJS), and then access `stub` as a property: `sinon.stub()`.
TypeError: Cannot read properties of undefined (reading 'reset') OR console warning: 'spy.reset()' is deprecated. Use 'spy.resetHistory()' instead.
Attempting to use the `spy.reset()` method, which has been deprecated since v4.1.4 and is no longer the recommended way to clear a spy's state.
fix
Replace all calls to `spy.reset()` with `spy.resetHistory()` to clear the call history, arguments, and return values of a spy or stub. Use `spy.restore()` to revert the original method.
Error: Cannot stub non-existent property 'someMethodName'
Sinon cannot stub a method or property that does not exist on the target object at the moment `sinon.stub()` is called.
fix
Verify that the method or property you intend to stub (`someMethodName`) exists on the object you are passing to `sinon.stub()`. This often happens with dynamic properties or when stubbing properties on prototypes that haven't been correctly inherited or defined.
Upgrade
Version history
0.1.1latest on npm
Audit
Dependencies
fake-timersrequiredUsed internally by Sinon for faking global timers (setTimeout, setInterval, Date).
niserequiredProvides fake XHR and fake server functionality, extracted from Sinon.js in v3.0.0.
Agent activity
2 hits · last 30 days
node
2
Resources