jest-extended icon indicating copy to clipboard operation
jest-extended copied to clipboard

`toHaveBeenCalledBefore` should provide API for comparing concrete calls

Open deser opened this issue 5 years ago • 4 comments

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

deser avatar Jul 31 '18 14:07 deser

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."

benjaminkay93 avatar Aug 01 '18 12:08 benjaminkay93

The problem is that sinon has graceful capabilities for that: expect(spy.firstCall).calledBefore(spy2.secondCall)

I believe something like that would be great

deser avatar Aug 01 '18 14:08 deser

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 :)

benjaminkay93 avatar Aug 01 '18 18:08 benjaminkay93

Thanks!

deser avatar Aug 02 '18 05:08 deser