jest-extended
jest-extended copied to clipboard
`toHaveBeenCalledBefore` should provide API for comparing concrete calls
Consider failing test:
Expected first mock to have been called before, invocationCallOrder:
[15]
Received second mock with invocationCallOrder:
[8, 16]
So I'd like to compare second call of a mocked function but toHaveBeenCalledBefore
can't do that
The logic of toHaveBeenCalledBefore is explicitly based on the first calls of the functions, However, this raises an interesting idea. May I suggest a more powerful matcher that can take any number of mocks and compare against a call order. Example use case: "I am writing an integration test for my system, the system I am writing has multiple calls out to other services, can I have a single matcher that checks these calls are made in an order specified by me."
The problem is that sinon has graceful capabilities for that: expect(spy.firstCall).calledBefore(spy2.secondCall)
I believe something like that would be great
you could always do the below to achieve the above
expect(mock1.mock.invocationCallOrder[0]).toBeLessThan(mock2.mock.invocationCallOrder[1])
the mock function itself is core jest and not part of jest-extended and therefore probably not appropriate here although I do think this is technically possible to extend. personal 2 cents on this is having a .firstCall
or .secondCall
is arguably an unsutainable API and the mechanisms inside jest already allow what you where on about above :)
Thanks!