jest-community/jest-extended

How about new matcher combining toHaveBeenCalledTimes and several toHaveBeenNthCalledWith?

tkxksdl2 opened this issue · 0 comments

In many case, I usually use both toHaveBeenCalledTimes and toHaveBeenCalledWith when test a function.
But some functions are avoked more than 2 time, so toHaveBeenNthCalledWith has to be repeated with different args.
This makes code longer. so i'm currently using custom hook like this:

type MockFunc<T extends any[], R> = (...args: T) => R;

export const expectCalledTimesAndWith = <T extends any[], R>(
  mockfunc: jest.Mock<any, any> | MockFunc<T, R>,
  times: number,
  ...args: T[]
) => {
  expect(mockfunc).toHaveBeenCalledTimes(times);
  args.map((arg, i) => {
    expect(mockfunc).toHaveBeenNthCalledWith(i + 1, ...arg);
  });
};
// use-case
expectCalledTimesAndWith( jwtService.sign, 2, [{ id: ID }, TokenType.Access], [{ id: ID }, TokenType.Refresh]);

So I think It would be cool that jest provide a matcher does these process. Is it possible to add matcher that test several avoke like this?