dwyl / aws-sdk-mock

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

Mocking AWS in middleware from test file #104

Open robinsonlam opened 7 years ago

robinsonlam commented 7 years ago

Hey everyone,

I'm currently attempting to test one my middleware which uses AWS, is there a way to do this by stubbing the AWS module in the middleware file with the mock-aws version?

const mockHttp = require('node-mocks-http');
const proxyquire = require('proxyquire');
const AWS = require('aws-sdk-mock');

AWS.mock('SNS', 'listTopics', () => [{ TopicArn: 'foobar' }]);

const snsTopics = proxyquire('../../src/middleware/sns_topics', {
  'aws-sdk': AWS,
});

describe.only('middleware > snsTopics', () => {
  it('should create an topic in the DB if it doesnt exist and return a topicArn', async () => {
    const req = mockHttp.createRequest({ registration: '' });

    await snsTopics(req, {}, () => {});

    expect(req.topicArn).to.equal('foobar');

    AWS.restore('SNS', 'listTopics');
  });
});

middleware is using promise():

...
      const awsTopicsData = await sns.listTopics({}).promise();
...

the error I'm receiving is TypeError: sns.listTopics(...).promise is not a function

sns.listTopics() returns:

{ promise: undefined,
  createReadStream: [Function: createReadStream] }

It seems the function is not being mocked? I've tried to change the mock to be just a string but receive the same output.

jcollum commented 6 years ago

I think I'm having the same issue. I suspect node-mocks-http is the problem. Some interaction between AWS requests and node-mocks-http is breaking things.