I've ran into a strange issue when attempting to auto mock the AWS S3 client. I'm trying to write test cases to check if the S3 Class is instantiated with the correct parameters.
To write my test cases I'm auto mocking the S3 client which works for testing the upload function in S3. The problem I'm having is with the deleteObject function, every time I run my test around this function, Jest complains TypeError: s3Bucket.deleteObject is not a function
It seems like Jest isn't properly auto mocking the deleteObject function.
Wondering if anyone has any ideas here? I've tried various approaches and cannot figure out how to get around this...
Thank you!
Source file
import S3 from "aws-sdk/clients/s3";
function uploadToS3(bucketParams, cb) {
const s3Bucket = new S3(bucketParams);
s3Bucket.upload({ Key: "test", Body: "test" }, cb);
}
function deleteFromS3(bucketParams, cb) {
const s3Bucket = new S3(bucketParams);
s3Bucket.deleteObject({ Key: "test", Body: "test" }, cb);
}
module.exports = { uploadToS3, deleteFromS3 };
Test file:
jest.mock("aws-sdk/clients/s3");
import S3 from "aws-sdk/clients/s3";
const targetModule = require("source code");
// this passes as expected
it("should set s3 bucket parameters on upload", async () => {
const bucketParams = { ... some params };
targetModule.uploadToS3(bucketParams, (err: any) => {
expect(S3).toBeCalledTimes(1);
expect(S3).toBeCalledWith(bucketParams);
expect(S3.prototype.upload).toBeCalledTimes(1);
});
});
// this fails - TypeError: s3Bucket.deleteObject is not a function
it("should set s3 bucket parameters on delete", () => {
const bucketParams = { ...some params };
targetModule.deleteFromS3(bucketParams, (err: any) => {
expect(S3).toBeCalledTimes(1);
expect(S3).toBeCalledWith(bucketParams);
expect(S3.prototype.deleteObject).toBeCalledTimes(1);
});
});