AWS SDK Vitest Mock - API Reference - v1.0.6
    Preparing search index...

    Variable matchersConst

    matchers: {
        toHaveReceivedCommand<
            TInput extends object,
            TOutput extends MetadataBearer,
        >(
            received: AwsClientStub,
            command: CommandConstructor<TInput, TOutput>,
        ): MatcherResult;
        toHaveReceivedCommandTimes<
            TInput extends object,
            TOutput extends MetadataBearer,
        >(
            received: AwsClientStub,
            command: CommandConstructor<TInput, TOutput>,
            times: number,
        ): MatcherResult;
        toHaveReceivedCommandWith<
            TInput extends object,
            TOutput extends MetadataBearer,
        >(
            this: { equals: (a: unknown, b: unknown) => boolean },
            received: AwsClientStub,
            command: CommandConstructor<TInput, TOutput>,
            input: TInput,
        ): MatcherResult;
        toHaveReceivedNthCommandWith<
            TInput extends object,
            TOutput extends MetadataBearer,
        >(
            this: { equals: (a: unknown, b: unknown) => boolean },
            received: AwsClientStub,
            n: number,
            command: CommandConstructor<TInput, TOutput>,
            input: TInput,
        ): MatcherResult;
        toHaveReceivedNoOtherCommands<
            TInput extends object,
            TOutput extends MetadataBearer,
        >(
            received: AwsClientStub,
            expectedCommands?: CommandConstructor<TInput, TOutput>[],
        ): MatcherResult;
    } = ...

    Custom Vitest matchers for asserting AWS SDK command calls.

    Type Declaration

    • toHaveReceivedCommand: function
      • Assert that a command was received at least once.

        Type Parameters

        • TInput extends object
        • TOutput extends MetadataBearer

        Parameters

        Returns MatcherResult

        Matcher result

        const s3Mock = mockClient(S3Client);
        const client = new S3Client({});

        await client.send(new GetObjectCommand({ Bucket: 'test', Key: 'file.txt' }));

        expect(s3Mock).toHaveReceivedCommand(GetObjectCommand);
    • toHaveReceivedCommandTimes: function
      • Assert that a command was received exactly N times.

        Type Parameters

        • TInput extends object
        • TOutput extends MetadataBearer

        Parameters

        • received: AwsClientStub

          The AWS client stub

        • command: CommandConstructor<TInput, TOutput>

          The command constructor to check for

        • times: number

          The exact number of times the command should have been received

        Returns MatcherResult

        Matcher result

        const s3Mock = mockClient(S3Client);
        const client = new S3Client({});

        await client.send(new GetObjectCommand({ Bucket: 'test', Key: 'file1.txt' }));
        await client.send(new GetObjectCommand({ Bucket: 'test', Key: 'file2.txt' }));

        expect(s3Mock).toHaveReceivedCommandTimes(GetObjectCommand, 2);
    • toHaveReceivedCommandWith: function
      • Assert that a command was received with specific input parameters.

        Type Parameters

        • TInput extends object
        • TOutput extends MetadataBearer

        Parameters

        • this: { equals: (a: unknown, b: unknown) => boolean }
        • received: AwsClientStub

          The AWS client stub

        • command: CommandConstructor<TInput, TOutput>

          The command constructor to check for

        • input: TInput

          The expected input parameters

        Returns MatcherResult

        Matcher result

        const s3Mock = mockClient(S3Client);
        const client = new S3Client({});

        await client.send(new GetObjectCommand({ Bucket: 'test', Key: 'file.txt' }));

        expect(s3Mock).toHaveReceivedCommandWith(GetObjectCommand, {
        Bucket: 'test',
        Key: 'file.txt'
        });
    • toHaveReceivedNthCommandWith: function
      • Assert that the Nth command call was a specific command with specific input.

        Type Parameters

        • TInput extends object
        • TOutput extends MetadataBearer

        Parameters

        • this: { equals: (a: unknown, b: unknown) => boolean }
        • received: AwsClientStub

          The AWS client stub

        • n: number

          The call number (1-indexed)

        • command: CommandConstructor<TInput, TOutput>

          The command constructor to check for

        • input: TInput

          The expected input parameters

        Returns MatcherResult

        Matcher result

        const s3Mock = mockClient(S3Client);
        const client = new S3Client({});

        await client.send(new PutObjectCommand({ Bucket: 'test', Key: 'file1.txt' }));
        await client.send(new GetObjectCommand({ Bucket: 'test', Key: 'file2.txt' }));

        expect(s3Mock).toHaveReceivedNthCommandWith(2, GetObjectCommand, {
        Bucket: 'test',
        Key: 'file2.txt'
        });
    • toHaveReceivedNoOtherCommands: function
      • Assert that no commands other than the expected ones were received.

        Type Parameters

        • TInput extends object
        • TOutput extends MetadataBearer

        Parameters

        • received: AwsClientStub

          The AWS client stub

        • expectedCommands: CommandConstructor<TInput, TOutput>[] = []

          Array of command constructors that are allowed

        Returns MatcherResult

        Matcher result

        const s3Mock = mockClient(S3Client);
        const client = new S3Client({});

        await client.send(new GetObjectCommand({ Bucket: 'test', Key: 'file.txt' }));
        await client.send(new GetObjectCommand({ Bucket: 'test', Key: 'other.txt' }));

        expect(s3Mock).toHaveReceivedNoOtherCommands([GetObjectCommand]);
    import { expect } from 'vitest';
    import { matchers } from 'aws-sdk-vitest-mock';

    expect.extend(matchers);