Closed garysassano closed 9 months ago
Hi! This came up while I was looking to mock this same operation in my own project.
if (error instanceof NotFound) {
console.log("NotFound@v3");
}
I'm able to test this reliably with something like this:
const s3Client = mockClient(S3Client);
// Inside of an individual test case
s3Client.on(HeadObjectCommand).rejects(new NotFound({ message: 'sorry', $metadata: {} }));
const res = await exists(...inputs);
expect(res).toEqual(false)
My libraries are at these versions:
Maybe see if upgrading the mock library to v3 fixes it for you?
@stern-shawn The bug is present also in aws-sdk-client-mock@3.0.0
Like @stern-shawn wrote, if you provide a custom error object to the rejects()
method, you will get it thrown back, so this works without an issue:
import {HeadObjectCommand, ListObjectsV2Command, NotFound, S3Client} from "@aws-sdk/client-s3";
import {mockClient} from "aws-sdk-client-mock";
const s3 = new S3Client({});
const s3Mock = mockClient(S3Client);
it('rejects with custom error', async () => {
s3Mock.on(HeadObjectCommand).rejects(new NotFound({message: 'sorry', $metadata: {}}));
const headObjectCommand = new HeadObjectCommand({Bucket: 'aa', Key: 'bb'});
await expect(s3.send(headObjectCommand)).rejects.toThrow(NotFound);
});
If you provide just the message like s3Mock.on(HeadObjectCommand).rejects('my message')
, you will get a standard Error
object back with the message included.
Please provide a reproduction example if something does not work for you.
Checklist
Bug description
While error classes function as expected for most commands, they seem to behave differently for the
HeadObjectCommand
.For example, I have this method:
During my tests using the method above, the only console output I received was
NotFound@v2
, suggesting that there might be an issue.Reproduction
Environment