Cypress
Cypress tests talk to the API with cy.request; no library is needed. Put the key and the
project into cypress.env.json (not committed) or the environment: CYPRESS_WAITFORMAIL_API_KEY,
CYPRESS_WAITFORMAIL_PROJECT, CYPRESS_WAITFORMAIL_DOMAIN. The commands read them with cy.env
(Cypress 16 and later, where Cypress.env() is gone), which keeps the key out of the Command Log.
Commands that make addresses and wait
// cypress/support/commands.js
const KEYS = [
'WAITFORMAIL_API_URL',
'WAITFORMAIL_API_KEY',
'WAITFORMAIL_PROJECT',
'WAITFORMAIL_DOMAIN',
];
Cypress.Commands.add('newAddress', (prefix = 'test') =>
cy.env(KEYS, { log: false }).then(({ WAITFORMAIL_DOMAIN }) => {
const random = Math.random().toString(36).slice(2, 12);
return `${prefix}-${random}@${WAITFORMAIL_DOMAIN}`;
}),
);
Cypress.Commands.add('waitForMessage', (to, timeoutSeconds = 30) =>
cy.env(KEYS, { log: false }).then((env) => {
const api = env.WAITFORMAIL_API_URL ?? 'https://api.waitformail.com';
return cy
.request({
url: `${api}/v1/projects/${env.WAITFORMAIL_PROJECT}/messages/wait`,
qs: { to, timeout: timeoutSeconds },
headers: { authorization: `Bearer ${env.WAITFORMAIL_API_KEY}` },
// The service holds the request open until the message arrives.
timeout: (timeoutSeconds + 15) * 1000,
log: false,
})
.its('body');
}),
);A sign-up test
// cypress/e2e/signup.cy.js
describe('sign up', () => {
it('confirms the email address', () => {
cy.newAddress('signup').then((email) => {
cy.visit('/signup');
cy.get('input[name=email]').type(email);
cy.contains('button', 'Create account').click();
cy.waitForMessage(email).then((message) => {
expect(message.subject).to.eq('Confirm your email address');
const link = message.links.find((l) => l.text === 'Confirm your email address');
cy.visit(link.url);
});
cy.contains('h1', 'Email confirmed');
});
});
});cy.visit only opens pages of your application's origin. If the link in the email points
elsewhere, read it with cy.request(link.url) instead, or check message.codes.