I am trying to create a custom reporter which should send emails only when tests fail. As an Emailer I am using a nodemailer. As a reporter I am using a custom reporter - code is bellow.
So this is my solution as of now:
import { FullResult, Reporter } from '@playwright/test/reporter';
import { Emailer } from './Emailer';
class MyReporter implements Reporter {
emailer: Emailer;
testSuiteStatus;
passStatus;
onEnd(result: FullResult) {
this.testSuiteStatus = result.status.toString();
if (result.status.toString() !== 'passed') {
this.passStatus = true;
}
return this.passStatus && this.testSuiteStatus;
}
onExit() {
this.emailer = new Emailer();
if (this.passStatus === true) {
return this.emailer.sendEmail(
`Test run status: ${this.testSuiteStatus} for ${process.env.NODE_ENV}.`
);
}
}
}
export default MyReporter;
For some reason the if block does not work and when tests fail no email is sent. When the if block is removed, emails are sent. Could anyone give me an advice on what I am doing wrong here? Thanks!