
Security News
AGENTS.md Gains Traction as an Open Format for AI Coding Agents
AGENTS.md is a fast-growing open format giving AI coding agents a shared, predictable way to understand project setup, style, and workflows.
jest-sinon
Advanced tools
Jest-Sinon is a collection of assertions for using the mocking library Sinon.js with Jest.
const bar = () => {};
const foo = sinon.spy();
foo(bar);
// instead of:
expect(foo.calledWith(bar)).toBeTruthy;
// we can write:
expect(foo).toHaveBeenCalledWith(bar);
The assertions: toHaveBeenCalledTimes
, toThrow
, toReturnWith
, toHaveBeenCalled
and toHaveBeenCalledWith
clash with the in-built Jest
mocking framework. Jest-Sinon
will try and detect which type of spy is being used and use the correct assertion. You should be able to use both mocking libraries in parallel.
const foo = sinon.spy();
const bar = jest.fn();
foo();
bar();
expect(foo).toHaveBeenCalled(); // true
expect(bar).toHaveBeenCalled(); // true
There are a number of reasons why you might want to use Sinon.js
instead of (or as well as) the in-built mocking assertions. Some of the use cases include:
Ultimately, it usually comes down to your own preferences and needs.
With npm:
npm install --save-dev jest-sinon
With yarn:
yarn add -D jest-sinon
Add Jest-Sinon
to your Jest setupFilesAfterEnv
configuration.
"jest": {
"setupFilesAfterEnv": ["jest-sinon"]
}
"jest": {
"setupTestFrameworkScriptFile": "./testSetup.js"
}
// testSetup.js
require('jest-sinon');
Jest-Sinon
adds a number of assertions to help test Sinon.js
Spies, Mocks and Stubs. Below is a list of currently implemented assertions.
.toHaveBeenAlwaysCalledOn(obj)
Also under the alias:
.toBeAlwaysCalledOn()
.toHaveBeenAlwaysCalledWith(args1, arg2, ...)
Also under the alias:
.toBeAlwaysCalledWith()
.toHaveBeenAlwaysCalledWithExactly(args1, arg2, ...)
Also under the alias:
.toBeAlwaysCalledWithExactly()
.toHaveBeenAlwaysCalledWithMatch(arg1, arg2, ...)
Also under the alias:
.toBeAlwaysCalledWithMatch()
.toHaveBeenAlwaysCalledWithNew()
Also under the alias:
.toBeAlwaysCalledWithNew()
.toHaveBeenCalled()
Also under the alias:
.toBeCalled()
.toHaveBeenCalledAfter(anotherSpy)
Also under the alias:
.toBeCalledAfter()
.toHaveBeenCalledBefore(anotherSpy)
Also under the alias:
.toBeCalledBefore()
.toHaveBeenCalledImmediatelyAfter(anotherSpy)
Also under the alias:
.toBeCalledImmediatelyAfter()
.toHaveBeenCalledImmediatelyBefore(anotherSpy)
Also under the alias:
.toBeCalledImmediatelyBefore()
.toHaveBeenCalledOn(obj)
Also under the alias:
.toBeCalledOn()
.toHaveBeenCalledOnce()
Also under the alias:
.toBeCalledOnce()
.toHaveBeenCalledOnceWith(arg1, arg2, ...)
Also under the alias:
.toBeCalledOnceWith()
.toHaveBeenCalledOnceWithExactly(arg1, arg2, ...)
Also under the alias:
.toBeCalledOnceWithExactly()
.toHaveBeenCalledThrice()
Also under the alias:
.toBeCalledThrice()
.toHaveBeenCalledTwice()
Also under the alias:
.toBeCalledTwice()
.toHaveBeenCalledWith(arg1, arg2, ...)
Also under the alias:
.toBeCalledWith()
.toHaveBeenCalledWithExactly(arg1, arg2, ...)
Also under the alias:
.toBeCalledWithExactly()
.toHaveBeenCalledWithMatch(arg1, arg2, ...)
Also under the alias:
.toBeCalledWithMatch()
.toHaveBeenCalledWithNew()
Also under the alias:
.toBeCalledWithNew()
.toHaveCallCount(number)
Also under the alias:
.toHaveBeenCalledTimes()
and.toBeCalledTimes()
.toHaveReturnedWith(obj)
Also under the alias:
.toReturnWith()
,.toHaveReturned()
and.toReturn()
.toHaveAlwaysReturnedWith(obj)
Also under the alias:
.toAlwaysReturnWith()
.toHaveThrown(error?)
Also under the alias:
.toHaveThrownError()
,.toThrow()
, and.toThrowError()
.toHaveAlwaysThrown(error?)
Also under the alias:
.toHaveAlwaysThrownError()
,.toAlwaysThrow()
and.toAlwaysThrowError()
For more information about what these do, you can visit Sinon.js.
Pull requests for new features, bug fixes, and suggestions are welcome!
1.1.0
FAQs
Jest assertions for the mocking library Sinon.js
The npm package jest-sinon receives a total of 8,883 weekly downloads. As such, jest-sinon popularity was classified as popular.
We found that jest-sinon demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
AGENTS.md is a fast-growing open format giving AI coding agents a shared, predictable way to understand project setup, style, and workflows.
Security News
/Research
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.