AWS SDK Client Mock Vitest Matchers

7.0.1 · active · verified Tue Apr 21

This package provides custom Vitest matchers for `aws-sdk-client-mock`, a library used to mock the AWS SDK for JavaScript v3 clients in tests. It enables developers to write expressive assertions about how AWS service commands are called on their mocked clients within a Vitest testing environment. The current stable version is `7.0.1`, with releases often aligned with major Vitest versions. Key differentiators include its tight integration with Vitest's `expect` API, offering a wide range of matchers such as `toHaveReceivedCommand`, `toHaveReceivedCommandWith`, and `toHaveReceivedCommandTimes`. This package fills a crucial gap for Vitest users, providing functionality analogous to `aws-sdk-client-mock-jest` for Jest, allowing for robust verification of AWS SDK interactions in unit and integration tests.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to configure Vitest to automatically load the custom matchers and then write a test verifying an S3 `GetObjectCommand` call using `toHaveReceivedCommandExactlyOnceWith`.

import { defineConfig } from "vitest/config";
import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3";
import { mockClient } from "aws-sdk-client-mock";

// vite.config.ts
export default defineConfig({
  test: {
    setupFiles: ["./tests/setup.ts"],
  },
});

// tests/setup.ts
import "aws-sdk-client-mock-vitest/extend";

// tests/s3.test.ts
describe("S3 Service", () => {
  let s3Mock;

  beforeEach(() => {
    s3Mock = mockClient(S3Client);
  });

  afterEach(() => {
    s3Mock.reset();
  });

  it("should call GetObjectCommand exactly once with correct parameters", async () => {
    s3Mock.on(GetObjectCommand).resolves({ Body: "mocked-content" });

    const client = new S3Client({});
    await client.send(new GetObjectCommand({
      Bucket: "my-bucket",
      Key: "test-key.txt",
    }));

    expect(s3Mock).toHaveReceivedCommandExactlyOnceWith(GetObjectCommand, {
      Bucket: "my-bucket",
      Key: "test-key.txt",
    });
  });
});

view raw JSON →