-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathbun.spec.js
53 lines (49 loc) · 1.57 KB
/
bun.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import {test, expect } from "bun:test";
//<gen>bun_import_nodemailer
import nodemailer from "nodemailer";
//</gen>
//<gen>bun_import_mailslurp
import {MailSlurp} from "mailslurp-client";
//</gen>
test("send email", async () => {
// configure mailslurp test inbox
const apiKey = process.env.API_KEY;
if (!apiKey) {
throw new Error("Please set API_KEY environment variable")
}
//<gen>bun_create_mailserver
const mailslurp = new MailSlurp({apiKey})
const {
smtpServerHost, smtpServerPort,
smtpUsername, smtpPassword
} = await mailslurp.getImapSmtpAccessDetails();
//</gen>
// create inboxes to send and receive with
const inbox1 = await mailslurp.createInboxWithOptions({inboxType: 'SMTP_INBOX'})
const inbox2 = await mailslurp.createInboxWithOptions({inboxType: 'SMTP_INBOX'})
//<gen>bun_nodemailer_setup
const transporter = nodemailer.createTransport({
host: smtpServerHost,
port: smtpServerPort,
secure: false,
auth: {
user: smtpUsername,
pass: smtpPassword
},
});
//</gen>
//<gen>bun_send_email
const sent = await transporter.sendMail({
from: inbox1.emailAddress,
to: inbox2.emailAddress,
subject: "Test email",
text: "Hello world!",
});
//</gen>
expect(sent).toBeTruthy()
//<gen>bun_read_email
const email = await mailslurp.waitForLatestEmail(inbox2.id, 30_000, true);
expect(email.subject).toContain("Test email")
expect(email.body).toContain("Hello world!")
//</gen>
});