Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How about new matcher combining toHaveBeenCalledTimes and several toHaveBeenNthCalledWith? #580

Open
tkxksdl2 opened this issue Mar 17, 2023 · 0 comments

Comments

@tkxksdl2
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant