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:
- Developer preference/familiarity to Sinon.js
- Migrating a code base from Mocha/Chai/Sinon to Jest
- To be difficult
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.
Also under the alias:
.toBeAlwaysCalledOn()
Also under the alias:
.toBenAlwaysCalledWith()
Also under the alias:
.toBeAlwaysCalledWithExactly()
Also under the alias:
.toBeAlwaysCalledWithMatch()
Also under the alias:
.toBeAlwaysCalledWithNew()
Also under the alias:
.toBeCalled()
Also under the alias:
.toBeCalledAfter()
Also under the alias:
.toBeCalledBefore()
Also under the alias:
.toBeCalledImmediatelyAfter()
Also under the alias:
.toBeCalledImmediatelyBefore()
Also under the alias:
.toBeCalledOn()
Also under the alias:
.toBeCalledOnce()
Also under the alias:
.toBeCalledOnceWith()
Also under the alias:
.toBeCalledOnceWithExactly()
Also under the alias:
.toBeCalledThrice()
Also under the alias:
.toBeCalledTwice()
Also under the alias:
.toBeCalledWith()
Also under the alias:
.toBeCalledWithExactly()
Also under the alias:
.toBeCalledWithMatch()
Also under the alias:
.toBeCalledWithNew()
Also under the alias:
.toHaveBeenCalledTimes()
and.toBeCalledTimes()
Also under the alias:
.toReturnWith()
,.toHaveReturned()
and.toReturn()
Also under the alias:
.toAlwaysReturnWith()
Also under the alias:
.toHaveThrownError()
,.toThrow()
, and.toThrowError()
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!
Copyright (c) 2019-present Darrel Fox
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.