Skip to content

How to generate unique random emails with nodemailer

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.



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.