I'm writing some Jasmine tests for some legacy javascript that produces an alert or a confirm at some points in the code.
At the moment where the alert pops up it pauses execution in the browser requiring me to press ok before going on.
I'm sure I'm missing something but is there a way of faking an alert?
Even better is it possible to find out what the message was for the alert?
Thanks for your help.
The alert() method in JavaScript is used to display a virtual alert box. It is mostly used to give a warning message to the users. It displays an alert dialog box that consists of some specified message (which is optional) and an OK button. When the dialog box pops up, we have to click "OK" to proceed.
The alert() method displays an alert box with a message and an OK button. The alert() method is used when you want information to come through to the user.
spyOn(window, 'alert');
. . .
expect(window.alert).toHaveBeenCalledWith('a message');
var oldalert = alert;
alert = jasmine.createSpy();
// do something
expect(alert).toHaveBeenCalledWith('message')
alert = oldalert
Another way is to do this in spec helper.
window.alert = function(){return;};
Or if you need the message.
var actualMessage = '';
window.alert = function(message){actualMessage = message; return;}
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