Cypress Email Plugin - Test email accounts in Javascript (and Typescript)

Cypress is a powerful end-to-end browser testing framework for Javascript and Typescript. With the official MailSlurp Cypress plugin you can create real email addresses in code. Send and receive email in Cypress to test user authentication, password reset, MFA, newsletters and more.

In this example we will use MailSlurp and Cypress JS to test the sign-up process of a dummy app hosted at

sign up

Example application

Our example app is a simple React SPA. It allows a user to sign up with an email address and password. After sign up an email verification code is sent to the user’s email address. The code must be read and entered into the app to confirm the user. Once confirmed a user may login and see a picture of a happy dog.

happy dog

Install Cypress and MailSlurp plugin

To use MailSlurp with Cypress first install the MailSlurp Cypress Plugin and CypressJS.

npm install --save-dev cypress cypress-mailslurp
If you wish to write tests using the standard MailSlurp client install mailslurp-client instead.

Initialize Cypress directories

To generate test directories in your project run:

cypress open

A tree of directories will be created under a cypress folder. Edit yours to look like this:

├── fixtures
├── integration
│   └── integration-test.spec.ts
├── plugins
├── screenshots
├── support
│   ├── commands.js
│   └── index.js
└── videos

Import the MailSlurp plugin

Include the MailSlurp plugin in your cypress/support/index.{js,ts} file.

import "cypress-mailslurp";

Typescript support

If you wish to use Typescript run:

npm install --save-dev typescript
tsc --init

Then use reference comments in your tests to allow for MailSlurp type hints:

/// <reference types="cypress-mailslurp" />

Or define the type yourself like so:

import { MailSlurp } from "mailslurp-client";

declare global {
    namespace Cypress {
        interface Chainable {
            mailslurp: () => Promise<MailSlurp>;

Setup API Key

The MailSlurp plugin expects a MailSlurp API Key. Create a MailSlurp account to get a free API Key.


Then set the CYPRESS_MAILSLURP_API_KEY environment variable or use the cypress.json file env property:

Environment variable

CYPRESS_MAILSLURP_API_KEY=your-api-key cypress run

Cypress env property

  "env": {
    "MAILSLURP_API_KEY": "your-mailslurp-api-key"

Configure timeouts

For MailSlurp to send and receive emails set timeout values in cypress.json:

  "defaultCommandTimeout": 30000,
  "responseTimeout": 30000,
  "requestTimeout": 30000

Using the MailSlurp command

The MailSlurp Cypress plugin adds the mailslurp() command to the global cy object:

cy.mailslurp().then(mailslurp => /* use maislurp client methods here */)

This returns a promise that can be accessed using Cypress then commands. The MailSlurp client passed to the then methods is an instance of the standard mailslurp client. It has many methods for creating inboxes, sending emails, and receiving email content. Most methods are asynchronous.

Chaining then commands

MailSlurp methods are async so you can chain commands and access the results using Cypress then methods.

    .then(mailslurp => mailslurp.createInbox())
    .then(inbox => {

Sharing variables across tests

You can also store variables like inboxes and emails between tests using Cypress wrap and as methods.

    .then(mailslurp => mailslurp.createInbox())
    .then(inbox => {
        // save inbox id and email address to this (make sure you use function and not arrow syntax)

When accessing aliased variables that have been wrapped you must use function notation (dynamic scope) not arrow syntax (lexical scope) so that this refers to a Cypress context.

  • Good: function() { this.myAlias }
  • Bad: () => { this.myAlias }

Access the stored variables using this inside a function.

it("can access wrapped variable", function () {

Full example

Let’s put the Cypress MailSlurp plugin to work on a real example. The demo application at allows a user to sign up with an email address and password. They then receive a confirmation code by email that must be entered to confirm the account. Next the user can sign in and see a happy dog. We will next each stage next.

Setup test

Create a test file at cypress/integration/integration-test.spec.ts. (We will use Typescript but you can use Javascript if you prefer.)

/// <reference types="cypress-mailslurp" />
describe("user sign up test with mailslurp plugin", function () {
    // we will add tests here

Create an email address before test run

We want to create a new email address for each test run. Use a before block inside the describe function to do so:

// use cypress-mailslurp plugin to create an email address before test
before(function () {
    return cy.mailslurp()
        .then(mailslurp => mailslurp.createInbox())
        .then(inbox => {
            // save inbox id and email address to this (make sure you use function and not arrow syntax)

Notice how we wrap the inbox id and emailAddress as separate aliases. We will use those in other test methods to sign up a user and receive a confirmation code.

Next we can load the playground application and assert the page title is shown.

it("01 - can load the demo application", function () {
    // get wrapped email address and assert contains a mailslurp email address
    // visit the demo application
    cy.title().should('contain', 'React App');


Sign up a user

Now we fill out the sign-up form using our created email address and a test password. Notice how we access the wrapped variables using this.

it("02 - can sign up using email address", function () {
    // click sign up and fill out the form
    // use the email address and a test password
    // click the submit button

Cypress submits the form and the playground app will send our user a confirmation email.


In the next step we will wait for the email to be received and then extract the code from the body.

Receive emails with Cypress

MailSlurp inboxes have real email addresses. We can use the waitFor methods to receive emails for a given inbox. In this test we wait for an email to arrive at the inbox we created and then use a regular expression to extract the confirmation code.

it("03 - can receive confirmation code by email", function () {
    // app will send user an email containing a code, use mailslurp to wait for the latest email
        // use inbox id and a timeout of 30 seconds
        .then(mailslurp => mailslurp.waitForLatestEmail(this.inboxId, 30000, true))
        // extract the confirmation code from the email body
        .then(email => /.*verification code is (\d{6}).*/.exec(email.body!!)!![1])
        // fill out the confirmation form and submit
        .then(code => {

Lastly we submit the code to the confirmation form.


Sign in with confirmed account

Now that our user has confirmed their account we can sign in and see a welcome message.


The code to sign in and test the welcome message looks like this:

// fill out sign in form
it("04 - can sign in with confirmed account", function () {
    // use the email address and a test password
    // click the submit button
// can see authorized welcome screen
it("05 - can see welcome screen", function () {
    // click sign up and fill out the form
    cy.get("h1").should("contain", "Welcome");

If successful we should see a welcoming dog.


Building your own commands

If you want to create your own Cypress commands that use MailSlurp then install the default MailSlurp client:

npm install --save mailslurp-client

Then add your own commands to cypress/support/commands.js:

const { MailSlurp } = require("mailslurp-client");

const apiKey = "YOUR_MAILSLURP_API_KEY";
const mailslurp = new MailSlurp({ apiKey });

For instance you can create your own createInbox or waitForLatestEmail commands:

Cypress.Commands.add("createInbox", () => {
  return mailslurp.createInbox();

Cypress.Commands.add("waitForLatestEmail", (inboxId) => {
  return mailslurp.waitForLatestEmail(inboxId);

Now in Cypress tests we can do the following:

cy.createInbox().then((inbox) => {
  // { id: '...', emailAddress: '...' }


cy.waitForLatestEmail( => {
  // { subject: '...', body: '...' }

Conclusion and next steps

Cypress is a great way to test applications end to end. You can use the Cypress MailSlurp plugin to create real email addresses during tests. Use it to send and receive emails and attachments in Cypress while testing user sign-up, login, and more. For more help see the MailSlurp examples pages or the developer documentation.

As always, example code is available on GitHub.