Reputation: 5777
I have a function that appends a random number an then calls another function. I want to check that it was called with the text passed in and match any random number. I'd like to be able to pass a Regex without Jest literally matching the Regex. Something like:
const typeFn = jest.fn();
function type (text) {
typeFn(text + Math.random());
})
type('hello')
expect(typeFn).toHaveBeenCalledWith(/hello\d+/)
Upvotes: 63
Views: 13017
Reputation: 11600
You can use one of the helper functions in expect
instead of an actual value:
expect(typeFn).toHaveBeenCalledWith(expect.stringMatching(/hello\d+/));
Here's a live example: https://repl.it/@marzelin/FrighteningCapitalConferences
Upvotes: 101