# Using test email accounts to test user sign up

If you are a software developer you know how hard it can be to test user sign-up on websites or mobile apps. Many applications rely on unique email addresses. Additionally most user sign up processes rely on email verification. How do we test this in automated tests?

# Creating email addresses in code

With MailSlurp (a free email API) you can create real email accounts on demand in code or tests. Then you can send and receive emails to imitate user sign up.

# Install MailSlurp

npm install --save mailslurp-client

# Get an API Key (it's free)

Create an account at app.mailslurp.com. Next copy the API Key and use it to initialize a MailSlurp Javascript client.

const MailSlurp = require("mailslurp-client").default;
const mailSlurp = new MailSlurp({ apiKey: "your_api_key" });

# Create test email accounts

You can create email addresses for testing user sign-up easily with the asynchronous createInbox command. This returns an inbox that has an addresses and an ID.

const inbox = await mailslurp.createInbox();

The resulting inbox will look something like this:

{
  "id": "123",
  "emailAddress": "123@mailslurp.com"
}

# Sign up the user

Once you have created an new email address use that to sign-up a user in your tests. We recommend using a browser testing suite like JestPuppetteer or CypressJS or WDIO for this.

# Complete email verification

If you sign-up process send a confirmation email to the user then you can use your MailSlurp inbox to receive the email in your tests.

// do something to sign up the user
await myApp.signUp(inbox.emailAddress);
const email = await mailSlurp.waitForLatestEmail(inbox.id);
expect(email.body).toContain("Welcome to my app!");

# That's it!

MailSlurp is a free email API for testing email related processes like user sign-up and email password reset. Check out the documentation for more.