Appearance
Cypress Guide
If you haven't already, see Getting Started. The following examples will assume you have already received your API key and created a namespace.
cypress-mailisk
Mailisk provides an official library with a custom set of commands for testing email in Cypress. These commands wrap the API Reference endpoints.
Install cypress-mailisk
sh
npm install --save-dev cypress-mailiskAfter installing the package add the following in your project's cypress/support/e2e.js:
js
import "cypress-mailisk";Setup API Key
To be able to use the API you will need to add your API key to cypress.config.js:
js
module.exports = defineConfig({
env: {
MAILISK_API_KEY: "YOUR_API_KEY",
},
});Usage
The cypress-mailisk plugin provides additional commands which can be accessed on the cypress object, for example cy.mailiskSearchInbox(). These commands extend the Chainable object which allows you to use the then() method to chain commands.
cy.mailiskSearchInbox
This is the main command to interact with Mailisk, it wraps the Search Inbox endpoint.
js
cy.mailiskSearchInbox("yournamespace", { to_addr_prefix: "test.user@" }).then((response) => {
const emails = response.data;
// ...
});This Cypress command does a few extra things out of the box compared to calling the raw API directly:
- By default it uses the
waitflag. This means the call won't return until at least one email is received. Disabling this flag viawait: falsecan cause it to return an empty response immediately. - The request timeout is adjustable by passing
timeoutin the request options. By default it uses a timeout of 5 minutes. - By default it returns emails in the last 15 minutes. This ensures that only new emails are returned. This can be overridden by passing the
from_timestampparameter (from_timestamp: 0will disable filtering by email age).
js
// timeout of 5 minute
cy.mailiskSearchInbox(namespace);
// timeout of 1 minute
cy.mailiskSearchInbox(namespace, {}, { timeout: 1000 * 60 });
// returns immediately, even if the result would be empty
cy.mailiskSearchInbox(namespace, { wait: false });For the full list of filters and their description see the Search Inbox endpoint reference.
Filter by TO address
The to_addr_prefix option allows filtering by the email's TO address. Specifically the TO address has to start with this.
For example, if someone sends an email to my-user-1@yournamespace.mailisk.net, you can filter it by using my-user-1@:
js
cy.mailiskSearchInbox(namespace, {
to_addr_prefix: "my-user-1@",
});Filter by FROM address
The from_addr_includes option allows filtering by the email's FROM address. Specifically the TO address has to include this. Note that this is different from the to address as it is includes not prefix.
For example, if someone sends an email from the example.com domain we could filter like so:
js
cy.mailiskSearchInbox(namespace, {
from_addr_includes: "@example.com",
});If we know a specific email address we want to listen to we can do this:
js
cy.mailiskSearchInbox(namespace, {
from_addr_includes: "no-reply@example.com",
});Filter by Subject
The subject_includes option allows filtering by the email's Subject. Specifically the Subject has to include this (case-insensitive).
If we're testing password reset that sends an email with the subject Password reset request. We could filter by something like this:
js
cy.mailiskSearchInbox(namespace, {
subject_includes: "password reset request",
});SMS Testing
If your plan includes SMS, request and activate a phone number in the dashboard (see SMS Testing). Once a number is active you can poll it directly from Cypress using cy.mailiskSearchSms.
cy.mailiskSearchSms
js
const smsNumber = Cypress.env("MAILISK_SMS_NUMBER");
cy.mailiskSearchSms(
smsNumber,
{
body: "Your login code",
},
{
timeout: 1000 * 120,
}
).then(({ data }) => {
expect(data).to.not.be.empty;
const sms = data[0];
const otp = sms.body.match(/(\d{6})/)[1];
// use OTP in your flow
});This command mirrors cy.mailiskSearchInbox:
- It defaults to
wait: true, so the request will keep polling until at least one SMS matches the filters. Usewait: falseto opt out. - Pass a third
optionsargument to override the default 5 minute timeout for particularly long MFA flows. - The second argument accepts the same filters that the Search SMS API exposes, making it easy to target a specific OTP or sender.
Filter by body or sender
Use the body filter for substring matches inside the SMS payload (case-insensitive) and from_number to restrict the sender. from_number behaves as a prefix search, so +1555 would match all senders that start with that code.
js
cy.mailiskSearchSms(smsNumber, {
body: "verification code",
from_number: "+1555123",
});To avoid stale data between runs, prefer unique state in your application (e.g. unique account IDs) rather than relying on offset/limit pagination.
List SMS numbers
When you need to confirm which phone numbers are available to your API key (for example, before setting MAILISK_SMS_NUMBER in CI), call cy.mailiskListSmsNumbers().
js
cy.mailiskListSmsNumbers().then(({ data }) => {
expect(data).to.not.be.empty;
const [{ phone_number, status }] = data;
expect(status).to.equal("active");
Cypress.env("MAILISK_SMS_NUMBER", phone_number);
});Pairing this with cy.mailiskSearchSms ensures your specs always target an active, approved number.
Common test cases
Password reset page
This example demonstrates going to a password reset page, requesting a new password, receiving reset code link via email and finally setting the new password.
js
describe("Test password reset", () => {
let resetLink;
const namespace = "yournamespace";
const testEmailAddr = `test.test@${namespace}.mailisk.net`;
it("Starts a password reset", () => {
cy.visit("https://example.com/password_reset");
cy.get("#email_field").type(testEmailAddr);
});
it("Gets a password reset email", () => {
cy.mailiskSearchInbox(namespace, {
to_addr_prefix: testEmailAddr,
subject_includes: "password",
}).then((response) => {
expect(response.data).to.not.be.empty;
const email = response.data[0];
expect(email.subject).to.equal("Please reset your password");
resetLink = email.text.match(/.(https:\/\/example.com\/password_reset\/.*)>\n*/)[1];
expect(resetLink).to.not.be.undefined;
});
});
it("Goes to password reset link", () => {
cy.visit(resetLink);
cy.title().should("contain", "Change your password");
cy.get("#password").type("MyNewPassword");
cy.get("#password_confirmation").type("MyNewPassword");
cy.get("form").submit();
});
});SMS-based MFA
This spec triggers an SMS one-time passcode and asserts it directly inside Cypress:
js
describe("SMS MFA", () => {
const smsNumber = Cypress.env("MAILISK_SMS_NUMBER");
it("receives and uses OTP", () => {
cy.visit("https://example.com/login");
cy.contains("Use SMS instead").click();
cy.get("#phone").type(smsNumber);
cy.contains("Send code").click();
cy.mailiskSearchSms(smsNumber, {
body: "Your login code",
}).then(({ data }) => {
expect(data).to.not.be.empty;
const sms = data[0];
const otp = sms.body.match(/(\d{6})/)[1];
cy.get("#otp").type(otp);
cy.contains("Verify").click();
});
});
});For longer-lived OTP flows combine body with from_number or from_date so the command only resolves once the relevant verification SMS has landed.
Breaking changes
- With version v2.0.0 the default
from_timestamphas been changed from the past 5 seconds to the past 15 minutes. Since this can break existing tests a new major version has been released.
