Releases: timkindberg/jest-when
v3.0.0
Breaking Changes
WhenMocks will no longer pass if you don't explicitly provide every argument to calledWith
.
const fn = jest.fn()
when(fn).calledWith(1, 2).mockReturnValue(true)
const returnValue = fn(1, 2, 3, 4)
expect(returnValue).toBe(true) // Passed in v2, Fails in v3
In v3 pass matchers for every argument:
when(fn).calledWith(1, 2, 3, 4).mockReturnValue(true)
Or, remember you can use expect.anything() if you want flexibility:
when(fn).calledWith(1, 2, expect.anything(), expect.anything()).mockReturnValue(true)
Or, make your own handy util:
const restAnything = num => Array(num).fill(expect.anything())
when(fn).calledWith(1, 2, ...restAnything(2)).mockReturnValue(true)
Full Explanation
Contributor @tjenkinson found an inconsistency with jest-when's behavior compared to jest. I've always stated that the goal of this lib is to feel like a missing feature of jest and I try to stay as consistent as possible with the precedences set by jest's APIs.
In regular jest if you write this test it will fail.
const spy = jest.fn();
spy(1, 2, 3);
expect(spy).toHaveBeenCalledWith(1, 2);
Output:
expect(jest.fn()).toHaveBeenCalledWith(...expected)
Expected: 1, 2
Received: 1, 2, 3
This is not balanced with v2 of jest-when which allows you to provide a subset of the args, essentially the unspecified args implicitly behaved like an expect.anything()
matcher. So I'd either have to provide a special syntax for folks who wanted to opt out of the implicit anything
behavior, or merge this fix. So since I consider this a miss in our original API we did the latter.
Commits
- do not match when too many args provided 26abfc6