I am trying to mock the deleteObject
and upload
AWS SDK functions being used in my backend routes:
const params: AWS.S3.DeleteObjectRequest = {
Bucket: bucketName,
Key: fileName,
};
// Delete the object from the bucket
await s3.deleteObject(params).promise();
const params: AWS.S3.PutObjectRequest = {
Bucket: bucketName,
Key: filename,
Body: file.data,
};
// Upload file to S3
const data = await s3.upload(params).promise();
Currently I have tried mocking them like this in my Jest files:
// ...
jest.mock("aws-sdk");
// ...
const s3 = new AWS.S3() as jest.Mocked<S3>;
s3.upload.mockResolvedValue({});
But get an error:
'Argument of type '{}' is not assignable to parameter of type 'never'
I have also tried:
// ...
// Mock the S3 class and its upload method
const mockUpload = jest.fn();
jest.mock('aws-sdk', () => {
return {
S3: jest.fn(() => ({
upload: mockUpload,
})),
};
});
// ...
mockUpload.mockReturnValue({
promise: jest.fn().mockResolvedValue({
Location: "https://example-bucket.s3.amazonaws.com/filename.jpg",
}),
});
But this gives a similar error:
Argument of type '{ Location: string; }' is not assignable to parameter of type 'never'
I have tried something similar with the getSignedUrl
function and that works alright with no type errors:
const signedUrl = "https://signed-url.com/somefile.txt";
// Mock the signed URL generation
jest
.spyOn(AWS.S3.prototype, "getSignedUrlPromise")
.mockResolvedValue(signedUrl);
2