November 5, 2024

Handling email delays in Cypress tests

TL;DR. Try increasing the timeout so the email is received even with the delay

// timeout of 10 minutes
cy.mailiskSearchInbox(namespace, {}, { timeout: 1000 * 60 * 10 });

When automating end-to-end tests involving emails, timing-related issues are common. Emails may take longer than expected to arrive, causing flaky tests or unexpected failures. We'll explore how to handle email delays when using Cypress with Mailisk

Why Delays Happen

Email delivery can be delayed for various reasons. While Mailisk ensures your emails are reliably received, your email delivery provider might introduce delays. Here are some common causes:

  • Network Latency: Emails take time to travel between servers.
  • High Server Load: Sending a large volume of emails can cause processing delays.
  • Email Service Provider Delays: ESPs might experience temporary slowdowns or throttling.

The first step is to determine if the issue is an email delivery delay or a backend bug preventing email sending.

Check the Namespace Inbox

The simplest way to verify if emails are delayed or not sent at all is to check your namespace's inbox on Mailisk. If you've run recent tests and expect emails to have been sent, inspect the inbox. If no emails are there, they likely weren't sent.

Remember that the inbox displays all emails sent to that namespace, so even if there was a typo or it got sent to a wrong address, it should still appear here as long as it was sent to {anything}@yournamespace.mailisk.net.

Increase the timeout

If emails are in the inbox but tests fail, increased delivery time might be the cause. By default, cy.mailiskSearchInbox has a 5-minute timeout. Increasing this might resolve the issue.

// timeout of 10 minutes
cy.mailiskSearchInbox(namespace, {}, { timeout: 1000 * 60 * 10 });

Note that increasing the timeout too much prolongs test failure if emails aren't sent at all.

Change the from_timestamp

Very unlikely, but another potential issue could be that the email arrives too soon. How?

In short, when you call cy.mailiskSearchInbox, by default it only looks at emails that arrived after a certain time—specifically, it ignores emails older than 15 minutes before the function was called.

For example: Suppose an email is sent, then your test runs for 20 minutes before calling cy.mailiskSearchInbox. In this case, the email would be ignored since it’s considered too old.

If this scenario applies to you, you can adjust the default from_timestamp of the function. Setting it to 0 ensures that all emails are searched, regardless of when they arrived

// set from_timestamp to 0, all emails will be searched regardless of age
cy.mailiskSearchInbox(namespace, { from_timestamp: 0 });

Wrapping It Up

When dealing with email delays in Cypress tests:

  • Check your inbox first to confirm if emails were sent or not.
  • If emails arrive late, try increasing the timeout.
  • If emails arrive too early, adjust the from_timestamp so all relevant emails are included.

Experiment with these fixes and find what works best for your tests. Happy testing!

Ready to start testing emails?
Create a free account.

Get started