How to assert function invocation order in jest

The solution by clemenspeters (where he wanted to make sure logout is called before login) works for me:

const logoutSpy = jest.spyOn(client, 'logout');
const loginSpy = jest.spyOn(client, 'login');
// Run actual function to test
await client.refreshToken();
const logoutOrder = logoutSpy.mock.invocationCallOrder[0];
const loginOrder = loginSpy.mock.invocationCallOrder[0];
expect(logoutOrder).toBeLessThan(loginOrder)

Instead of your workaround you can install jest-community's jest-extended package which provides support for this via .toHaveBeenCalledBefore(), e.g.:

it('calls mock1 before mock2', () => {
  const mock1 = jest.fn();
  const mock2 = jest.fn();

  mock1();
  mock2();
  mock1();

  expect(mock1).toHaveBeenCalledBefore(mock2);
});

Note: per their doc you need at least v23 of Jest to use this function

https://github.com/jest-community/jest-extended#tohavebeencalledbefore

P.S. - This feature was added a few months after you posted your question, so hopefully this answer still helps!