Skip to content
Advertisement

Jest skipping S3 Get Object call in function

Im testing a standard S3 Get Object Call, but when Jest tests my function, it skips the Get Object call and jumps to end of function, ending the execution and returning nothing. When I checked code coverage, all lines are covered expect the getObject call.

This is my s3 file being tested. I have checkpoints there to show that the s3.getObject call is being skipped by Jest.

JavaScript

This is my test file.

JavaScript

This test passes (even though the expect state is wrong. It should return ‘s3File.js’, not ‘blah’ but the test passes regardless). For the console logs, only checkpoints 1 and 3 are hit – the entire S3 call is ignored by the function. Is this an issue with how I mocked s3?

Advertisement

Answer

Since the s3.getObject() accepts an error-first callback, it’s NOT a JS promise, you should use .mockImplementation((params, callback) => callback(mockErr, mockData)) to mock the error-first callback and invoke it with mock data or error.

E.g.

s3.js:

JavaScript

s3.test.js:

JavaScript

Test result:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement