Vitest / Node.js で sleep() なし・低コストに OTP メール認証を自動テスト。
各 Vitest / Node.js テストで POST /v1/mailboxes から独立した一時メールを発行し、Fetch SSE で message.ready を待機して OTP を抽出できます。
3ステップの決定的ワークフロー
1. テストごとに受信箱を作成
POST /v1/mailboxes で一意のアドレスと Bearer access_token を取得します。
2. SSE で即時待機
GET /v1/mailboxes/me/events を購読し message.ready イベントを受け取ります。
3. OTP 抽出と削除
GET /v1/mailboxes/me/messages/{id} を読み取り、最後に DELETE /v1/mailboxes/me を呼びます。
コピーして使える Vitest / Node.js OTP テストコード
TypeScriptimport { 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 実行には Pro の 1〜20件一括 API を活用
Pro(月額 $1.99 / 買い切り $8.99)なら 1回の API 呼び出しで最大20件の並列受信箱と専用隔離ドメインを確保できます。
POST https://api.inboxflick.com/v1/member/mailboxes/batch
Authorization: Bearer <PRO_MEMBER_JWT>
Content-Type: application/json
{ "count": 20, "is_permanent": false } 並列 CI メールテストを買い切り $8.99 で解放
1日の上限なし・1〜20一括 API・専用ドメイン・10,000件永久保持。
CI メールテスト FAQ
Vitest / Node.js 専用の SDK パッケージは必要ですか?
不要です。標準の fetch() や Python requests だけで動作します。
並列テストでゲスト制限を回避するには?
Pro JWT を使って POST /v1/member/mailboxes/batch を呼び出してください。
マジックリンクや HTML メールも取得できますか?
はい、text_body とサニタイズ済み html_body の両方を返します。