Skip to content

Type error with Vitest matchers #252

@kyota-fujikura

Description

@kyota-fujikura

Checklist

  • I have read Caveats documentation and didn't find a solution for this problem there.

Bug description

When using matchers for Vitest, TypeScript reports the following error:

error TS2339: Property 'toHaveReceivedCommandWith' does not exist on type 'Assertion<AwsStub<ServiceInputTypes, ServiceOutputTypes, CognitoIdentityProviderClientResolvedConfig>>'.

This happens despite correctly importing aws-sdk-client-mock-jest/vitest in the test files.

The error occurs under either of these conditions:

  • When using ES modules ("type": "module" in package.json)
  • When using CommonJS ("type": "commonjs" in package.json) and importing "aws-sdk-client-mock-jest/vitest" before importing "vitest"

Adding import "vitest" at the top of the vitest.d.ts file (in node_modules/aws-sdk-client-mock-jest) seems to fix the issue, which matches what's shown in the Vitest official documentation.
I'm not familiar enough with TypeScript's module mechanisms to understand why this fixes the issue.

Reproduction

package.json

{
  "type": "module",
  "dependencies": {
    "@aws-sdk/client-lambda": "^3.777.0",
    "aws-sdk-client-mock": "^4.1.0",
    "aws-sdk-client-mock-jest": "^4.1.0",
    "typescript": "^5.8.2",
    "vitest": "^3.0.9"
  },
  "pnpm": {
    "overrides": {
      // fix https://github.com/m-radzikowski/aws-sdk-client-mock/issues/243
      "chalk": "^5.4.0"
    }
  }
}

tsconfig.json

{
    "compilerOptions": {
        "target": "es2022",
        "lib": ["es2022"],
        "module": "node16",
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true,
        "skipLibCheck": true,
        "types": ["vitest/globals"]
    },
    "include": ["src/**/*"]
}

vitest.config.ts

import { defineConfig } from "vitest/config";

export default defineConfig({
  test: {
    globals: true,
  },
});

test file

import "aws-sdk-client-mock-jest/vitest";
import { InvokeCommand, LambdaClient } from "@aws-sdk/client-lambda";
import { mockClient } from "aws-sdk-client-mock";
import "aws-sdk-client-mock-jest/vitest";

test("test", () => {
  const mockLambdaClient = mockClient(LambdaClient);

  // error occurs here
  expect(mockLambdaClient).toHaveReceivedCommand(InvokeCommand);
});

Environment

  • Node version: v20.9.0
  • Testing lib and version: vitest v3.0.9
  • Typescript version: v5.8.2
  • AWS SDK v3 Client mock version: v4.1.0
  • AWS JS SDK libs and versions: @aws-sdk/client-lambda: v3.777.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions