We are utilizing supertest with Typescript to conduct API testing. For certain endpoints such as user registration and password modification, an email address is required for confirmation (containing user confirm token or reset password token). To facilitate this process, we opted to use GuerillaMail, a simple disposable email client with an available API. After completing the necessary setup by configuring the email through their platform, the code snippet below effectively handles the task in various scenarios:
private async getEmailId(sid_token: string, emailType: EmailType): Promise<string> {
var mail;
var mailToken = this.getSidToken(sid_token);
//Continuously check inbox until the expected email arrives
// Jest framework timeout prevents infinite loop
while (!mail) {
const result = await request(this.guerillaMailApiUrl)
.get('')
.query({
f: 'check_email',
seq: 0,
sid_token: mailToken
});
if (result.body.list != undefined) {
mail = result.body.list.filter(m => m.mail_subject == emailType && m.mail_from == '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d2b7bfb3bbbe92b6bdbfb3bbbcfcb1bdbf">[email protected]</a>' && m.mail_read == 0)[0];
}
else {
mail = undefined;
}
}
return mail.mail_id;
}
Despite its functionality, the service imposes a restriction of 20 requests per minute, leading to test failures.
Is there a way to control the frequency of requests?
LATER EDIT: After introducing a delay method:
async delay(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}
and integrating it before exiting the while
loop:
await this.delay(5000);
Are there alternative methods to achieve this with improved efficiency and clarity?