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.
const AWS = require('aws-sdk');
const s3 = new AWS.S3({ apiVersion: '2012-11-05 ' })
function getS3Object(bucket, file) {
return new Promise((resolve, reject) => {
console.log('Checkpoint 1')
const parameters = {
Bucket: bucket,
Key: file
}
s3.getObject(parameters, (error, data) => {
console.log('checkpoint 2')
if (error) {
console.log('error')
reject(error)
} else {
console.log('success')
resolve(data)
}
})
console.log('Checkpoint 3')
})
}
module.exports = {
getS3Object
}
This is my test file.
const s3Handler = require('../s3')
const AWS = require('aws-sdk')
jest.mock('aws-sdk', () => {
const mockS3 = {
getObject: jest.fn().mockReturnThis(),
promise: jest.fn()
}
return {
config: {
update() {
return {};
}
},
S3: jest.fn(() => mockS3)
}
})
describe('S3 get object test', () => {
test('test', async () => {
const mockS3 = new AWS.S3();
mockS3.getObject('test-bucket', 'test-key').promise.mockResolvedValueOnce('s3file.js');
const output = s3Handler.getS3Object('test-bucket', 'test-key');
expect(output).resolves.toEqual('blah')
})
})
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?