【问题标题】:How to mock AWS response in callback function when using aws-mock使用 aws-mock 时如何在回调函数中模拟 AWS 响应
【发布时间】:2022-07-26 11:56:51
【问题描述】:

上下文

我正在使用 Typescript 和 aws-sdk-mock 来模拟各种 aws 函数的响应。 在我的仓库中添加了几个新包后,我的大部分测试都失败了

error TS2769: No overload matches this call.
      Overload 1 of 2, '(err: undefined, data: StartExecutionOutput): void', gave the following error.
        Argument of type 'string' is not assignable to parameter of type 'StartExecutionOutput'.
      Overload 2 of 2, '(err: AWSError, data?: undefined): void', gave the following error.
        Argument of type '"invoked"' is not assignable to parameter of type 'undefined'.

我理解错误,我理解下面的代码应该会导致错误,因为回调函数中data 参数的预期返回类型应该是StartExecutionOutput 类型,而不是我试图用它来调用它一个字符串。

AWSMock.mock('StepFunctions', 'startExecution', (params, callback) => {
  fn();
  callback(null, 'invoked');
});

我不明白为什么这只是我更新软件包后的问题。我添加的包(axios 和我公司拥有和编写的包)不涉及 AWS 代码或与它有任何关系。

我知道我可以通过更改所有 AWS Mocks 以返回正确的类型来解决问题,但我无权访问并且不想浪费我的时间来创建所有内部 AWS 类型的测试对象以在这些回调中返回。

我的问题

为什么这只是我更新软件包后的问题?

我是否可以轻松地在任何地方抑制此错误,以便我可以在这些回调中返回我想要的任何内容?

谢谢

来自 package.json 的包

我应该注意,develop(通过测试)和我的分支(失败测试)中的包 json 是完全相同的,除了添加了公司包和 axios。

"dependencies": {
    "(company package obscured for privacy)": "^1.0.31",
    "@types/aws-lambda": "^8.10.59",
    "@types/jest": "^26.0.15",
    "@types/uuid": "^8.3.0",
    "aws-sdk": "^2.1046.0",
    "axios": "^0.27.2",
    "eslint-config-airbnb": "^18.2.0",
    "full-icu": "^1.3.1",
    "luxon": "^1.25.0",
    "module-alias": "^2.2.2",
    "ts-loader": "^8.0.6",
    "typescript": "^4.0.3",
    "uuid": "^8.3.1",
    "webpack": "^5.1.3",
    "webpack-cli": "^4.1.0",
    "webpack-node-externals": "^2.5.2"
  },
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^4.5.0",
    "@typescript-eslint/parser": "^4.5.0",
    "aws-sdk-mock": "^5.1.0",
    "elasticmq-npm": "^0.13.10",
    "eslint": "^7.11.0",
    "eslint-config-airbnb-typescript": "^12.0.0",
    "eslint-plugin-import": "^2.22.1",
    "husky": "^4.3.0",
    "jest": "^26.6.0",
    "jest-junit": "^12.0.0",
    "mocked-env": "^1.3.2",
    "nodemon": "^2.0.6",
    "pm2": "^5.1.0",
    "prettier": "^2.1.2",
    "serverless": "^2.46.0",
    "serverless-dynamodb-local": "^0.2.39",
    "serverless-offline": "^6.8.0",
    "serverless-offline-sqs": "^4.0.1",
    "ts-jest": "^26.4.1"
  }

【问题讨论】:

    标签: node.js typescript amazon-web-services jestjs ts-jest


    【解决方案1】:

    我在使用 aws-sdk, v2. 时遇到了这个过载问题。在Github 上有一个关于它的帖子,看起来没有解决,很遗憾已经关闭。

    他们的线程中的指导是升级到 SDK 的 v3。

    【讨论】:

      猜你喜欢
      • 2020-08-13
      • 1970-01-01
      • 1970-01-01
      • 2021-06-01
      • 1970-01-01
      • 2019-12-07
      • 1970-01-01
      • 1970-01-01
      • 2022-06-23
      相关资源
      最近更新 更多