dwyl / aws-sdk-mock

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

uncessfull mocking - missing credentials #42

Open tomdavidson opened 8 years ago

tomdavidson commented 8 years ago

Trying out basic use of tape and aws-sdk-mock but am not able to mock aws. I am initialling the AWS service in the function:

...
 const AWS = require("aws-sdk");
 AWS.config.region = "us-west-2";
...
 exports.SNSPublish = (message, subject, topic) => {
     console.log("sns_pub");
     let sns = new AWS.SNS();

     let publishParams = {
         TopicArn: topic,
         Subject: subject,
         Message: JSON.stringify(message, null, 2)
     };

     sns.publish(publishParams, function(err, data) {
         if (err) {
             console.log(err.stack);
             return;
         }
     });
 };

my attempt to mock sns publish:

...
const AWS = require('aws-sdk-mock');
const targetServerjs = require("../server.js");
...
function lambdaSNSPublishTest() {
    AWS.mock('SNS', 'publish', 'test-message');
    var testTarget = targetServerjs;
    var testEvent = Object.assign(mockEvent);
    var testSNS = testEvent.Records[0].Sns;
    var testSpec = Object.assign(testEvent);

    test("SNSPublish() test", t => {
        var output = testTarget.SNSPublish(testSNS.Message, testSNS.Subject, testSNS.TopicARN);
        t.equals(output, testSpec);
        t.end();
    });

    AWS.restore();
}

which fails over creds:

  TimeoutError: Missing credentials in config

Seems straightforward, but I am at a loss.

jcollum-cambia commented 7 years ago

Did you solve this? Did the dev ever respond?

hoegertn commented 7 years ago

I think the problem is, that you restore the mock before the test function is called, as this is done asynchronously.

Does your test framework support before and after methods? That would be the place to mock and restore.

jcollum-cambia commented 7 years ago

Solved it. I was using an SQS Consumer lib and in that lib there was a call to Request.send. Solution:

const requestSend = sinon.stub();
AWSMock.mock("Request", "send", requestSend);