Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

s3 interface initialization inside or outside the function #3840

Closed
s1mrankaur opened this issue Jul 19, 2021 · 1 comment
Closed

s3 interface initialization inside or outside the function #3840

s1mrankaur opened this issue Jul 19, 2021 · 1 comment
Assignees
Labels
guidance Question that needs advice or information.

Comments

@s1mrankaur
Copy link

s1mrankaur commented Jul 19, 2021

Confirm by changing [ ] to [x] below:

We currently have

export function s3Call(action: string, params: any): Promise<any> {
const s3 = new AWS.S3();
return s3[action](params).promise();
}

I tried to change it to

    const s3 = new AWS.S3();
    export function s3Call(action: string, params: any): Promise<any> {
    return s3[action](params).promise();
    }

but on running the unit test, ran into the following error

> failed to get the current sub/segment from the context new s3
enter image description here

The unit test itself is pretty simple:


    describe("uploadImage tests", () => {
    it("should return correct image url", async () => { process.env.IMAGE_UPLOAD_BUCKET_NAME = 'TestBucket'; process.env.AWS_REGION = 'us-west-2';
    const putObject = jest.fn().mockImplementation(() => Promise.resolve({})); 
    AWSMock.mock('S3', 'putObject', putObject);
    const bucketUrl = https://${process.env.IMAGE_UPLOAD_BUCKET_NAME}.s3-${process.env.AWS_REGION}.amazonaws.com/; 
    const result = await uploadImage(imageData, bucketUrl); expect(result).toContain(process.env.IMAGE_UPLOAD_BUCKET_NAME); 
expect(result).toContain(process.env.AWS_REGION); 
AWSMock.restore('S3');   }); });

How can the s3 interface be initialized once? and why do we see this error once when s3 initialization is taken out of the function?

@s1mrankaur s1mrankaur added guidance Question that needs advice or information. needs-triage This issue or PR still needs to be triaged. labels Jul 19, 2021
@vudh1 vudh1 removed the needs-triage This issue or PR still needs to be triaged. label Nov 22, 2021
@vudh1
Copy link
Contributor

vudh1 commented Feb 21, 2022

Hi @s1mrankaur thanks for reaching out. S3 interface needs to be initialized inside the function in this case because the test function needs to use the instance created.

@vudh1 vudh1 closed this as completed Feb 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
guidance Question that needs advice or information.
Projects
None yet
Development

No branches or pull requests

2 participants