1

I'm trying to unit test some code that involves a call to AWS's SES service.

Here's the code in question:

const AWS = require('aws-sdk');
const send = function(options) {
  const SES = new AWS.SES();
  return new Promise((resolve, reject) => {
    // clipped for brevity
    SES.sendEmail(sesOpts, (error, data) => {
      return error ? reject(error) : resolve(data);
    });
  });
}
module.exports = send;

And here's a test, run with Mocha:

let SESMock = function() {};
SESMock.prototype.sendEmail = sinon.stub();

const emailHelper = proxyquire('../../src/helpers/email', {
  'aws-sdk': {
    SES: SESMock,
  },
});

it('should call sendEmail', (done) => {
  const opts = {}; // imagine this has the mail sending options
  emailHelper.send(opts).then(() => {
        assert(SESMock.prototype.sendEmail.called);
        done();
    })
    .catch((error) => {
        console.error(error);
        done();
    });;
});

Somehow, despite done() being included in both the then() and catch() callbacks, the test times out every time. I very much expect this is something to do with my setup of the stub, I think I've turned myself around so many times with this that I've lost my bearings altogether.

Can anyone push me in the correct direction to solve this?

Thanks

user1381745
  • 3,850
  • 2
  • 21
  • 35

1 Answers1

2

Yup, it was something daft. The sendEmail stub needs to yield:

SESMock.prototype.sendEmail = sinon.stub().yields();
user1381745
  • 3,850
  • 2
  • 21
  • 35