I`ve beed following the aws-sdk-mock documentation to mock a DynamoDB service so I can test my functions, which works fine like this:
const AWSMock = require("aws-sdk-mock");
const AWS = require("aws-sdk");
const mockedData = require("./mockData");
export async function getDynamoItem() {
AWSMock.setSDKInstance(AWS);
AWSMock.mock("DynamoDB.DocumentClient", "get", (params, callback) => {
console.log("DynamoDB.DocumentClient", "get", "mock called");
callback(null, mockedData);
});
const input = { TableName: "", Key: {} };
const client = new AWS.DynamoDB.DocumentClient({
apiVersion: "2012-08-10",
});
const response = await client.get(input).promise();
AWSMock.restore("DynamoDB.DocumentClient");
return response;
}
But I have a function that scans a DynamoDB table like this:
export async function readAll(table) {
const params = {
TableName: table,
};
const scanResults = { Items: [] };
let items;
do {
items = await cli.scan(params).promise();
items.Items.forEach((item) => scanResults.Items.push(item));
params.ExclusiveStartKey = items.LastEvaluatedKey;
} while (typeof items.LastEvaluatedKey !== "undefined");
return scanResults;
}
When I try to use the same logic into the mock, I keep getting a region error, which means that my mock is trying to read my table directly, which it is not supposed to do.
Does anyone know how can I "mock" a scan operation using aws-sdk-mock?
I've been checking every piece of documentation I can find, but couldn't find something helpful for this problem.