aws-sdk-mock icon indicating copy to clipboard operation
aws-sdk-mock copied to clipboard

AWS.mock only returns {}

Open RyanMitchellWilson opened this issue 6 years ago • 3 comments

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

RyanMitchellWilson avatar Jul 03 '18 15:07 RyanMitchellWilson

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'});
});

casey-largent avatar Sep 26 '19 18:09 casey-largent

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

dylanwatsonsoftware avatar Sep 08 '20 13:09 dylanwatsonsoftware

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.

gskaplan avatar Nov 04 '20 02:11 gskaplan