简体   繁体   English

aws-sdk-js ReferenceError:您正在尝试在 Jest 环境被拆除后“导入”文件

[英]aws-sdk-js ReferenceError: You are trying to `import` a file after the Jest environment has been torn down

I need to test whether a pre-signed url to bucket Amazon S3 was returned.我需要测试是否返回了存储在 Amazon S3 中的预签名 url。

The Jest show the message below when run yarn test : Jest 在运行yarn test时显示以下消息:

ReferenceError: You are trying to `import` a file after the Jest environment has been torn down.

      at Object.userAgent (node_modules/aws-sdk/lib/util.js:34:43)
      at HttpRequest.setUserAgent (node_modules/aws-sdk/lib/http.js:111:78)
      at new HttpRequest (node_modules/aws-sdk/lib/http.js:104:10)
      at MetadataService.request (node_modules/aws-sdk/lib/metadata_service.js:93:23)
      at MetadataService.fetchMetadataToken (node_modules/aws-sdk/lib/metadata_service.js:116:10)

That's the test:那就是测试:

index.spec.js索引.spec.js

const AWS = require('aws-sdk');

const bucketName = 'bucket';
const key = 'image.png';

describe('Test', () => {
  it('should be able return pre signed url', () => {
    const s3SigV4Client = new AWS.S3({
      signatureVersion: 'v4',
    });
    
    const s3PreSignedUrl = s3SigV4Client.getSignedUrl('getObject', {
      Bucket: bucketName,
      Key: key,
      Expires: 60,
    });

    expect(s3PreSignedUrl);
  });
});

package.json package.json

{
  "name": "test_aws_sdk",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "test": "jest"
  },
  "dependencies": {
    "aws-sdk": "^2.807.0"
  },
  "devDependencies": {
    "jest": "^26.6.3"
  }
}

Node.js and Yarn version installed: Node.js 和安装的 Yarn 版本:

$ node -v
v10.19.0
$ yarn -v
1.22.4

There's something I did wrong.我做错了什么。

Please can you help me?你能帮帮我吗?

After the imports, add jest.useFakeTimers() in your test file:导入后,在您的测试文件中添加jest.useFakeTimers()

Example:例子:

const AWS = require('aws-sdk');

jest.useFakeTimers()

const bucketName = 'bucket';
const key = 'image.png';

describe('Test', () => {
  it('should be able return pre signed url', async () => {
    const s3SigV4Client = new AWS.S3({
      signatureVersion: 'v4',
    });
    
    const s3PreSignedUrl = s3SigV4Client.getSignedUrl('getObject', {
      Bucket: bucketName,
      Key: key,
      Expires: 60,
    });

    expect(s3PreSignedUrl);
  });
});

Try mocking instance:尝试 mocking 实例:

  jest.mock('dynamoose', () => ({
    aws: new class {
      sdk = { config: { update: jest.fn((arg) => arg) } };
    },
    Schema: class { },
    Condition: class { },
    transaction: new class { },
    model: jest.fn(),
    logger: new class { },
    UNDEFINED: new class { },
    THIS: new class { },
    NULL: new class { },
  }));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ReferenceError:您正在尝试在 Jest 环境被拆除后“导入”一个文件:在 loadCjsDefault - ReferenceError: You are trying to `import` a file after the Jest environment has been torn down: at loadCjsDefault ReferenceError:你正试图在 Jest 环境被拆除后“导入”一个文件 - ReferenceError: You are trying to `import` a file after the Jest environment has been torn down 由 AWS SDK 引起的环境破坏后尝试导入的 Jest - Jest trying to import after environment torn down, caused by AWS SDK 在开玩笑中获取“您正在尝试在 Jest 环境被拆除后导入文件”和“instanceof 的右侧不可调用” - Getting "You are trying to import a file after the Jest environment has been torn down" and "right-hand side of instanceof is not callable" in jest 在 Jest 环境被拆除后导入文件 - Import a file after the Jest environment has been torn down Jest 测试通过但是.. 有控制台消息您正在尝试在 Jest 环境被拆除后访问它的属性或方法 - Jest test passes but .. has console message You are trying to access a property or method of the Jest environment after it has been torn down 如何将 aws-sdk-js 捆绑到无服务器框架优化包中? - How do you bundle aws-sdk-js into a Serverless Framework optimized package? 使用angular + aws-sdk-js +预签名网址将文件上传到S3 - Uploading a file to S3 with angular + aws-sdk-js + pre-signed url 将aws-sdk-js与CognitoSync服务一起使用时出现InvalidSignatureException - InvalidSignatureException while using aws-sdk-js with CognitoSync service 如何使用 aws-sdk-js 列出日期范围内的对象? - How to list objects in a date range with aws-sdk-js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM