Skip to content

Commit

Permalink
Add unit tests for Nodemailer
Browse files Browse the repository at this point in the history
 * Add unit tests for nodemailer
 * Make adjustments to Nodemailer module to work wiht Unit Tests
 * Add Nodemailer env vars to CI
 * Add nodemailer env vars to docker compose
 * Add env vars in right place in CI
 * Remove nodemailer env vars
 * Add dotenv to test script
 * Remove dotenv import
 * return a promise with resolve/reject
 * Use objectContaining/arrayContaining to read response
 * Add mailhog under services
  • Loading branch information
SerpentBytes committed Feb 12, 2023
1 parent aa9a70d commit 7481522
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ jobs:
image: motoserver/moto:4.1.1
ports:
- '5053:5000'
mailhog:
image: jcalonso/mailhog:latest
ports:
- 8025:8025
- 1025:1025
steps:
- name: Checkout code
uses: actions/checkout@v3
Expand Down
28 changes: 16 additions & 12 deletions app/lib/notifications.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,22 @@ const initializeTransport = () => {
};

const sendNotification = async (emailAddress: string, subject: string, text: string) => {
try {
const transport = initializeTransport();
logger.debug(`Sending notification to ${emailAddress}`);
return await transport.sendMail({
from: NOTIFICATIONS_EMAIL_USER,
to: emailAddress,
subject,
text,
});
} catch (error) {
logger.warn('Unable to send notification', error);
}
return new Promise<String>(async (resolve, reject) => {
try {
const transport = initializeTransport();
logger.debug(`Sending notification to ${emailAddress}`);
await transport.sendMail({
from: NOTIFICATIONS_EMAIL_USER,
to: emailAddress,
subject,
text,
});
resolve(`Sent notification to ${emailAddress}`);
} catch (error) {
logger.warn('Unable to send notification', error);
reject(`failed to send notification to ${emailAddress}`);
}
});
};

export default sendNotification;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"setup": "run-s db:generate db:push db:seed",
"start": "cross-env NODE_ENV=production node ./build/server.js",
"start:e2e": "cross-env NODE_ENV=production node --require dotenv/config ./build/server.js",
"test": "vitest",
"test": "NODE_OPTIONS=--require=dotenv/config vitest",
"test:e2e:dev": "cross-env PORT=8080 start-server-and-test dev http://localhost:8080 \"playwright test\"",
"pretest:e2e:run": "npm run build",
"test:e2e:run": "cross-env PORT=8811 start-server-and-test start:e2e http://localhost:8811 \"playwright test\"",
Expand Down
29 changes: 29 additions & 0 deletions test/unit/notifications.server.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import sendNotification from '../../app/lib/notifications.server';
const { NOTIFICATIONS_EMAIL_USER } = process.env;
describe('Notification server lib function test', () => {
const recipient: string = '[email protected]';
const subject: string = 'Sample subject';
const text: string = 'Sample text';

test('sending email notification using sendNotification() ', async () => {
const response = await sendNotification(recipient, subject, text);
expect(response).toBe(`Sent notification to ${recipient}`);
});

test('verifying MailHog received test email notification via SendNotification() ', async () => {
const response = await fetch(`http://localhost:8025/api/v2/search?kind=to&query=${recipient}`);
const responseToJSON = await response.json();
const mostRecentMessage = responseToJSON.items[0];
expect(mostRecentMessage).toEqual(
expect.objectContaining({
Content: expect.objectContaining({
Headers: expect.objectContaining({
To: expect.arrayContaining([recipient]),
From: expect.arrayContaining([NOTIFICATIONS_EMAIL_USER]),
}),
Body: expect.stringMatching(text),
}),
})
);
});
});

0 comments on commit 7481522

Please sign in to comment.