I have implemented Sendgird SDK to send emails and need to stub it in my tests. I am getting following, error
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
It seems this issue is not related to test execution time but some related to Promise.
This is the original class,
import sendGridMail from '@sendgrid/mail';
import Promise from 'bluebird';
export default class sendGridClient {
constructor() {
this.apiKey = 'Test key';
this.sendGrid = Promise.promisifyAll(sendGridMail);
this.sendGrid.setApiKey(this.apiKey);
}
async send(to, from, subject, description, body) {
const message = {
to,
from,
subject,
text: description,
html: body,
};
const response = await this.sendGrid.sendAsync(message);
return response;
}
}
Test code,
import { expect } from 'chai';
import Promise from 'bluebird';
import sinon from 'sinon';
import sendGridMail from '@sendgrid/mail';
import sendGridClient from './../../../src/lib/sendGrid/sendGridClient';
describe.only('sendGridClient', function () {
it('sends email successfully', async function () {
const sendGrid = Promise.promisifyAll(sendGridMail);
const setApiKey = sinon.stub(sendGrid, 'setApiKey');
const mandrillSend = sinon
.stub(sendGrid, 'send')
.returns(Promise.resolve({}));
const sendGridApi = new sendGridClient();
const actualResult = await sendGridApi.send(
'supprt@test.com',
'user@gmail.com',
'Subject',
'Email description',
'Email body',
);
expect(mandrillSend.callCount).to.equal(1);
setApiKey.restore();
mandrillSend.restore();
});
});