dwyl / aws-sdk-mock

:rainbow: AWSomocks for Javascript/Node.js aws-sdk tested, documented & maintained. Contributions welcome!
Apache License 2.0
1.11k stars 109 forks source link

trouble mocking hapi-mail #125

Open createthis opened 6 years ago

createthis commented 6 years ago

Hello,

I can't figure out what is going wrong here. I'm trying to mock SES sendMail, which is used inside the hapi-mail package here: https://github.com/paullang/hapi-mail/blob/master/lib/email-ses.js#L12

My test looks something like this:

import helper from '~/test_helpers'
import AWS from 'aws-sdk-mock'
let prs = the thing that sends the email // psudeo code paraphrasing
let server = helper.server;

    it('should send an email', async () => {
      AWS.mock('SES', 'sendEmail', function(params, callback) {
        console.log("here");
        callback(null, 'success');
      }); 
      let result = await prs.send_email(server); // this uses hapi-mail internally
      AWS.restore('SES', 'sendEmail');
    }).timeout(20000); // sending an email takes longer than most things

It seems like the mock just isn't happening. I never see "here" printed in the test output and this is the error I get:

{ CredentialsError: Missing credentials in config
    at ClientRequest.<anonymous> (/path/to/node_modules/aws-sdk/lib/http/node.js:83:34)
    at Object.onceWrapper (events.js:314:30)
    at emitNone (events.js:105:13)
    at ClientRequest.emit (events.js:207:7)
    at Socket.emitTimeout (_http_client.js:722:34)
    at Object.onceWrapper (events.js:314:30)
    at emitNone (events.js:105:13)
    at Socket.emit (events.js:207:7)
    at Socket._onTimeout (net.js:398:8)
    at ontimeout (timers.js:469:11)
    at tryOnTimeout (timers.js:304:5)
    at Timer.listOnTimeout (timers.js:264:5)

I'm kind of at a loss for how to debug this. Any ideas?

psaxton commented 5 years ago

It appears that your prs is being instantiated and initialized outside of your mocked scope. My understanding is that the mock with be registered before creating the object depending on the mock.

The code you've referenced is creating the AWS.SES in its constructor, not when .SendEmail() is called.