Node Jest S3上传模拟

时间:2019-09-30 11:15:31

标签: node.js typescript amazon-s3

我正在尝试在单元测试用例中模拟S3上传功能。我正在使用笑话和AWS-SDK-MOCK。

以下是我尝试过的代码:

#functionality to mock

const bucketUpload = async (
      bucketName: string,
      key: string,
      body: any
) => {
  const s3 = new S3();
  const params: S3.PutObjectRequest = {
    Body: body,
    Bucket: bucketName,
    Key: key
  };
  return await s3
  .upload(params)
  .promise()
  .then(result => result)
  .catch(err => err);
};

#Code used to mock it 

import * as AWS from "aws-sdk";
import * as AWSMock from "aws-sdk-mock";

jest.mock("aws-sdk");

describe("when using the create function", () => {
    const mockResponse = jest.fn((params: AWS.S3.PutObjectRequest, callback:any) =>
    callback(null, {Location:"PublicLink"})
    );
    it("calls the put function", async () => {
       AWSMock.setSDKInstance(AWS);
       AWSMock.mock('S3', 'upload', mockResponse);
       ---- further test case ----
       expect(Mock).toHaveBeenCalledTimes(1);
 });
});

Error: Cannot read property 'upload' of undefined

这是我在执行工作时遇到的错误。当我尝试调试问题时,我发现它每次都在模拟将数据上传到存储桶的功能

0 个答案:

没有答案