0

I would like to use nodemailer in a few tests to be able to pragmatically test the contents of an email that is sent. So far, its working great but there is one issue I haven't been able to solve.

I am using the createTestAccount from the Javascript nodemailer, and notice that it often returns me the same test email (I think it uses the same email for a period of time?)

How can I use this to always generate different emails?

I have a condition where I my code fails if the email isn't unique across tests.


As an example, a flow I am testing is:

  1. [behind the scenes a user is created in the database]
  2. User goes to sign in page
  3. User enters email and is asked to enter one time password
  4. User signs into email and gets code
  5. User enters code into website and is successfully signed in

The email with the one time password is sent via an external service so I can't mock it.

For each of my tests there are different configurations so I need a unique user I create in the background for each test. My tests also change the information about the user (in the database). I am also running these tests in parallel so using the same user will result in tests being influenced by other tests.

Charklewis
  • 4,427
  • 4
  • 31
  • 69
  • `pragmatically test the contents of an email that is sent` what part do you need to test? Why do you need to deliver it to a mail server you could just write it to a file? – t.niese May 17 '22 at 08:36
  • `I have a condition where I my code fails if the email isn't unique across tests.` why do you need multiple emails instead of fixing that problem in your test? – t.niese May 17 '22 at 08:37
  • Ok so you use nodemail only to create the ethereal account and not to send the emails? And you use ethereal only to receive emails instead of using it as a none delivering smtp server? – t.niese May 17 '22 at 08:56
  • Yes that’s correct :-) – Charklewis May 17 '22 at 09:01
  • Ok, now it makes more sense. I only know what ethereal does but never used it, so I can't help there. But why don't you use your own project domain for that with a catch-all address on a subdomain like `unit-test.your-project.tld` or a gmail (or any other service supporting the `+`) address utilizing the `+` separator (`accountname+whateveryouwant@gmail.com`)? – t.niese May 17 '22 at 09:13

1 Answers1

1

After looking the source code of nodemailer I found there is a cache you can disabled by ETHEREAL_CACHE=false. This generates new emails every time.

Charklewis
  • 4,427
  • 4
  • 31
  • 69