In my test scenarios, I've created a mock version of the aws-sdk
, which is functioning perfectly:
jest.mock("aws-sdk", () => {
return {
Credentials: jest.fn().mockImplementation(() => ({})),
Config: jest.fn().mockImplementation(() => ({})),
config: {
update: jest.fn(),
},
S3: jest.fn().mockImplementation(() => ({
getSignedUrlPromise: awsGetSignedUrlMock,
})),
SQS: jest.fn().mockImplementation(() => ({})),
};
});
However, my various tests rely on this mocked AWS SDK, and I want to avoid duplicating the code unnecessarily. When attempting to move this function into a separate file for import, I encounter an issue:
Within my testSupport.ts
file:
export const mockAwsSdk = () => {
return {
Credentials: jest.fn().mockImplementation(() => ({})),
Config: jest.fn().mockImplementation(() => ({})),
config: {
update: jest.fn(),
},
S3: jest.fn().mockImplementation(() => ({
getSignedUrlPromise: awsGetSignedUrlMock,
})),
SQS: jest.fn().mockImplementation(() => ({})),
};
};
When trying to use this in the test file:
jest.mock("aws-sdk", mockAwsSdk);
This results in the following error:
ReferenceError: Cannot access 'testSupport_1' before initialization
I have experimented with different approaches, including creating a parent function that accepts the jest
instance, but I have not yet found a solution.
Is there a way to efficiently share jest mocks across multiple tests?