I’ve got code that uses GetItemCommand
to get data from my DynamoDb instance, however I’m not entirely sure how I go about testing my function that uses it.
I don’t want to test the GetItemCommand
itself, I just want to mock out the value it returns.
db.ts
:
export const getFromDb = async (id) => {
const command = new GetItemCommand({
TableName: 'table-name',
Key: {
ID: { S: id },
},
});
try {
return await client.send(command);
} catch (err) {
// do something
}
};
function.ts
:
export const doStuff = async () => {
...
const dbData: GetItemCommandOutput = await getFromDb(id);
if (!dbData.Item) throw Error("no record found");
...
}
So far I have tried the following:
test.ts
:
import * as dbStuff from './db';
const mockGetData = jest.spyOn(dbStuff, "getFromDb").mockImplementation((y) => Promise.resolve({Item: []}));
Anything I have tried, my IDE tells me:
XYZ is not assignable to parameter of type Promise<GetItemCommandOutput>
8