AssertInbox

Detox React Native email OTP test

Detox drives a real React Native app on a simulator or emulator, and its tests run in Node with Jest. That split is useful for email: the app on the device asks your backend to send a sign-up code, and the test code, running on your machine or CI runner, reads that email through AssertInbox and types the code back into the app. This guide shows the complete loop with element(by.id(...)).typeText, one inbox tag per test and the CommonJS client file. It targets Detox 20 or newer with its Jest runner, and Node 18 or newer.

How it works

Mail sent to <namespace>.<tag>@assertinbox.com is stored for your key. GET /v1/wait long-polls and returns the newest matching message as JSON, including id, subject, otp and magic_link, or 408 if nothing arrived. Messages are deleted after 24 hours. Detox test files execute in a Node process, not on the phone, so they can call this API with fetch while the app waits on its verification screen. The API key never goes onto the device and never into the app bundle.

Get an API key

Get one in a click at /start, or with the request below. One request creates a key and your private namespace. No email address or card is needed. The key is shown once, so keep it in your secret manager or CI secrets.

curl -s -X POST https://assertinbox.com/v1/keys
# {"api_key":"ai_...","namespace":"k3x9a1bz","address_template":"k3x9a1bz.<tag>@assertinbox.com"}

Export ASSERTINBOX_KEY (the api_key) and ASSERTINBOX_NAMESPACE (the namespace). If the call returns 403 signups_closed, the beta is not open for new keys. Use inboxes only for services you own or control; see the terms. Limits and error codes are on the limits page.

Install the client

Detox's Jest runner loads test files as CommonJS, so use the CommonJS copy of the client. It is one file with no dependencies. Put it in your e2e folder:

curl -o e2e/assertinbox.cjs https://assertinbox.com/sdk/assertinbox.cjs

The methods used here are uniqueTag(), address(tag), waitForOtp() and delete(tag). The client page lists the rest.

Test IDs belong to your app

Detox finds views by their testID prop, so the IDs in the test below, signup-email, signup-submit, otp-input, otp-submit and welcome-title, are placeholders. Replace them with the IDs in your screens. On a text field the prop looks like <TextInput testID="signup-email" />. Put IDs on the controls the test touches and on one element that only exists after success, so the final assertion cannot pass on a screen that is merely similar.

Configure Jest for long waits

Detox's default Jest timeout is meant for UI actions, and a real email can take longer. This is a minimal e2e/jest.config.js in the shape Detox generates, with a two-minute test timeout. The timeout is a ceiling: a wait returns as soon as the email arrives, so a healthy run is not slower for it.

/** @type {import('@jest/types').Config.InitialOptions} */
module.exports = {
  rootDir: "..",
  testMatch: ["<rootDir>/e2e/**/*.test.js"],
  testTimeout: 120000,
  maxWorkers: 1,
  globalSetup: "detox/runners/jest/globalSetup",
  globalTeardown: "detox/runners/jest/globalTeardown",
  reporters: ["detox/runners/jest/reporter"],
  testEnvironment: "detox/runners/jest/testEnvironment",
  verbose: true,
};

The test file

Save this as e2e/signup-otp.test.js and run your usual npx detox test command. Every test builds its own tag in beforeEach, relaunches the app with a fresh state, and deletes its messages in afterEach even when it failed. The wait for the code uses two retries of the 20-second wait, so a slow provider has about a minute.

const { AssertInbox } = require("./assertinbox.cjs");

const inbox = new AssertInbox({
  key: process.env.ASSERTINBOX_KEY,
  namespace: process.env.ASSERTINBOX_NAMESPACE,
});

describe("email sign-up with a one-time code", () => {
  let tag;
  let email;

  beforeEach(async () => {
    tag = inbox.uniqueTag("detox");
    email = inbox.address(tag);
    await device.launchApp({ newInstance: true, delete: true });
  });

  afterEach(async () => {
    await inbox.delete(tag).catch(() => {});
  });

  it("verifies the address with the emailed code", async () => {
    await element(by.id("signup-email")).typeText(email);
    await element(by.id("signup-submit")).tap();

    await waitFor(element(by.id("otp-input"))).toBeVisible().withTimeout(10000);

    const code = await inbox.waitForOtp(tag, { retries: 2 });
    expect(code).toMatch(/^[0-9]{6}$/);

    await element(by.id("otp-input")).typeText(code);
    await element(by.id("otp-submit")).tap();

    await waitFor(element(by.id("welcome-title"))).toBeVisible().withTimeout(10000);
  });

  it("keeps the user on the code screen after a wrong code", async () => {
    await element(by.id("signup-email")).typeText(email);
    await element(by.id("signup-submit")).tap();
    await waitFor(element(by.id("otp-input"))).toBeVisible().withTimeout(10000);

    const real = await inbox.waitForOtp(tag, { retries: 2 });
    const wrong = real === "000000" ? "111111" : "000000";

    await element(by.id("otp-input")).typeText(wrong);
    await element(by.id("otp-submit")).tap();

    await expect(element(by.id("otp-input"))).toBeVisible();
    await expect(element(by.id("welcome-title"))).not.toExist();
  });
});

Three details are specific to mobile. First, the test waits for the code screen before it reads the inbox, so a failed network call in the app shows up as a clear Detox timeout and not as a missing email. Second, typeText sends the characters through the on-screen keyboard, which is slow for a long address; if it is flaky in your app, replaceText sets the field value in one step and is a fine choice for the address, while typeText stays closer to what a user does. Third, delete: true on launch removes the app's stored data, so a previous run's session cannot skip the sign-up screen.

Magic links on a device

If your app signs in from a link in the email, the link usually opens through a universal link or a custom scheme. Read it with inbox.waitForLink(tag) and pass it to device.openURL({ url }), which hands it to the app the way the operating system would. That tests your deep-link handling without a mail client on the device. A link on your own web domain only opens the app when the device has the association files set up, so use a debug scheme link when the staging build does not.

Run it in CI

Store ASSERTINBOX_KEY and ASSERTINBOX_NAMESPACE as secrets in your CI provider and pass them as environment variables to the step that runs detox test. Never print the key. Point the app build at a staging backend that sends real mail, since a mocked mailer sends nothing to read. Emulator and simulator start-up dominates the run time, so keep the email tests in a small suite and run them on the pipeline you already use for device tests.

Troubleshooting

Related guides: Appium email OTP test, Jest email verification test.

Next steps

For unique tags, the trigger-then-wait order, retries and cleanup in one place, read Reliable email tests. More stacks are on the guides page.