Gathering detailed insights and metrics for aws-sdk-client-mock-vitest
Gathering detailed insights and metrics for aws-sdk-client-mock-vitest
Gathering detailed insights and metrics for aws-sdk-client-mock-vitest
Gathering detailed insights and metrics for aws-sdk-client-mock-vitest
Custom vitest matcher for aws-sdk-client-mock
npm install aws-sdk-client-mock-vitest
Typescript
Module System
Node Version
NPM Version
97
Supply Chain
97.7
Quality
80.5
Maintenance
100
Vulnerability
98.9
License
TypeScript (97.21%)
Shell (1.78%)
JavaScript (1.01%)
Total Downloads
1,020,410
Last Day
917
Last Week
32,661
Last Month
143,412
Last Year
992,873
MIT License
23 Stars
83 Commits
2 Forks
1 Watchers
9 Branches
1 Contributors
Updated on May 07, 2025
Minified
Minified + Gzipped
Latest Version
6.1.1
Package Id
aws-sdk-client-mock-vitest@6.1.1
Unpacked Size
39.57 kB
Size
8.07 kB
File Count
15
NPM Version
10.9.2
Node Version
22.13.1
Published on
Feb 04, 2025
Cumulative downloads
Total Downloads
Last Day
-29.7%
917
Compared to previous day
Last Week
-15%
32,661
Compared to previous week
Last Month
6.4%
143,412
Compared to previous month
Last Year
3,505.6%
992,873
Compared to previous year
2
2
This module adds custom matchers to verfiy calls to your AWS Client Mock. It was heavily inspired by aws-sdk-client-mock-jest.
You develop code that makes use of the AWS SDK for JavaScript v3. You are already writing tests for it through the great aws-sdk-client-mock package. You also want to ensure that your actual code performs certain calls against your AWS Client Mocks. While there is aws-sdk-client-mock-jest you prefer vitest.
You can use this module to use expect extensions for vitest to ensure certain commands have been called on your AWS clients.
npm install --save-dev aws-sdk-client-mock-vitest
You must register the new matchers explicity (think about putting this to a setup file). You can either just register the matchers you are interested in, or register all matchers
To register all matchers use the following:
1/* 2 you may want to put the following into a file tests/setup.ts 3 and then specify your vite.config.ts as such 4 5 import { defineConfig } from "vitest/config"; 6 7 export default defineConfig({ 8 test: { 9 setupFiles: ["tests/setup.ts"], 10 }, 11 }); 12 13 to add the custom matchers before each test run 14*/ 15import { expect } from "vitest"; 16import { allCustomMatcher } from "aws-sdk-client-mock-vitest"; 17 18expect.extend(allCustomMatcher)
You can also register just the matchers you care about:
1/* 2 you may want to put the following into a file tests/setup.ts 3 and then specify your vite.config.ts as such 4 5 import { defineConfig } from "vitest/config"; 6 7 export default defineConfig({ 8 test: { 9 setupFiles: ["tests/setup.ts"], 10 }, 11 }); 12 13 to add the custom matchers before each test run 14*/ 15import { expect } from "vitest"; 16import { 17 toReceiveCommandTimes, 18 toHaveReceivedCommandTimes, 19 toReceiveCommandOnce, 20 toHaveReceivedCommandOnce, 21 toReceiveCommand, 22 toHaveReceivedCommand, 23 toReceiveCommandWith, 24 toHaveReceivedCommandWith, 25 toReceiveNthCommandWith, 26 toHaveReceivedNthCommandWith, 27 toReceiveLastCommandWith, 28 toHaveReceivedLastCommandWith, 29 toReceiveAnyCommand, 30 toHaveReceivedAnyCommand, 31 toReceiveCommandExactlyOnceWith, 32 toHaveReceivedCommandExactlyOnceWith, 33} from "aws-sdk-client-mock-vitest"; 34 35expect.extend({ 36 toReceiveCommandTimes, 37 toHaveReceivedCommandTimes, 38 toReceiveCommandOnce, 39 toHaveReceivedCommandOnce, 40 toReceiveCommand, 41 toHaveReceivedCommand, 42 toReceiveCommandWith, 43 toHaveReceivedCommandWith, 44 toReceiveNthCommandWith, 45 toHaveReceivedNthCommandWith, 46 toReceiveLastCommandWith, 47 toHaveReceivedLastCommandWith, 48 toReceiveAnyCommand, 49 toHaveReceivedAnyCommand, 50 toReceiveCommandExactlyOnceWith, 51 toHaveReceivedCommandExactlyOnceWith, 52});
In case you are using typescript, create a vitest.d.ts
file with the following content
1// tests/vitest.d.ts 2import "vitest"; 3import { CustomMatcher } from "aws-sdk-client-mock-vitest"; 4 5declare module "vitest" { 6 interface Assertion<T = any> extends CustomMatcher<T> {} 7 interface AsymmetricMatchersContaining extends CustomMatcher {} 8}
If you get the following error in your tests
Error: Invalid Chai property: toHaveReceivedCommandWith
Then your probably forgot to run expect.extend
with the matcher you are using in your test (see above)
Lets assume you have code that retrieves a secret from the AWS Secrets Manager
1// src/main.ts 2import { 3 SecretsManagerClient, 4 GetSecretValueCommand, 5} from "@aws-sdk/client-secrets-manager"; 6 7export async function readSecret(secretId: string): Promise<string> { 8 const client = new SecretsManagerClient({}); 9 const command = new GetSecretValueCommand({ SecretId: secretId }); 10 const response = await client.send(command); 11 if (response.SecretString) { 12 return response.SecretString; 13 } 14 throw new Error("Unable to read the secret"); 15}
You can test this with vite without doing any network requests thanks to
aws-sdk-client-mock
1// tests/main.test.ts 2import { describe, it, expect } from "vitest"; 3import { mockClient } from "aws-sdk-client-mock"; 4import { 5 GetSecretValueCommand, 6 SecretsManagerClient, 7} from "@aws-sdk/client-secrets-manager"; 8 9import { readSecret } from "../src/main"; 10 11const smMock = mockClient(SecretsManagerClient); 12 13describe("readSecret", () => { 14 it("should return the secret value", async () => { 15 /* Setup our mock. In this test the secret will always be secr3t */ 16 smMock.on(GetSecretValueCommand).resolves({ SecretString: "secr3t" }); 17 18 const result = await readSecret("foo"); 19 expect(result).toBe("secr3t"); 20 21 // We have not verified that we actually interacted with our 22 // Secret Manager correcty 23 }); 24});
But we may want to actually inspect our mock client to verify that we actually have sent a specific command. We can do this by changing our testfile and registering custom matchers.
1// tests/main.test.ts 2import { describe, it, expect } from "vitest"; 3import { mockClient } from "aws-sdk-client-mock"; 4import { 5 GetSecretValueCommand, 6 SecretsManagerClient, 7} from "@aws-sdk/client-secrets-manager"; 8 9import { 10 CustomMatcher, 11 toHaveReceivedCommandWith, 12} from "aws-sdk-client-mock-vitest"; 13 14/* you can also run this in setupTests, see above */ 15expect.extend({ toHaveReceivedCommandWith }); 16 17/* You may want to put this in some vitest.d.ts, see above */ 18declare module "vitest" { 19 interface Assertion<T = any> extends CustomMatcher<T> {} 20 interface AsymmetricMatchersContaining extends CustomMatcher {} 21} 22 23import { readSecret } from "../src/main"; 24 25const smMock = mockClient(SecretsManagerClient); 26 27describe("readSecret", () => { 28 it("should read it", async () => { 29 smMock.on(GetSecretValueCommand).resolves({ SecretString: "secr3t" }); 30 31 const result = await readSecret("foo"); 32 expect(result).toBe("secr3t"); 33 34 /* Ensure we use the inut of the function to fetch the correct secret */ 35 expect(smMock).toHaveReceivedCommandWith(GetSecretValueCommand, { 36 SecretId: "foo", 37 }); 38 }); 39});
In order to run tests locally, execute the following
npm ci
npm run test:coverage
If you get an ERR_INSPECTOR_NOT_AVAILABLE
error, make sure your nodejs is compiled with
inspector
support. Otherwise run npm run test
to skip code coverage
I would like to thank Maciej Radzikowski for the awesome aws-sdk-client-mock
and
aws-sdk-client-mock-jest
packages. These helped a lot testing AWS code and also
helped building this library
No vulnerabilities found.
No security vulnerabilities found.