Cypress tip: how to stub an alert

I’ve recently started using Cypress for testing web applications, and even for unit testing. One thing that was not immediately clear to me was how to stub (aka, fake) a basic thing like an alert, so that I can “listen” for this alert to be called, and assert that yes, it was indeed called.

How to do it

This is my snippet of code, used in a unit test in this case

Given a component like:

We can test the onClick like this:

 

Leave a Reply

Your email address will not be published. Required fields are marked *