Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trouble mocking hapi-mail #125

Open
createthis opened this issue Apr 27, 2018 · 1 comment
Open

trouble mocking hapi-mail #125

createthis opened this issue Apr 27, 2018 · 1 comment

Comments

@createthis
Copy link

createthis commented Apr 27, 2018

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
Copy link

psaxton commented Jan 20, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants