dwyl / aws-sdk-mock

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

TypeError: Cannot read property 'setPromisesDependency' of undefined #110

Open nicekiwi opened 6 years ago

nicekiwi commented 6 years ago

If I run the following code with Jest, I get:

TypeError: Cannot read property 'setPromisesDependency' of undefined

      at node_modules/aws-sdk-mock/index.js:252:43
      at Object.<anonymous> (node_modules/aws-sdk-mock/index.js:262:3)
      at Object.<anonymous> (__tests__/main.js:4:11)
          at Generator.next (<anonymous>)
          at new Promise (<anonymous>)
          at Generator.next (<anonymous>)
          at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:188:7)

The test:

// __tests__/main.js
'use strict'

const sinon = require('sinon')
var AWS = require('aws-sdk-mock');
var AWS_SDK = require('aws-sdk')

AWS.setSDKInstance(AWS_SDK);

/* MOCKS */

let obj = { a: 1 }
let bucket = null, key = { path: null, data: null }

AWS.mock('S3', 'createBucket', (params, callback) => {
  bucket = params.Bucket
  callback()
})

AWS.mock('S3', 'getObject', (params, callback) => {
  if (params.Key !== key.path) callback({ errorCode: 'NoSuchKey' })
  else if (!params.Bucket) callback({ errorCode: 'NoSuchBucket' })
  else callback(null, { Body: key.data })
})

AWS.mock('S3', 'putObject', (params, callback) => {
  key.path = params.Key
  key.data = params.Body
  callback()
})
nicekiwi commented 6 years ago

Actually it seems calling

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

on it's own will return the same error.

rcwestlake commented 5 years ago

@nicekiwi how did you resolve this? Getting the same thing.

nicekiwi commented 5 years ago

Hmm, I don't think I ever got this working. Found it easier to write my own mocks. https://github.com/nicekiwi/lowdb-adapter-aws-s3/blob/master/__mocks__/aws-sdk.js

th3morg commented 5 years ago

I got this error due to the fact that jest had been used to mock aws-sdk in another test and it was never unmocked. That mock did not contain a config object, and therefore this exception resulted. My advice would be to ensure that aws-sdk is what you expect it to be in your test environment. Once I had ensured aws-sdk was the genuine SDK, aws-sdk-mock worked perfectly.

electblake commented 4 years ago

I got this error similar to @th3morg but I had a __mocks__/aws-sdk.js defined. once I deleted/renamed mock file, fixed.