Search code examples
node.jsaws-sdkaws-sdk-mock

Mocking AWS with dwyl/aws-sdk-mock not working


First of all, I'm not using lambda, so I believe this thread isn't the same question.

I'm getting this error in the before hook:

TypeError: Attempted to wrap undefined property S3 as function
      at checkWrappedMethod (node_modules/aws-sdk-mock/node_modules/sinon/lib/sinon/util/core.js:78:29)
      at Object.wrapMethod (node_modules/aws-sdk-mock/node_modules/sinon/lib/sinon/util/core.js:121:21)
      at Object.stub (node_modules/aws-sdk-mock/node_modules/sinon/lib/sinon/stub.js:67:26)
      at mockService (node_modules/aws-sdk-mock/index.js:67:27)
      at Object.AWS.mock (node_modules/aws-sdk-mock/index.js:43:5)
      at Context.<anonymous> (myModule.spec.js:14:9)

I've never used this module before neither do I have much experience with sinon.js.

Here is my module:

var AWS = require('aws-sdk')
const https = require('https')

module.exports = function () {
    var s3 = new AWS.S3({
      httpOptions: {
        agent: new https.Agent()
      },
      signatureVersion: 'v4'
    })
    return s3.getObject({Bucket: 'somebucket', Key: 'somekey'}).promise()
}

Here is my test script:

const chai = require('chai')
const chaiAsPromised = require('chai-as-promised')

const AWS = require('aws-sdk-mock')
const myFunc = require('./myModule')

chai.use(chaiAsPromised)
const expect = chai.expect
chai.should()

describe('Get MMS API Key', function () {

  before(function () {
    AWS.mock('S3', 'getObject', function (params, callback) {
      callback(null, 'dummy-data')
    })
  })

  it('Should get dummy data', function () {
    myFunc().should.eventually.equal('dummy-data')
  })

  after(function () {
    AWS.restore('S3', 'getObject')
  })
})

Thoughts?


Solution

  • I've the same problem. Please, check under node-modules -> aws-sdk version. Mine was 2.6.2 and it does not worked. Temporary fix is to use aws-sdk 2.5.5 version. Probably soon they will fix for all versions as it is stated in aws-sdk-mock module. aws-sdk-mock should work with all aws-sdk versions greater that 2.3.0.