Jest is a popular test framework for NodeJS. With the Puppeteer plugin we can write test using a headless browser. We can use these tests to test user sign-up end to end. This blog post covers how to test user sign-up and how to use test email accounts in your tests.

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.

Here the corresponding package.json file:

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.

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.

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.

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.