Jest और Node.js में ईमेल टेस्टिंग
mailer को mock करना साबित करता है कि आपके कोड ने send() कॉल किया। असली इनबॉक्स साबित करता है कि ईमेल सही subject, लिंक और कोड के साथ पहुँचा। यहाँ बताया है कि इसे Jest में कैसे जोड़ें।
helper
CI में चलने वाले integration टेस्ट के लिए Business API के प्राइवेट इनबॉक्स सबसे अच्छे हैं। QI_KEY और QI_NAMESPACE सेट करें, फिर:
// inbox.js - private test inboxes with the Business API (Node 18+)
const BASE = 'https://business.quickinbox.net';
const NS = process.env.QI_NAMESPACE; // e.g. "acme7x"
const auth = { headers: { 'X-API-Key': process.env.QI_KEY } };
// Any tag works at once: acme7x.<tag>@quickinbox.net
export function newAddress(prefix = 'test') {
const tag = `${prefix}-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 6)}`;
return { tag, address: `${NS}.${tag}@quickinbox.net` };
}
export async function waitForEmail(tag, { timeoutMs = 60_000, subject = '' } = {}) {
const since = Math.floor(Date.now() / 1000) - 60;
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
const res = await fetch(`${BASE}/api/business/mail?namespace=${NS}&since=${since}&limit=50`, auth);
if (res.ok) {
const { messages } = await res.json();
const hit = messages.find((m) => m.to === `${NS}.${tag}` && m.subject.includes(subject));
if (hit) return hit; // id, to, from, subject, text, html, spam, receivedAt
}
await new Promise((r) => setTimeout(r, 2000));
}
throw new Error(`No email for ${tag} within ${timeoutMs} ms`);
}क्योंकि आपके namespace पर हर टैग एक नया पता है, आपको create कॉल की ज़रूरत नहीं, और पैरेलल Jest वर्कर कभी इनबॉक्स शेयर नहीं करते।
वेलकम ईमेल का टेस्ट
// welcome.test.js
import { newAddress, waitForEmail } from './inbox.js';
import { createUser } from '../src/users.js'; // your code that sends the email
jest.setTimeout(90_000);
test('sends a welcome email with a working dashboard link', async () => {
const { tag, address } = newAddress('welcome');
await createUser({ email: address, name: 'Test User' });
const email = await waitForEmail(tag, { subject: 'Welcome' });
expect(email.subject).toBe('Welcome to Acme, Test User');
expect(email.html).toContain('https://app.example.com/dashboard');
expect(email.html).not.toContain('localhost');
});रीसेट कोड का टेस्ट
test('password reset email has a 6-digit code', async () => {
const { tag, address } = newAddress('reset');
await requestPasswordReset(address);
const email = await waitForEmail(tag, { subject: 'Reset' });
expect(email.text).toMatch(/\b\d{6}\b/);
});Unit टेस्ट तेज़ रखें
हर सेव पर चलने वाले unit टेस्ट के लिए mocks रखें। ये असली-इनबॉक्स टेस्ट अलग Jest project के रूप में या सिर्फ़ CI पर चलाएँ, जैसे jest --selectProjects integration से।
डिलीवरेबिलिटी भी जाँचें
Business namespace पर हर मैसेज के साथ स्पैम स्कोर और SPF, DKIM और DMARC के नतीजे आते हैं। टूटा हुआ sender सेटअप पकड़ने के लिए expect(email.spam.score).toBeGreaterThan(70) जोड़ें। देखें ईमेल टेम्पलेट टेस्ट करना।
इससे जुड़ी गाइड
- pytest के साथ Python में ईमेल टेस्टिंग
- मैजिक लिंक लॉगिन कैसे टेस्ट करें
- ईमेल OTP और 2FA कोड कैसे टेस्ट करें
- GitHub Actions में ईमेल कैसे टेस्ट करें
- ईमेल टेस्टिंग API की जानकारी
प्राइवेट इनबॉक्स पाएँ: QuickInbox Business API में हर महीने 5,000 ईमेल वाला फ़्री प्लान है, और पेड प्लान $9 से। फ़्री अकाउंट बनाएँ या API डॉक्स पढ़ें।