swaf/test/_mail_server.ts

39 lines
1.3 KiB
TypeScript
Raw Normal View History

import MailDev, {Mail} from "maildev";
export const MAIL_SERVER = new MailDev({
ip: 'localhost',
});
export async function setupMailServer(): Promise<void> {
await new Promise((resolve, reject) => MAIL_SERVER.listen((err?: Error) => {
if (err) reject(err);
else resolve();
}));
}
export async function teardownMailServer(): Promise<void> {
await new Promise((resolve, reject) => MAIL_SERVER.close((err?: Error) => {
if (err) reject(err);
else resolve();
}));
}
export async function popEmail(): Promise<Record<string, unknown> | null> {
return await new Promise<Record<string, unknown> | null>((resolve, reject) => {
MAIL_SERVER.getAllEmail((err: Error | undefined, emails: Mail[]) => {
if (err) return reject(err);
if (emails.length === 0) return resolve(null);
const email = emails[0];
expect(email).toBeDefined();
expect(email.id).toBeDefined();
return resolve(new Promise<Record<string, unknown>>((resolve, reject) => {
MAIL_SERVER.deleteEmail(email.id as string, (err: Error | undefined) => {
if (err) return reject(err);
resolve(email as Record<string, unknown>);
});
}));
});
});
}