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

Simple test fails "AWS.SNS is not a function" #59

Open harrisonhjones opened 7 years ago

harrisonhjones commented 7 years ago

Hi Gang,

I'm sure I'm missing something super obvious but I can't seem to get this module to work. I've distilled my test to the following example:

Step to reproduce

  1. Install both the aws-sdk and aws-sdk-mock using npm.

  2. Create a file called test.js and insert the following code into it.

    var AWS = require('aws-sdk-mock');
    AWS.mock('SNS', 'publish', 'test-message');
    console.log(AWS); 
    
    var sns = AWS.SNS();
    sns.publish({}, function(err, data) {
      if (err) console.log(err, err.stack);
      console.log(data);
    });
  3. Run the command node test.js.

Expected Response

A console dump of the entire AWS sdk and then the message 'test-message'.

Actual Response

An error (see below).

var sns = AWS.SNS();
              ^

TypeError: AWS.SNS is not a function

Note

console.log(AWS) returns:

{ setSDK: [Function],
  mock: [Function],
  restore: [Function],
  Promise: [Function: Promise] }

What's going on? How do I get this example working?

jruts commented 7 years ago

Hi @harrisonhjones .

Could you init SNS before applying the mock to it and see if that works for you?

var AWS = require('aws-sdk-mock');
var sns = AWS.SNS();

AWS.mock('SNS', 'publish', 'test-message');
console.log(AWS); 

sns.publish({}, function(err, data) {
  if (err) console.log(err, err.stack);
  console.log(data);
});
vdeltoral commented 7 years ago

Is there a resolution to this? I'm having the exact same issue. I even tried it with @jruts code and it's giving me the exact same error message. I've installed aws-sdk and aws-sdk-mock via npm.

var sns = AWS.SNS();
              ^

TypeError: AWS.SNS is not a function
antstanley commented 6 years ago

Not too sure if anyone found the fix, but to get it working you need to instantiate a new instance (aka use 'new')

so use

var sns = new AWS.SNS instead of var sns = AWS.SNS

Blackbaud-ChristiSchneider commented 6 years ago

I was running into this problem as well. The problem is that you're using the AWS mock to try to make your SNS object, when you should be using the AWS sdk that is mocked. I think this can be clarified by changing the README examples to use a different variable name for the mock.

myCode.js

var AWS = require('aws-sdk');
exports.handler = (event, context, callback) => {
  var sns = new AWS.SNS();
  // do stuff with sns
  callback(args, result);
};

myCode.spec.js

var awsMock = require('aws-sdk-mock');
awsMock.mock('SNS');
awsMock.restore();

describe('myCode', function() {
  it('does the thing', function(done) {
    var myCode = require('./myCode.js');
    myCode.handler(e, context, function() {
      done();
    });
  });
});
jlarroque commented 3 years ago

I have the same problem, but in my case I want to mock the SNS.publish(...).promise(). Any ideas?