
Research
/Security News
9 Malicious NuGet Packages Deliver Time-Delayed Destructive Payloads
Socket researchers discovered nine malicious NuGet packages that use time-delayed payloads to crash applications and corrupt industrial control systems.
aws-sdk-client-mock-vitest
Advanced tools
Custom matchers for AWS SDK Client mock to be used in vitest
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
[!IMPORTANT]
This package depends on@vitest/expectso you normally want to use a package version that fits the vite version you are using. The latest version should work forvitestversion4. If you are using an older version ofvitest, you should stick to an earlier version as well.If you use vitest
3:npm run install --save-dev aws-sdk-client-mock-vitest@^6.2.1If you use vitest
2:npm run install --save-dev aws-sdk-client-mock-vitest@^5.1.0If you use vitest
1:npm run install --save-dev aws-sdk-client-mock-vitest@^3.0.0
In order to use the new matchers, we have to register them. The easiest way is to do this in a setup file.
You can either use a helper to extend with all matchers or manually only extend the matchers you are interested in.
Create a a new file tests/setup.ts with the following content (or adapt an
existing one):
// tests/setup.ts
import "aws-sdk-client-mock-vitest/extend";
Now ensure this file is loaded by vitest before each test by adapting your
vite.config.ts file:
import { defineConfig } from "vitest/config";
export default defineConfig({
test: {
// Ensure our file is loaded and matchers are extended
setupFiles: ["tests/setup.ts"],
},
});
Alternatively, you can manually register matchers.
Create a a new file tests/setup.ts with the following content (or adapt an
existing one):
// tests/setup.ts
import { expect } from "vitest";
import { allCustomMatcher } from "aws-sdk-client-mock-vitest";
expect.extend(allCustomMatcher);
We can only extend with individual matchers:
// tests/setup.ts
import { expect } from "vitest";
import {
toReceiveCommandTimes,
toHaveReceivedCommandTimes,
toReceiveCommandOnce,
toHaveReceivedCommandOnce,
toReceiveCommand,
toHaveReceivedCommand,
toReceiveCommandWith,
toHaveReceivedCommandWith,
toReceiveNthCommandWith,
toHaveReceivedNthCommandWith,
toReceiveLastCommandWith,
toHaveReceivedLastCommandWith,
toReceiveAnyCommand,
toHaveReceivedAnyCommand,
toReceiveCommandExactlyOnceWith,
toHaveReceivedCommandExactlyOnceWith,
} from "aws-sdk-client-mock-vitest";
expect.extend({
toReceiveCommandTimes,
toHaveReceivedCommandTimes,
toReceiveCommandOnce,
toHaveReceivedCommandOnce,
toReceiveCommand,
toHaveReceivedCommand,
toReceiveCommandWith,
toHaveReceivedCommandWith,
toReceiveNthCommandWith,
toHaveReceivedNthCommandWith,
toReceiveLastCommandWith,
toHaveReceivedLastCommandWith,
toReceiveAnyCommand,
toHaveReceivedAnyCommand,
toReceiveCommandExactlyOnceWith,
toHaveReceivedCommandExactlyOnceWith,
});
Now ensure this file is loaded by vitest before each test by adapting your
vite.config.ts file:
import { defineConfig } from "vitest/config";
export default defineConfig({
test: {
// Ensure our file is loaded and matchers are extended
setupFiles: ["tests/setup.ts"],
},
});
[!NOTE] If you're using the automatic setup with
import "aws-sdk-client-mock-vitest/extend"and a moduleResoltion setting of at leastnode16orbundlerTypeScript declarations are automatically included and you can skip this section.
Create a vitest.d.ts file with the following content
// tests/vitest.d.ts
import "vitest";
import { CustomMatcher } from "aws-sdk-client-mock-vitest";
declare module "vitest" {
interface Matchers<T = any> extends CustomMatcher<T> {}
}
[!TIP] If you are using
eslintin your project you may want to add the following lines at the beginning of yourvitest.d.tsfile:/* eslint-disable @typescript-eslint/no-empty-object-type */ /* eslint-disable @typescript-eslint/no-explicit-any */
If you get the following error in your tests
Error: Invalid Chai property: toHaveReceivedCommandWith
Then you probably forgot to run expect.extend with the matcher you are using in your test (see above)
Lets assume you have written a function to read a secret from the AWS Secrets Manager. It may look like this:
// src/main.ts
import {
SecretsManagerClient,
GetSecretValueCommand,
} from "@aws-sdk/client-secrets-manager";
export async function readSecret(secretId: string): Promise<string> {
const client = new SecretsManagerClient({});
const command = new GetSecretValueCommand({ SecretId: secretId });
const response = await client.send(command);
if (response.SecretString) {
return response.SecretString;
}
throw new Error("Unable to read the secret");
}
Naturally we want to test this function to verify it either returns the secret
when found or raises an exception otherwise. But we do not want to do actual
AWS API calls. We can write a test with vite without doing any network requests
thanks to aws-sdk-client-mock. This test may look like this:
// tests/main.test.ts
import { describe, it, expect } from "vitest";
import { mockClient } from "aws-sdk-client-mock";
import {
GetSecretValueCommand,
SecretsManagerClient,
} from "@aws-sdk/client-secrets-manager";
import { readSecret } from "../src/main";
const smMock = mockClient(SecretsManagerClient);
describe("readSecret", () => {
it("should return the secret value", async () => {
/* Setup our mock. In this test the secret will always be secr3t */
smMock.on(GetSecretValueCommand).resolves({ SecretString: "secr3t" });
const result = await readSecret("foo");
expect(result).toBe("secr3t");
// We have not verified that we actually interacted with our
// Secret Manager correcty
});
});
The test above verifies our readSecret function does indeed return the value
of the secret when the API response includes a SecretString. However we have
not validated we are actually retrieving the correct secret.
We may want to actually inspect our mock client to verify we sent
a specific command. We can do this with our custom expect matcher
expect(mockClient).toHaveReceivedCommandWith(...).
To make use of it we are changing our testfile and registering custom matchers
// tests/main.test.ts
import { describe, it, expect } from "vitest";
import { mockClient } from "aws-sdk-client-mock";
import {
GetSecretValueCommand,
SecretsManagerClient,
} from "@aws-sdk/client-secrets-manager";
import {
CustomMatcher,
toHaveReceivedCommandWith,
} from "aws-sdk-client-mock-vitest";
/* you can also run this in setupTests, see above */
expect.extend({ toHaveReceivedCommandWith });
/* You may want to put this in some vitest.d.ts, see above */
declare module "vitest" {
interface Assertion<T = any> extends CustomMatcher<T> {}
interface AsymmetricMatchersContaining extends CustomMatcher {}
}
import { readSecret } from "../src/main";
const smMock = mockClient(SecretsManagerClient);
describe("readSecret", () => {
it("should read it", async () => {
smMock.on(GetSecretValueCommand).resolves({ SecretString: "secr3t" });
const result = await readSecret("foo");
expect(result).toBe("secr3t");
/* Ensure we use the inut of the function to fetch the correct secret */
expect(smMock).toHaveReceivedCommandWith(GetSecretValueCommand, {
SecretId: "foo",
});
});
});
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
FAQs
Custom matchers for AWS SDK Client mock to be used in vitest
The npm package aws-sdk-client-mock-vitest receives a total of 72,776 weekly downloads. As such, aws-sdk-client-mock-vitest popularity was classified as popular.
We found that aws-sdk-client-mock-vitest demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Research
/Security News
Socket researchers discovered nine malicious NuGet packages that use time-delayed payloads to crash applications and corrupt industrial control systems.

Security News
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.