Testing email in e2e tests with Mailosaur

In writing automated end-to-end tests for WordPress.com I have encountered some flows where I need to test that an email is received and act on something in that email (an invitation link for example).

My first attempt at this was to set up a GMail account for testing purposes and use plus addressing to have an unlimited number of unique email accounts for testing purposes. This works well for manual testing, but to have an automated script to retrieve the GMail email is a little tricky, as it means either automating their UI (very brittle and slow and even possibly against their TOS) or creating an ad-hoc IMAP connection which GMail discourages by having to lower the security of your account to allow this.

I had a look at a couple of services that provide email testing services namely Mailinator and Mailosaur. They both provide a service that offers an API you can call to retrieve emails sent to special inboxes you set up for testing purposes.

We ended up choosing Mailosaur as it automatically breaks every email down into a nicely formatted JSON object ready for us to easily inspect and extract content (and it was cheaper – win).

Mailosaur JSON structure
Mailosaur’s JSON view (some data obfuscated)

This means we can use the Mailosaur NPM package to easily retrieve, verify and visit links in emails using its API.

I love the simplicity of this service as it makes the email part of our automated end-to-end tests very easy and reliable.

Author: Alister Scott

Alister is an Excellence Wrangler for Automattic.

6 thoughts on “Testing email in e2e tests with Mailosaur”

  1. Do you know of any mail services or mock/stub mail servers for this type of testing that is open source or free? The services mentioned here are not free, although still good, well Mailinator has a free service but that one is constrained.


Comments are closed.