Skip to content

Commit

Permalink
Add unit tests for Nodemailer (#184)
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
 * Remove nested Promise
 * Use objectContaining/arrayContaining in all notification tests
 * Remove test log message
 * Remove await and extra catch
 * Add await and remove catch
 * Eliminate test dependency
 * Remove types where they are already inferred
 * Test against the whole item array
 * Assign value to messageId inside if block to prevent typecheck fail
 * remove exclamation mark after messageId
 * remove if block
 * use invariant to throw if sentMailResponse.messageId is undefined
 * Add blank lines for better readibility
 * Pass responseToJSON.items directly to expect
 * Add cross-env to test script
 * Remove invariant
  • Loading branch information
SerpentBytes authored Feb 15, 2023
1 parent 682e4c1 commit dde1ede
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,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
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": "cross-env 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
40 changes: 40 additions & 0 deletions test/unit/notifications.server.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import sendNotification from '../../app/lib/notifications.server';

const { NOTIFICATIONS_EMAIL_USER } = process.env;

describe('Notification server lib function test', () => {
const recipient = '[email protected]';
const subject = 'Sample subject';
const text = 'Sample text';

test('sending and receiving email notification using sendNotification()', async () => {
const sentMailResponse = await sendNotification(recipient, subject, text);

let messageId = sentMailResponse?.messageId;

expect(sentMailResponse).toEqual(
expect.objectContaining({
accepted: expect.arrayContaining([recipient]),
})
);

const mailhogResponse = await fetch(
`http://localhost:8025/api/v2/search?kind=to&query=${recipient}`
);
const responseToJSON = await mailhogResponse.json();
expect(responseToJSON.items).toEqual(
expect.arrayContaining([
expect.objectContaining({
Content: expect.objectContaining({
Headers: expect.objectContaining({
'Message-ID': expect.arrayContaining([messageId]),
'To': expect.arrayContaining([recipient]),
'From': expect.arrayContaining([NOTIFICATIONS_EMAIL_USER]),
}),
Body: expect.stringMatching(text),
}),
}),
])
);
});
});

0 comments on commit dde1ede

Please sign in to comment.