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
aws-sdk-client-mock
Easy and powerful mocking of AWS SDK v3 Clients
aws-sdk-client-mock-jest
Custom Jest matchers for AWS SDK v3 Client mock
@aws-sdk/client-sso
AWS SDK for JavaScript Sso Client for Node.js, Browser and React Native
aws-sdk-mock
Functions to mock the JavaScript aws-sdk
Custom vitest matcher for aws-sdk-client-mock
npm install aws-sdk-client-mock-vitest
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
18 Stars
36 Commits
1 Forks
1 Watching
2 Branches
1 Contributors
Updated on 17 Oct 2024
Minified
Minified + Gzipped
TypeScript (95.73%)
Shell (2.57%)
JavaScript (1.7%)
Cumulative downloads
Total Downloads
Last day
1.1%
3,691
Compared to previous day
Last week
21%
20,322
Compared to previous week
Last month
22.4%
74,668
Compared to previous month
Last year
9,623.3%
293,741
Compared to previous year
4
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). Feel free to only extend the matchers you are intending to use
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 mat chers 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} from "aws-sdk-client-mock-vitest"; 30 31expect.extend({ 32 toReceiveCommandTimes, 33 toHaveReceivedCommandTimes, 34 toReceiveCommandOnce, 35 toHaveReceivedCommandOnce, 36 toReceiveCommand, 37 toHaveReceivedCommand, 38 toReceiveCommandWith, 39 toHaveReceivedCommandWith, 40 toReceiveNthCommandWith, 41 toHaveReceivedNthCommandWith, 42 toReceiveLastCommandWith, 43 toHaveReceivedLastCommandWith, 44});
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.