2020-11-14 18:16:58 +01:00
|
|
|
import MailDev, {Mail} from "maildev";
|
2020-07-08 10:49:29 +02:00
|
|
|
|
|
|
|
export const MAIL_SERVER = new MailDev({
|
|
|
|
ip: 'localhost',
|
|
|
|
});
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
export async function setupMailServer(): Promise<void> {
|
2021-01-21 15:58:03 +01:00
|
|
|
await new Promise<void>((resolve, reject) => MAIL_SERVER.listen((err?: Error) => {
|
2020-07-08 10:49:29 +02:00
|
|
|
if (err) reject(err);
|
|
|
|
else resolve();
|
|
|
|
}));
|
2020-09-25 23:42:15 +02:00
|
|
|
}
|
2020-07-08 10:49:29 +02:00
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
export async function teardownMailServer(): Promise<void> {
|
2021-01-21 15:58:03 +01:00
|
|
|
await new Promise<void>((resolve, reject) => MAIL_SERVER.close((err?: Error) => {
|
2020-07-08 10:49:29 +02:00
|
|
|
if (err) reject(err);
|
|
|
|
else resolve();
|
|
|
|
}));
|
2020-09-25 23:42:15 +02:00
|
|
|
}
|
2020-11-14 18:16:58 +01:00
|
|
|
|
|
|
|
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>);
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|