This is a helper for CodeceptJS to check for emails sent to MailCatcher during a test.
- Download
mailcatcher.jsto your project that has CodeceptJS and make sure to addgotas npm dependency (or replace the usages ofgotin this helper by your preferred http library). - Make sure the application you're testing is sending the emails to a mailcatcher instance.
In your codecept.config.js add something like this to your helpers configuration:
"helpers" : {
"mailcatcher": {
"require": "./helpers/mailcatcher.js",
"host": process.env.MAIL_HOST || "http://localhost:1080"
}
} In a test, you should be now able to use:
I.amOnPage('/signup');
I.waitForText('Sign up');
I.fillField('email', '[email protected]');
I.click('Sign up');
I.waitForText('Check your mailbox');
const email = await I.receivedAnEmail('[email protected]', 'Continue your sign up.');
// do something with the emailOur application generates a unique signup confirmation link, I also made a helper-method (as page-object) to open a link from an email:
openLinkFromEmail(emailBody, linkBase) {
const regex = new RegExp(`https?:\/\/(.*)${linkBase}(.*)`, 'm');
const result = regex.exec(emailBody);
if (result === null) {
throw new Error(`cannot find link ${linkBase}`);
}
this.amOnPage(`${linkBase}${result[2]}`);
}which can then be used in the test as such:
const email = await I.receivedAnEmail('[email protected]', 'Continue your sign up.');
I.openLinkFromEmail(email.body, '/signup/');
I.waitForText('Create your account');