我正在尝试编写上传测试代码。但我并没有低估如何正确使用 jest.mock('aws-sdk')
export class S3Service {
private readonly s3: S3;
private readonly bucket: string;
constructor(private readonly configService: ConfigService) {
this.s3 = new S3({
accessKeyId: this.configService.get(''),
secretAccessKey: this.configService.get(''),
region: this.configService.get(''),
});
this.bucket = this.configService.get('');
}
async upload(name: string, contentType: string, buffer: Buffer): Promise<string> {
const upload = await this.s3.upload({params...}).promise();
return upload;
}
}
原文由 luickx 发布,翻译遵循 CC BY-SA 4.0 许可协议
这是单元测试解决方案:
s3Service.ts
:s3Service.test.ts
:100% 覆盖率的单元测试结果: