aws-sdk-client-mock
aws-sdk-client-mock copied to clipboard
Jest matchers don't allow use of jest submatchers for input object filtering
Checklist
- [x] I have read Caveats documentation and didn't find a solution for this problem there.
Bug description
The Jest matchers added here still use the sinon call matching logic here so jest submatchers don't work. For example, I would expect this code to work:
expect(mock).toHaveReceivedCommandWith(QueryCommand, {
TableName: expect.stringMatching(/^foo/)
});
Instead I get the following error:
Error: Expected DynamoDBDocumentClient to receive "QueryCommand" with {"TableName": StringMatching /^foo-/}
DynamoDBDocumentClient received "QueryCommand" 0 times
Calls:
1. QueryCommand: {"ExpressionAttributeValues": {":guid": "guid"}, "KeyConditionExpression": "id = :guid", "Limit": 1, "ScanIndexForward": false, "TableName": "foo-undefined"}
Environment
- Node version: 14.19.3
- Testing lib and version: Jest
- Typescript version: 4.2.4
- AWS SDK v3 Client mock version: 1.0.0
- AWS JS SDK libs and versions:
- @aws-sdk/client-dynamodb: 3.49.0
- @aws-sdk/lib-dynamodb: 3.49.0
I played around and it seems to work if I add this function:
function checkMatch(callInput, input) {
try {
expect(callInput).toMatchObject(input);
return true;
} catch {
return false;
}
}
and then change this line to this:
const commandCalls = calls.filter(call =>
call.args[0] instanceof command && checkMatch(call.args[0].input, input)
);
But I don't know if that's the recommended approach.
Thank you, I will check what's the standard way of achieving this with custom Jest matchers and make an update.
Running into this issue too, when migrating from v2 to v3 and trying to update our tests :(.
I did some work in this regard on the jest-matchers-asymmetric branch. I think I have this figured out. I did not proceed with merging it because I think I will need to extract Jest matchers as a separate package anyway due to conflicts when using Mocha (#101).
I can't provide you with a date when I will sit and work on this but will try to make it sooner than later.
Great news @m-radzikowski !
Merged, I will update later on the release.
Jest matchers were moved to a separate package: aws-sdk-client-mock-jest.
It was just released as a beta version (v2.0.0-beta.2). The corresponding beta release of the main package, aws-sdk-client-mock, does not include Jest matchers anymore.
Thanks @m-radzikowski !! What was the reasoning behind the separate packages?
#101