Picture this common scenario-
A user signs up to your web app for an account. You send them a verification email with a link. They click on the link, create a password, and log into your site.
Simple, right? Sure.
But how do you use an automated testing tool like Cypress to test it, if you can’t access the emails?
Ideally, we’re looking for a simple way to receive an email as a JSON object, that we can then run assertions against. Here’s 3 ways you can achieve just that:
A) Point your app to a dummy mail server
These solutions act as an SMTP mail server, and require you to point your app to this new mail server. They run a local UI- and sometimes API, and let you see exactly what was ‘sent’. Some have options to ‘forward’ the email on to your original addressee.
Pros: Often free, simple UI, most have some form of API access
Cons: Tricky to setup, requires changes to your app codebase to alter your email flow, requires more server resources to run the dummy server
B) Spy on your outgoing transactional mail server
Your email is probably already being sent by a transactional email server. Some mail servers allow you API access to review the logs or events, and see what was sent. Look in the developer’s documentation for ‘events API’, ‘logs API’ or similar for reference.
Pros: Email is actually ‘sent’ to your user, no extra costs
Cons: Not supported by all transactional mail servers, and implementation can be tricky. Watch out for sending emails to dummy accounts- this can impact your sender reputation.
C) Use a disposable email service
This is the most comprehensive way to test your app. These services allow you to programatically create a dummy email address, use that email address to sign up, your emails are actually delivered, and then that inbox is accessible via an API endpoint. You can then scrape that email for the auth code, verification link, etc.
Pros: Email is actually ‘delivered’ to your user, full end-to-end testing, no changes to your app codebase
Cons: Some extra costs, be wary of ‘public’ or free email accounts
In my case- I don’t have access to change our transactional email server to one that supports API logs access, and I wanted to ensure that we didn’t alter the email flow too much. Implementing Mailslurp as a disposable email service was simple and very effective- and allows us to generate and receive as many emails as we need.
#cypress #email #end-to-end #testing #smtp #intercept #api
Note: Gmail and other free email services don’t like you logging in programatically to check your inbox. There’s plenty of examples online of people logging in, but the services change their access frequently, and this will cause flaky tests. Avoid these free services if you can.