Can I use custom matcher with toHaveBeenCalledWith?

722 Views Asked by At

I need to check a URL that was passed to a method. I wrote a custom matcher for this but I don't know how to use it in toHaveBeenCalledWith.

How can I use my custom matcher in this situation?

E.g. in C# Moq I can do the following to match only even numbers:

mock.Setup(foo => foo.Add(It.Is<int>(i => i % 2 == 0))).Returns(true);
1

There are 1 best solutions below

0
On BEST ANSWER

Maybe not as elegant as what you're looking for, but you should be able to apply your custom matcher to yourMockFunction.mock.calls[0][0].

expect(customMatcher(mockFunction.mock.calls[0][0])).toEqual(true)

If there are multiple arguments, you can read the next elements in mockFunction.mock.calls[0]. If you want to check multiple calls, they will be available in mockFunction.mock.calls[1], mockFunction.mock.calls[2] and so on.