简体   繁体   中英

How to mock AWS response in callback function when using aws-mock

Context

Im using Typescript and aws-sdk-mock to mock the responses of various aws functions. After adding a couple new packages to my repo, a large portion of my tests are failing with

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'.

I understand the error, I understand that the below code should cause the error, since the expected return type of the data parameter in the callback function should be of type StartExecutionOutput , instead i am trying to call it with a string.

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

What i don't understand is why this is only a problem after I update my packages. The packages I've added (axios & a package owned and written by my company) don't touch AWS code or have anything to do with it.

I know I can solve the problem by changing all of my AWS Mocks to return the proper types, but I don't have access to and don't want to waste my time creating test objects of all the internal AWS types to return in these callbacks.

My Questions

Why is this only a problem after I've updated my packages?

Can i easily suppress this error everywhere so i can return whatever i want in these callbacks?

Thanks

Packages from package.json

I should note that the package json in develop (passing tests) and my branch (failing tests) are the exact same except for the addition of the company package and 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"
  }

I ran into this overload issue using the aws-sdk , v2. There is a thread about it on Github , which looks unresolved and closed unfortunately.

Guidance in their thread is to upgrade to v3 of the SDK.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM