I need to test a file in charge of retrieving data from S3 through the 'aws-sdk' (nodeJs + Jest). The file is:
const AWS = require('aws-sdk');
let S3 = null;
const getS3 = async () => {
if (S3 === null) {
const config = {
endpoint: new AWS.Endpoint(process.env.S3_URL),
s3ForcePathStyle: true,
};
S3 = new AWS.S3(config);
}
return S3;
};
module.exports.getObjectList = async (prefix) => {
const params = {
Bucket: process.env.S3_BUCKET,
Delimiter: '/',
Prefix: prefix,
};
const s3 = await getS3();
const objectList = s3
.listObjects(params)
.promise()
.then((data) => {
const keys = data.Contents.map((c) => c.Key);
return keys;
})
.catch((err) => {
console.error(err);
return null;
});
return objectList;
};
and the test file is as below :
const s3Client = require('./s3Client');
const mockS3Instance = {
listObjects: jest.fn().mockReturnThis(),
promise: jest.fn().mockReturnThis(),
catch: jest.fn(),
};
jest.mock('aws-sdk', () => {
return {
S3: jest.fn(() => mockS3Instance),
Endpoint: jest.fn(() => {
'blabla';
}),
};
});
describe('s3Client tests', () => {
it('basic test', async () => {
const getObjectListResult = await s3Client.getObjectList('test');
expect(1).toBe(1);
});
});
But a error message is returned :
ypeError: s3.listObjects(...).promise(...).then is not a function