From dde1ede83794cb3bb4878ebf8b8ce3f49c63ba71 Mon Sep 17 00:00:00 2001 From: TD <50856799+SerpentBytes@users.noreply.github.com> Date: Wed, 15 Feb 2023 15:15:01 -0500 Subject: [PATCH] Add unit tests for Nodemailer (#184) * 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 --- .github/workflows/ci.yaml | 5 ++++ package.json | 2 +- test/unit/notifications.server.test.ts | 40 ++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 test/unit/notifications.server.test.ts diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 2a6cc0d3..a115327e 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -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 diff --git a/package.json b/package.json index 7eb3f8a9..7df4060b 100644 --- a/package.json +++ b/package.json @@ -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\"", diff --git a/test/unit/notifications.server.test.ts b/test/unit/notifications.server.test.ts new file mode 100644 index 00000000..24eedb94 --- /dev/null +++ b/test/unit/notifications.server.test.ts @@ -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 = 'dev@starchart.invalid'; + 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), + }), + }), + ]) + ); + }); +});