Home > Software design >  How to generate unique random emails with nodemailer
How to generate unique random emails with nodemailer

Time:05-18

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.

CodePudding user response:

Sujin's anwser is acceptable but there are small change to get only contain number, that's not allow for email. Easy way to fix is pick an random charactor and add it to email.

CodePudding user response:

use this code!

var chars = 'abcdefghijklmnopqrstuvwxyz1234567890';

var string = '';

for(var ii=0; ii<15; ii  ) {
    string  = chars[Math.floor(Math.random()*chars.length)];
}

alert(string   '@gmail.com');
  • Related