简体   繁体   中英

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?

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.

I too faced the same issue but was trying to mock DynamoDB instead of S3. Had logged an issue for the same. The bug is now fixed with latest version of aws-sdk (v2.6.4). Would be great if you give it shot pointing to latest version 2.6.4.

Thanks, Zaid

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM