【问题标题】:"docClient.scan(...).promise is not a function" error when mocking AWS promises模拟 AWS 承诺时出现“docClient.scan(...).promise 不是函数”错误
【发布时间】:2020-08-04 13:30:31
【问题描述】:

我正在尝试将一些代码从回调更改为承诺,将 .promise 添加到 aws 调用中。

const AWS = require("aws-sdk")

const docClient = new AWS.DynamoDB.DocumentClient({
  apiVersion: "2012-08-10",
  region: process.env.AWS_REGION
})

class Stuff {
   getRawItems() {
     let params = {
        TableName : "TableName"
     }

     return docClient.scan(params).promise()
       .then(function(data) {
          return data.Items
       })
       .catch(function(err) {
         console.warn("Error with Dynamo request", err)
         throw err
       })
   }
}

我认为这是正确的,但测试有问题。我得到了错误:

'TypeError: docClient.scan(...).promise 不是函数'

我认为这与模拟的编写方式有关:

const fakeDynamo = { scan: jest.fn() }

const realAWS = require("aws-sdk")
realAWS.DynamoDB.DocumentClient = jest.fn( () => fakeDynamo )
const Stuff = require("../src/stuff").Stuff

test("Test that the scan is performed and the data comes back", done => {
   fakeDynamo.scan.mockImplementation( () => Promise.resolve({Items:[1,2,3]}))
   const stuff = new Stuff()
   const defaultItems = 
   stuff.getRawItems(lat, lon)
   defaultItems.then ( (data) => {
      expect(fakeDynamo.scan).toHaveBeenCalledTimes(1)
      expect(data.length).toEqual(3)
      done()
 })
})

【问题讨论】:

    标签: amazon-web-services mocking jestjs amazon-dynamodb


    【解决方案1】:

    模拟是问题所在,这里是示例代码:

    const fakePromise = {
       promise : jest.fn()
    }
    const fakeDynamo = { scan: () => {
       return fakePromise
    }}
    
    const realAWS = require("aws-sdk")
    realAWS.DynamoDB.DocumentClient = jest.fn( () => fakeDynamo )
    const Stuff = require("../src/stuff").Stuff
    
    test("Test that the scan is performed and the data comes back", done => {
      fakePromise.promise.mockImplementation(() => Promise.resolve({Items:[1,2,3]}))
      const stuff = new Stuff()
      const defaultItems = stuff.getRawItems(lat, lon)
      defaultItems.then ( (data) => {
         expect(fakePromise.promise).toHaveBeenCalledTimes(1)
         expect(data.length).toEqual(3)
         done()
      })
    })
    

    【讨论】:

      【解决方案2】:

      我遇到了这个问题,并找到了最简单的解决方案,即让 mock 发送一个被视为承诺的回调。

      fakePromise.promise.mockImplementation((params, callback) => callback(null, {Items:[1,2,3]}))

      【讨论】:

        【解决方案3】:

        我想也许可以阅读 Promises

         return docClient.scan(params).promise()
           .then(function(data) {
              return data.Items
           })
           .catch(function(err) {
             console.warn("Error with Dynamo request", err)
             throw err
           })
        

        应该是:

         return docClient.scan(params)
           .then(function(data) {
              return data.Items
           })
           .catch(function(err) {
             console.warn("Error with Dynamo request", err)
             throw err
           })
        

        解析器告诉你:scan 函数的结果中没有 promise 函数。

        【讨论】:

        • aws sdk中有一个promise函数。扫描返回一个 Request 并且可以从请求中获得承诺。这是模拟的问题。模拟函数不返回承诺。我需要以某种方式模拟扫描和承诺功能
        猜你喜欢
        • 1970-01-01
        • 2018-07-17
        • 2022-01-10
        • 1970-01-01
        • 2017-12-09
        • 2016-01-22
        • 1970-01-01
        • 2021-05-02
        • 2018-11-05
        相关资源
        最近更新 更多