【发布时间】: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