The results from the test depends on the order of the tests. Expect no items to be returned from the mock and then expect some items to be returned. This fails with both an extra item returned in the first test case and none in the second test case
describe('Change Max Winning', () => {
const ddbMock = mockClient(DynamoDBClient);
beforeEach(() => {
ddbMock.reset()
jest.restoreAllMocks();
});
test('build', async () => {
const ddbMock = mockClient(DynamoDBClient);
ddbMock
.on(QueryCommand)
.resolves({
Items: [
marshall(configItem),
],
})
.on(ScanCommand)
.resolves({Count: 0});
const resultBuild = await main(defaultEventWithBody, defaultContext);
expect(resultBuild).toEqual(expectedResultEmpty);
})
test('handle single group', async () => {
const ddbMockSingle = mockClient(DynamoDBClient);
ddbMockSingle
.on(QueryCommand)
.resolves({
Items: [
marshall(configItem),
],
})
.on(ScanCommand)
.resolves({Count: 0});
const result1 = await main(defaultEventWithBody, defaultContext);
expect(result1).toEqual(expectedResult1);
expect(ddbMockSingle).toHaveReceivedCommandTimes(PutItemCommand, 1);
})
If I switch the order of the test cases so that 'build' runs second, both tests work. 'build' which has an incorrect mock QueryCommand result receives no result and 'handle single group' receives the result it expects.