TypeError: UpdateCommand is not a constructor - AWS SDK v3 for nodejs unit test with jest

79 views Asked by At

I keep getting "TypeError: UpdateCommand is not a constructor" when the unit test reaches the stage where it is calling the UpdateCommand from @aws-sdk/lib-dynamodb. I think it is the mock that has issue. Not really sure where to go from here

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
      })

    }
})

Tried clean npm install but no luck. Would really appreciate any inputs on this.

Thanks in advance!

1

There are 1 answers

2
Leeroy Hannigan On

I'm not familiar with your style of mocking but the parameters you pass to UpdateCommand are not correct, unlike PutCommand it does not take Item as parameter.

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