Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for Nodemailer #184

Merged
merged 1 commit into from
Feb 15, 2023

Conversation

SerpentBytes
Copy link
Contributor

@SerpentBytes SerpentBytes commented Feb 12, 2023

Description

Resolves #147 by adding initial Unit Tests to test the functionality of Nodemailer. I had to adjust the sendNotification function for unit testing. Also, I have modified the tests script to require dotenv/config to read environment variables defined in .env.example.

Steps to Test

  1. Setup the project locally (make sure Docker is running)
  2. Run npm test (all tests should pass)
  3. Navigate to the MailHog interface at http://localhost:8025/ to see the test notification:

list view

detailed view

@SerpentBytes SerpentBytes force-pushed the issue-126 branch 2 times, most recently from 56dcce2 to 1f2582d Compare February 12, 2023 04:57
@SerpentBytes SerpentBytes marked this pull request as ready for review February 12, 2023 05:00
@SerpentBytes SerpentBytes self-assigned this Feb 12, 2023
@SerpentBytes SerpentBytes added category: testing Unit tests, end to end tests category: notifications A service to notify users about their certificates/domains category: back end Back end part of our web service labels Feb 12, 2023
@SerpentBytes SerpentBytes added this to the Milestone 0.2 milestone Feb 12, 2023
@Eakam1007 Eakam1007 self-requested a review February 12, 2023 06:54
@Eakam1007 Eakam1007 self-requested a review February 12, 2023 08:04
Eakam1007
Eakam1007 previously approved these changes Feb 12, 2023
Copy link
Contributor

@Eakam1007 Eakam1007 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

app/lib/notifications.server.ts Outdated Show resolved Hide resolved
app/lib/notifications.server.ts Outdated Show resolved Hide resolved
test/unit/notifications.server.test.ts Outdated Show resolved Hide resolved
@SerpentBytes SerpentBytes marked this pull request as draft February 12, 2023 17:10
@SerpentBytes SerpentBytes force-pushed the issue-126 branch 2 times, most recently from 38d2e22 to 7481522 Compare February 12, 2023 23:11
@SerpentBytes SerpentBytes marked this pull request as ready for review February 12, 2023 23:15
app/lib/notifications.server.ts Outdated Show resolved Hide resolved
test/unit/notifications.server.test.ts Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
@shawnyu5
Copy link
Contributor

Please stop force pushing....

@Eakam1007
Copy link
Contributor

Please stop force pushing....

I think we don't need to squash and merge locally in general. It makes it hard to re-review changes.
When we merge PRs, we can make sure to Squash and Merge instead and keep the history on the PR

@shawnyu5
Copy link
Contributor

Exactly

test/unit/notifications.server.test.ts Outdated Show resolved Hide resolved
@Eakam1007 Eakam1007 self-requested a review February 15, 2023 19:01
Eakam1007
Eakam1007 previously approved these changes Feb 15, 2023
Copy link
Contributor

@Eakam1007 Eakam1007 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

humphd
humphd previously approved these changes Feb 15, 2023
test/unit/notifications.server.test.ts Show resolved Hide resolved
 * 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
test/unit/notifications.server.test.ts Outdated Show resolved Hide resolved
const text = 'Sample text';

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have one question. Is this response always same when provided email does not exist?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, even if the recipient is [email protected] the email would still get delivered using MailHog. We don't need real email addresses for testing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. I was thinking we might need a fail case testing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In production, we would expect it to fail. Here, we are just testing if are able to send/receive notifications.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uhhh there is a distinction between production testing and dev testing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. I was thinking we might need a fail case testing.

I could add that in a follow-up.

@SerpentBytes SerpentBytes merged commit dde1ede into DevelopingSpace:main Feb 15, 2023
@SerpentBytes SerpentBytes deleted the issue-126 branch February 15, 2023 20:15
Genne23v pushed a commit that referenced this pull request Feb 16, 2023
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category: back end Back end part of our web service category: notifications A service to notify users about their certificates/domains category: testing Unit tests, end to end tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement tests to send test notifications using MailHog
5 participants