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

    Interface AwsClientStub<TClient>

    Client stub for configuring and managing mock behaviors for an AWS SDK client.

    const s3Mock = mockClient(S3Client);

    // Configure mock behavior
    s3Mock.on(GetObjectCommand).resolves({ Body: 'data' });

    // Mock works as configured
    await client.send(new GetObjectCommand({ Bucket: 'test', Key: 'file.txt' }));
    expect(s3Mock.calls()).toHaveLength(1);

    // Reset clears call history but keeps mock configuration
    s3Mock.reset();
    expect(s3Mock.calls()).toHaveLength(0);

    // Mock still works after reset
    await client.send(new GetObjectCommand({ Bucket: 'test', Key: 'file.txt' }));
    expect(s3Mock.calls()).toHaveLength(1);
    interface AwsClientStub<TClient extends AnyClient = AnyClient> {
        client: TClient;
        on: <TCtor extends AwsCommandConstructor>(
            command: TCtor,
            request?: Partial<CommandInputType<TCtor>>,
            options?: MockOptions,
        ) => AwsCommandStub<
            CommandInputType<TCtor>,
            CommandOutputType<TCtor>,
            TClient,
        >;
        reset: () => void;
        restore: () => void;
        calls: () => AwsSdkCommand[];
        enableDebug: () => void;
        disableDebug: () => void;
    }

    Type Parameters

    • TClient extends AnyClient = AnyClient
    Index

    Properties

    client: TClient

    The client instance being mocked (undefined for class-level mocks).

    on: <TCtor extends AwsCommandConstructor>(
        command: TCtor,
        request?: Partial<CommandInputType<TCtor>>,
        options?: MockOptions,
    ) => AwsCommandStub<
        CommandInputType<TCtor>,
        CommandOutputType<TCtor>,
        TClient,
    >

    Configure mock behavior for a specific command.

    Type Declaration

      • <TCtor extends AwsCommandConstructor>(
            command: TCtor,
            request?: Partial<CommandInputType<TCtor>>,
            options?: MockOptions,
        ): AwsCommandStub<
            CommandInputType<TCtor>,
            CommandOutputType<TCtor>,
            TClient,
        >
      • Type Parameters

        • TCtor extends AwsCommandConstructor

        Parameters

        • command: TCtor

          The AWS SDK command constructor to mock

        • Optionalrequest: Partial<CommandInputType<TCtor>>

          Optional partial input to match against (uses partial matching by default)

        • Optionaloptions: MockOptions

          Optional configuration for strict matching

        Returns AwsCommandStub<CommandInputType<TCtor>, CommandOutputType<TCtor>, TClient>

        A command stub for configuring mock responses

    s3Mock.on(GetObjectCommand).resolves({ Body: 'data' });
    
    s3Mock.on(GetObjectCommand, { Bucket: 'my-bucket' }).resolves({ Body: 'bucket data' });
    
    s3Mock.on(GetObjectCommand, { Bucket: 'my-bucket', Key: 'file.txt' }, { strict: true })
    .resolves({ Body: 'exact match' });
    reset: () => void

    Clear mock call history while preserving configured behaviors. Mock configurations remain active after reset, only the call history is cleared. Use this between tests when you want to reuse the same mock setup.

    afterEach(() => {
    s3Mock.reset();
    });
    restore: () => void

    Restore the original client behavior and clear all mocks. After calling restore, the client will no longer be mocked.

    afterAll(() => {
    s3Mock.restore();
    });
    calls: () => AwsSdkCommand[]

    Get an array of all commands that were sent to the client.

    Type Declaration

      • (): AwsSdkCommand[]
      • Returns AwsSdkCommand[]

        Array of AWS SDK commands

    const calls = s3Mock.calls();
    console.log(calls.length); // Number of commands sent
    console.log(calls[0].input); // Input of first command
    enableDebug: () => void

    Enable debug logging to see detailed information about mock configuration and interactions. Logs both mock setup (when .on(), .resolves(), etc. are called) and command execution details. Useful for troubleshooting mock configurations and why commands aren't matching expected mocks.

    s3Mock.enableDebug();
    // Logs will appear for:
    // - Mock configuration (when .on(), .resolves(), etc. are called)
    // - Command execution (incoming commands and inputs)
    // - Mock matching results and reasons for failures
    // - Lifecycle events (reset, restore)
    disableDebug: () => void

    Disable debug logging for mock configuration and interactions.

    s3Mock.disableDebug();