aws-sdk-client-mock icon indicating copy to clipboard operation
aws-sdk-client-mock copied to clipboard

Jest matchers don't allow use of jest submatchers for input object filtering

Open taschmidt opened this issue 3 years ago • 5 comments

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

taschmidt avatar Jun 14 '22 21:06 taschmidt

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.

taschmidt avatar Jun 14 '22 22:06 taschmidt

Thank you, I will check what's the standard way of achieving this with custom Jest matchers and make an update.

m-radzikowski avatar Jun 17 '22 20:06 m-radzikowski

Running into this issue too, when migrating from v2 to v3 and trying to update our tests :(.

bernardobridge avatar Aug 04 '22 15:08 bernardobridge

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.

m-radzikowski avatar Aug 04 '22 16:08 m-radzikowski

Great news @m-radzikowski !

taschmidt avatar Aug 04 '22 16:08 taschmidt

Merged, I will update later on the release.

m-radzikowski avatar Aug 15 '22 11:08 m-radzikowski

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.

m-radzikowski avatar Aug 21 '22 19:08 m-radzikowski

Thanks @m-radzikowski !! What was the reasoning behind the separate packages?

taschmidt avatar Aug 22 '22 12:08 taschmidt

#101

m-radzikowski avatar Aug 22 '22 12:08 m-radzikowski