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+/)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With