Test user sign up with NodeJS and Jest (testing email verification codes)

An example

Let’s imagine we have an application that allows users to sign-up. When they do they are sent a confirmation code via email. They are then asked to enter that code to verify their account. How do we test this?

Test email accounts

Services like MailSlurp provide test email addresses that can be used via HTTP APIs. If we create a new email address during each test run we can sign up with its unique email address and then parse the confirmation code to verify the account.

Setup a test environment

To use Jest and Puppeteer we need NodeJS and some packages.

npm install -s jest jest-puppeteer mailslurp-client

Here the corresponding package.json file:

  "dependencies": {
    "jest": "^24.1.0",
    "jest-puppeteer": "^4.0.0",
    "mailslurp-client": "^3.0.15"

Write a test to sign-up a user

Here is a simple test that uses Jest and Jest-Puppeteer to load an imagined login page and enter an email and password.

describe("test user sign up", () => {
  it("can sign up as new user", async () => {
    // we will write this function next
    const inbox = await generateNewEmailAddress();
    await page.type('input[name="password"]', password);
    await page.type('input[name="email"]', inbox.email);
    await page.$eval('input[name="submit"]', (btn) => btn.click());

Generating new email addresses

For our test to sign up a new user every time it executes it needs to generate new email addresses on demand. Luckily we can use MailSlurp for that. MailSlurp is an test email account API that we can use to create email addresses. Let’s use the official package in our test.

// test continued
import { MailSlurp } from "mailslurp-client"
const api = new MailSlurp({ apiKey: "test" })

async function generateNewEmailAddress() {
  return  = await api.createInbox()

This async function will create a new email account for our test method and return its ID and email address. We use that to sign up as a new user and then we make another call to MailSlurp to receive the confirmation code.

Receiving real emails

To receive the verification code that is sent to our new email address we use the MailSlurp API again to fetch and parse it.

// get the verification code for a given inboxId
async function getVerificationCode(inboxId) {
  const emails = await api.getEmails(inboxId, { minCount: 1 });
  const latestEmail = await api.getEmail(emails[0].id);
  const verificationCode = /code = (.*)/.exec(latestEmail.content)[1];
  return verificationCode;

Then we can use that verification code for the next stage of our test to submit to the application and verify the test email account.

In practice

Using an email testing service like MailSlurp to test user sign up is a great way to increase your application’s test coverage. Many applications rely on email for essential actions like user sign up, invoicing, and event notifications. With MailSlurp you can create unlimited unique email addresses in code and use them to send and receive real email.

For more information please see the MailSlurp documentation.