Sinon Stub Framework (Sinon Re-export)
This package, `sinon-stub-framework`, is a minimalistic wrapper that directly re-exports the entire `sinon` library. Its primary function appears to have been to provide the `sinon` package under an alternative name, possibly for historical or specific project reasons. Currently at version 1.0.4, the package has not seen updates in approximately five years (as of early 2026), suggesting it is effectively abandoned. There is no unique functionality, API, or release cadence separate from `sinon` itself, which is a mature and actively maintained library for test spies, stubs, and mocks. Users are strongly advised to install and use `sinon` directly instead of this wrapper to ensure they receive the latest features, bug fixes, and security updates.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'restore')
cause Attempting to call `.restore()` on a stub that was not properly created or has already been restored/garbage collected.fixEnsure the stub variable is correctly assigned the return value of `sinon.stub()` and that `restore()` is called only once per stub in an appropriate cleanup hook (e.g., `afterEach`). If using a `sinon.sandbox`, use `sandbox.restore()` instead. -
TypeError: myObject.myMethod is not a function
cause Attempting to stub a method that does not exist on the object, or the object/method reference is incorrect at the time of stub creation.fixVerify that `myObject` and `myMethod` are correctly spelled and accessible at the point where `sinon.stub(myObject, 'myMethod')` is called. Ensure the method isn't being stubbed on an instance that's different from the one being tested.
Warnings
- breaking This package is effectively abandoned and has not been updated in approximately five years. It only re-exports the `sinon` library, meaning it offers no unique functionality and likely contains outdated versions of `sinon` (it currently depends on `sinon@^7.0.0`, while `sinon` is at v17+).
- gotcha Because this package simply re-exports `sinon`, any breaking changes or API differences between `sinon` versions will apply. However, this wrapper will *not* automatically update `sinon` to its latest major versions, potentially leaving users stuck on older, unmaintained versions of `sinon`.
- deprecated The use of `require()` for CommonJS imports in modern Node.js environments is discouraged in favor of ES Modules (`import`). While `sinon` supports both, relying on an abandoned wrapper might complicate future migration or tooling integration.
Install
-
npm install sinon-stub-framework -
yarn add sinon-stub-framework -
pnpm add sinon-stub-framework
Imports
- stub
import stub from 'sinon-stub-framework';
import { stub } from 'sinon-stub-framework'; - Sinon
const sinon = require('sinon-stub-framework'); // For older Node.js or CJS modulesimport * as sinon from 'sinon-stub-framework';
- assert
import { sinonAssert } from 'sinon-stub-framework';import { assert } from 'sinon-stub-framework';
Quickstart
import { stub } from 'sinon-stub-framework';
class MyService {
fetchData(id) {
return Promise.resolve(`Data for ${id}`);
}
processData(data) {
return data.toUpperCase();
}
}
describe('MyService', () => {
let myService;
let fetchDataStub;
beforeEach(() => {
myService = new MyService();
// Stub a method to control its behavior during tests
fetchDataStub = stub(myService, 'fetchData');
});
afterEach(() => {
// Restore the original method after each test
fetchDataStub.restore();
});
it('should return stubbed data', async () => {
fetchDataStub.returns(Promise.resolve('Mocked Data'));
const result = await myService.fetchData(1);
expect(result).toBe('Mocked Data');
expect(fetchDataStub.calledOnceWith(1)).toBe(true);
});
it('should throw an error when stubbed', async () => {
const errorMessage = 'Network Error!';
fetchDataStub.throws(new Error(errorMessage));
await expect(myService.fetchData(2)).rejects.toThrow(errorMessage);
expect(fetchDataStub.calledOnceWith(2)).toBe(true);
});
it('should process data correctly even with stubbed dependency', () => {
fetchDataStub.returns(Promise.resolve('raw data'));
// Test another method that might rely on fetchData but we control its output
const processed = myService.processData('another data'); // Assuming processData doesn't call fetchData here for simplicity
expect(processed).toBe('ANOTHER DATA');
});
});