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

AWS.mock only returns {} #143

Open RyanMitchellWilson opened 6 years ago

RyanMitchellWilson commented 6 years ago

I have tried so many different ways to get this to work and actually return what I put into the mock function but every time no matter what I set the value to I only get back {}. Here are the few different versions I have tried with no luck.

AWS.mock('S3', 'headObject', () => 'foo')
AWS.mock('S3', 'headObject', () => {foo: 'bar'})
AWS.mock('S3', 'headObject', () => null)
AWS.mock('S3', 'headObject', 'foo')
AWS.mock('S3', 'headObject', {foo: 'bar'})
AWS.mock('S3', 'headObject', null)

None of these do anything to the headObject request. I only get back an empty object. Is there something I am doing wrong or this a bug in the library?

Using: aws-sdk-mock: ^4.0.0 mocha: ^5.2.0

casey-largent commented 5 years ago

in the replace function passed in for the third argument, try passing in these parameters and calling it this way. The callback should return the mocked value you need in the second parameter for the callback. It's similar behavior to how writing Lambdas in Node works: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html

AWS.mock('S3', 'headObject', (params, callback) => {
    callback(null, {foo: 'bar'});
});
dylanwatsonsoftware commented 4 years ago

Did this work? I'm having the same issue but headObject seems not able to be mocked in this way...?

gskaplan commented 3 years ago

This does not work for me at all. I have tried all of permutations, using jest.fn() and sinon spy, but I cannot get it to return data back.

const testData = an array filled with a bunch of strings

AWS.mock("S3", "getObject", testdata)

when executing:

result = S3.getObject(s3Config).createReadStream()

the array of string is properly returned. But if I change it to:

  AWS.mock("S3", "getObject", (params, callback) => {
    callback(null, Buffer.from(testData));
  });

or {Body: Buffer.from(testData)} or even: callback(null, testData), I get an empty array back. I've even tried this with Sinon and it doesn't work. The only way it works is if I provide the testData in the first example. I need to be able to see how many times the function is called and I can't get it to work.

Thanks.