m-radzikowski / aws-sdk-client-mock

AWS JavaScript SDK v3 mocks for easy unit testing. 🖋️ Typed 🔬 Tested 📄 Documented 🛠️ Maintained
https://m-radzikowski.github.io/aws-sdk-client-mock/
MIT License
749 stars 37 forks source link

Asymmetric Matchers not working #217

Closed marcusds closed 4 weeks ago

marcusds commented 2 months ago

Checklist

Bug description

Hi, thank for this package - it is great.

However, Asymmetric Matchers such as expect.objectContaining or expect.any(String) don't appear to be working. The following example does not match.

Example:

expect(IAMMock).toHaveReceivedCommandWith(
    AttachRolePolicyCommand,
    expect.objectContaining({
      RoleName: 'S3-Ingest-abc',
      PolicyArn: 'someArn',
    }),
  );

Am I doing something wrong? Or are they just not supported?

Reproduction

expect(IAMMock).toHaveReceivedCommandWith(
    AttachRolePolicyCommand,
    expect.objectContaining({
      RoleName: 'S3-Ingest-abc',
      PolicyArn: 'someArn',
    }),
  );

Environment

marcusds commented 2 months ago

For anyone else, I am working around this for now by doing:

await waitFor(() =>
        expect(S3Mock).toHaveReceivedCommand(GetBucketPolicyCommand),
      );
      await waitFor(() => {
        const args = S3Mock.commandCalls(GetBucketPolicyCommand)[0].args[0]
          .input;
        expect(args.Bucket).toStrictEqual('bucketName');
      });

But its not ideal.

mikhoq commented 2 months ago

I'm hitting this problem too. I get TypeError: this.customTesters is not iterable.

import 'aws-sdk-client-mock-jest'
import { BatchWriteCommand, DynamoDBDocumentClient, ScanCommand } from '@aws-sdk/lib-dynamodb'
import { mockClient } from 'aws-sdk-client-mock'

test('should...', async () => {
  const docClientMock = mockClient(DynamoDBDocumentClient)

  docClientMock.on(BatchWriteCommand).resolves({ ... })

  await callProdCode()

  expect(docClientMock).toHaveReceivedCommandTimes(BatchWriteCommand, 1) // ok
  const batchWriteCalls = docClientMock.commandCalls(BatchWriteCommand)
  const batchWriteInput = batchWriteCalls[0].args[0].input

  // causes "TypeError: this.customTesters is not iterable" when test is run
  expect(batchWriteInput).toMatchObject(expect.objectContaining({ ... }))
})

It seems to fall over somewhere matchers.js when it tries to do

const pass = ...
  ...this.customTesters

If I don't import aws-sdk-client-mock-jest //import 'aws-sdk-client-mock-jest' then I get TypeError: expect(...).toHaveReceivedCommandTimes is not a function and when I do import import 'aws-sdk-client-mock-jest' then I get TypeError: this.customTesters is not iterable

m-radzikowski commented 4 weeks ago

Hey, the toHaveReceivedCommandWith matcher already uses expect.objectContaining() under the hood. You can use other asymmetric matchers for properties:

import 'aws-sdk-client-mock-jest';
import {mockClient} from "aws-sdk-client-mock";
import {PublishCommand, SNSClient} from "@aws-sdk/client-sns";

const sns = new SNSClient();
const snsMock = mockClient(SNSClient);

it('uses asymmetric matcher', async () => {
  await sns.send(new PublishCommand({
    TopicArn: 'arn:aws:sns:us-east-1:111111111111:MyTopic',
    Message: 'hello world',
  }));

  expect(snsMock).toHaveReceivedCommandWith(
    PublishCommand,
    {
      Message: expect.stringContaining('hello'),
    },
  );
});

I've added this to the README and docs. If I'm missing something, please reopen.