TypeError:UpdateCommand 不是构造函数 - AWS SDK v3 for Nodejs 单元测试与玩笑

问题描述 投票:0回答:1

当单元测试到达从 @aws-sdk/lib-dynamodb 调用 UpdateCommand 的阶段时,我不断收到“TypeError:UpdateCommand 不是构造函数”。我认为这是有问题的模拟。不太确定从这里去哪里

jest.mock('@aws-sdk/client-dynamodb', () => {
    return {
      DynamoDBClient: jest.fn().mockImplementation(() => {
        return {}
      }),
    };
  });
  jest.mock('@aws-sdk/lib-dynamodb', () => {
    return {
      DynamoDBDocumentClient: {
        from: jest.fn().mockImplementation(() => {
          return {
            send: jest.fn().mockImplementation((command) => {
              let res = ''
              if (command.name === 'GetCommand') {
                res = {
                  Item: {
                    data: 'data',
                  }
                }
              }else if (command.name === 'UpdateCommand' || command.name === 'PutCommand') {
                res = { TableName: 'tableName', Item: { PK: 'pk', SK: 'sk' } }
              }
              else if (command.name === 'QueryCommand') {
                res = {
                  Items:[
                  {
                      "data":"data"
                  }
                ]
              }
              }
              return Promise.resolve(res)
            })
          }
        })
      },
      GetCommand: jest.fn().mockImplementation((cmdPayload) => {
        if (cmdPayload.Key.PK === 'pk') {
          return { name: 'GetCommand' }
        }
        return undefined
      }),
      PutCommand: jest.fn().mockImplementation(() => {
        return { name: 'PutCommand' }
      }),
      UpdateCommand: jest.fn().mockImplementation((cmdPayload) => {
        return { name: 'UpdateCommand' }
      }),
      QueryCommand: jest.fn().mockImplementation((cmdPayload) => {
        // ensure valid api name in mock table
        if (cmdPayload.ExpressionAttributeValues[':ct'] === 'test') {
          return { name: 'QueryCommand' }
        }
        return undefined
      })

    }
})

尝试过干净的 npm install 但没有成功。非常感谢对此的任何意见。

提前致谢!

javascript node.js amazon-dynamodb aws-sdk aws-sdk-nodejs
1个回答
0
投票

我不熟悉你的模拟风格,但你传递给 UpdateCommand 的参数不正确,与 PutCommand 不同,它不接受

Item
作为参数。

https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-lib-dynamodb/Class/UpdateCommand/

© www.soinside.com 2019 - 2024. All rights reserved.