Package Exports
- jest-matcher-called-with-once
- jest-matcher-called-with-once/lib-esm/index.js
- jest-matcher-called-with-once/lib/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (jest-matcher-called-with-once) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
jest-matcher-called-with-once
Adds .toHaveBeenCalledWithOnce()
to your Jest expect()
matchers.
expect(spy).toHaveBeenCalledWithOnce(...args)
passes if the Jest spy
has
been called any number of times, exactly one of them with the given arguments.
Note: this matcher does not require spy
to have been called only once. It could
have been called multiple times with other arguments. But there must have been
exactly one call with arguments given to the matcher.
Installation
With yarn:
yarn add --dev jest-matcher-called-with-once`
With npm:
npm i -D jest-matcher-called-with-once`
Usage
import { toHaveBeenCalledWithOnce } from 'jest-matcher-called-with-once';
expect.extend({ toHaveBeenCalledWithOnce });
describe('test myModule', () => {
it('should return 42', () => {
const mySpy = jest.fn();
mySpy(11, 22, 33);
mySpy(444, 555);
mySpy(444, 555);
expect(mySpy).toHaveBeenCalledWithOnce(11, 22, 33);
// ^^ There was exactly one call with these args
expect(mySpy).not.toHaveBeenCalledWithOnce(444, 555);
// ^^ `.not` because there were two calls with these args
});
});
This matcher is useful when you're asserting on a function that could have been called multiple times with various arguments, and you want to make sure that you've called it exactly once with your arguments.
A common use case would be spying on a function that handles various events. You only want to make sure that it received your event exactly once, and want to ignore other calls.