跳至主要收件區
E2E 自動化測試範本 · VITEST / NODE.JS

在 Vitest / Node.js 穩定測試電子郵件與 OTP 驗證,無需 SDK 綁定。

於每個 Vitest / Node.js 測試前呼叫 POST /v1/mailboxes 建立獨立收件匣,透過 Fetch SSE 等待 message.ready 後擷取驗證碼。

三步驟確定性流程

1. 每個測試建立獨立信箱

呼叫 POST /v1/mailboxes 取得專屬地址與 Bearer token。

2. 訂閱 SSE 即時事件

以 Authorization: Bearer 連線 GET /v1/mailboxes/me/events。

3. 擷取驗證碼並清理

讀取訊息內容後於 teardown 呼叫 DELETE /v1/mailboxes/me。

可直接複製的 Vitest / Node.js 測試範本

TypeScript
import { describe, it, expect } from 'vitest';

const API = 'https://api.inboxflick.com/v1';

describe('auth email verification flow', () => {
  it('delivers 6-digit OTP to an isolated Bearer inbox', async () => {
    const res = await fetch(`${API}/mailboxes`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: '{}',
    });
    const { data } = await res.json();
    const token = data.access_token as string;
    const address = data.mailbox.address as string;

    try {
      await triggerYourAppSignupEmail(address);

      // Stream SSE until message.ready fires
      const sse = await fetch(`${API}/mailboxes/me/events`, {
        headers: { Authorization: `Bearer ${token}` },
      });
      const reader = sse.body!.getReader();
      const decoder = new TextDecoder();
      let buf = '';
      let messageId = '';

      while (!messageId) {
        const { value, done } = await reader.read();
        if (done) break;
        buf += decoder.decode(value, { stream: true });
        const m = buf.match(/event: message\.ready\s+data: (\{[^\n]+\})/);
        if (m) messageId = JSON.parse(m[1]).message_id;
      }
      await reader.cancel();

      const msgRes = await fetch(`${API}/mailboxes/me/messages/${messageId}`, {
        headers: { Authorization: `Bearer ${token}` },
      });
      const msg = await msgRes.json();
      expect(msg.data.text_body).toMatch(/\b\d{6}\b/);
    } finally {
      await fetch(`${API}/mailboxes/me`, {
        method: 'DELETE',
        headers: { Authorization: `Bearer ${token}` },
      });
    }
  });
});

並行 CI Worker?透過 Pro API 一次建立 1–20 個信箱

升級 Pro($1.99/月或 $8.99 永久)即可呼叫批量端點並行建立最多 20 個收件匣。

POST https://api.inboxflick.com/v1/member/mailboxes/batch
Authorization: Bearer <PRO_MEMBER_JWT>
Content-Type: application/json

{ "count": 20, "is_permanent": false }

以 $8.99 永久解鎖並行 CI 郵件測試

不限每日次數、1–20 批量 API 與 Pro 專屬網域。

playwright cypress pytest API 文件 Pro 會員

CI 郵件測試常見問題

Vitest / Node.js 需要安裝額外 SDK 嗎?
不需要,原生 fetch() 或 requests 即可串接。
並行測試如何避免單 IP 限制?
使用 Pro JWT 呼叫 /v1/member/mailboxes/batch 即可。
可擷取魔術連結與 HTML 嗎?
可以,API 同時提供 text_body 與淨化後的 html_body。