javascript - S3Client 不是 Jest 中的构造函数
问题描述
我正在尝试使用 Jest 测试基本的 S3 命令,但使用模拟后出现错误。不知道我错在哪里。
我正在查看实现和单元测试部分,但不明白我为什么要这样做。
https://docs.aws.amazon.com/code-samples/latest/catalog/javascriptv3-tests-s3-s3.test.js.html
aws-s3.js
const { S3Client, HeadBucketCommand, CreateBucketCommand } = require("@aws-sdk/client-s3");
const { REGION } = require('../config');
// Create S3 service object
const s3 = new S3Client({ region: REGION });
const createS3Bucket = async ({ bucketName }) => {
const bucket = await isBucketExists({ bucketName });//some other function in the same file
if (!bucket) {
try {
const data = await s3.send(new CreateBucketCommand({ Bucket: bucketName }));
console.log(`Bucket ${bucketName} created successfully`);
return data;
} catch (err) {
console.log(`Bucket ${bucketName} couldn't create`);
return null;
}
}
return bucket;
};
s3.test.js
var faker = require('faker');
const mockCreateBucket = jest.fn();
jest.mock("@aws-sdk/client-s3", () => ({
S3: function S3() {
this.createBucket = mockCreateBucket;
},
}));
const { createS3Bucket } = require("../utils/aws-s3");
test("has to mock S3#createBucket", async (done) => {
const bucketName = faker.random.word();
await createS3Bucket(bucketName);
expect(mockCreateBucket).toHaveBeenCalledWith(bucketName);
done();
});
错误
TypeError: S3Client is not a constructor
3 |
4 | // Create S3 service object
> 5 | const s3 = new S3Client({ region: REGION });
| ^
6 |
7 | const isBucketExists = async ({ bucketName }) => {
8 | try {
at Object.<anonymous> (utils/aws-s3.js:5:12)
at Object.<anonymous> (__test__/s3.test.js:11:28)
谢谢
解决方案
推荐阅读
- algorithm - 以紧凑的方式对带有后缀或前缀的数字进行编码
- python - 如何找到两个不同比例和大小的图像之间的差异?
- batch-file - 通过批处理文件 (.bat) 编译 Inno Setup 项目
- javascript - Javascript提示保存到c#中的变量,单击相同的按钮
- php - 我的 wordpress 简码不起作用并输出“未知”
- sorting - Woocommerce / 按价格对相关产品进行排序
- python - 当我尝试浏览 .xslx 文件时,会打开一个新的 Excel 文档
- c# - 如何从一个txt文件制作一个水果数组,每个水果在C#中有四个属性
- c# - 如何将图像插入 SQL Server?
- php - 如何在 PhpStorm 包含路径中包含部分 PHAR