Testing verification emails with Cypress

Custom commands

// cypress/support/commands.js
const BASE = 'https://inboxdome.com';

Cypress.Commands.add('createInbox', () =>
  cy
    .request({
      method: 'POST',
      url: `${BASE}/v1/inboxes`,
      headers: { Authorization: `Bearer ${Cypress.env('INBOXDOME_KEY')}` },
      body: {},
    })
    .its('body.inbox'),
);

Cypress.Commands.add('waitForOtp', (inboxId) =>
  cy
    .request({
      url: `${BASE}/v1/inboxes/${inboxId}/wait?for=otp&timeoutSeconds=45`,
      headers: { Authorization: `Bearer ${Cypress.env('INBOXDOME_KEY')}` },
      timeout: 50000,
    })
    .its('body.otp.value'),
);

The test

// cypress/e2e/signup.cy.js
it('signs up with email verification', () => {
  cy.createInbox().then((inbox) => {
    cy.visit('/signup');
    cy.get('[name=email]').type(inbox.emailAddress);
    cy.get('[name=password]').type('S3cure!pass');
    cy.contains('button', 'Create account').click();

    cy.waitForOtp(inbox.id).then((code) => {
      cy.get('[name=otp]').type(code);
      cy.contains('button', 'Verify').click();
      cy.contains('Welcome').should('be.visible');
    });
  });
});

Set the key once: npx cypress run --env INBOXDOME_KEY=idk_live_... or viacypress.env.json (gitignored). API reference →