Easy and powerful mocking of AWS SDK v3 Clients
npm install aws-sdk-client-mockEasy and powerful mocking of AWS SDK v3 Clients.


Library recommended by the AWS SDK for JavaScript team - see the introductory post on the AWS blog.
Features:
- 🌊 fluent interface - declaring behavior is short and readable
- 🔍 matching options - defining mock behavior by Command type and/or its input payload
- 🕵️ spying - checking if Commands were actually sent
- 🃏 Jest matchers - easily verifying sent Commands
- 🖋️ fully typed - same type control for declaring mock's behavior as when writing regular code
- ✅ fully tested - reliable mocks help instead of impeding
In action:
- About AWS SDK v3
- Usage
- Install
- Versions compatibility
- Import
- Mock
- DynamoDB DocumentClient
- Lib Storage Upload
- S3 GetObjectCommand
- Paginated operations
- SDK v2-style mocks
- Inspect
- Reset and restore
- Custom matchers
- Jest
- Vitest
- API Reference
- AWS Lambda example
- Caveats
- Mixed @smithy/types versions
- AwsClientStub and strictFunctionTypes
- Order of mock behaviors
- Order of type and instance mocks
- Using with Mocha
The AWS SDK for JavaScript version 3,
is the new version of SDK to use in Node.js and browser.
It comes with modular architecture and improved typing,
thanks to being written in TypeScript.
The recommended way of using it is to create a Client
and use it to send Commands.
For example, using SNS Client to publish a message to a topic looks like that:
``typescript
import {PublishCommand, SNSClient} from '@aws-sdk/client-sns';
const sns = new SNSClient({});
const result = await sns.send(new PublishCommand({
TopicArn: 'arn:aws:sns:us-east-1:111111111111:MyTopic',
Message: 'My message',
}));
console.log(Message published, id: ${result.MessageId});`
This library provides an easy way to mock sending CommandsCommand
and define returned results depending on the type and payload.
`bash`
npm install -D aws-sdk-client-mock
> Warning
> If you are getting type errors Argument of type 'typeof SomeClient' is not assignable to parameter of type...
> see instructions here.
#### Versions compatibility
| @aws-sdk/* | aws-sdk-client-mock |
|--------------|-----------------------|
| ≥ 3.363.0 | ≥ 3.x |
| < 3.363.0 | 2.x |
CommonJS:
`javascript`
const {mockClient} = require('aws-sdk-client-mock');
TypeScript / ES6:
`typescript`
import {mockClient} from 'aws-sdk-client-mock';
Create mock for all instances or for given instance of the AWS SDK Client:
`typescript
const snsMock = mockClient(SNSClient);
const dynamoDB = new DynamoDBClient({});
const dynamoDBMock = mockClient(dynamoDB);
`
By default, mocked Client#send() method returns undefined.
Using the obtained mock instance, you can specify the mock behavior
on receiving various commands to send.
See the AwsStub API Reference
for all available methods or check out the examples below.
Specify default mock behavior:
`typescript
snsMock.onAnyCommand().resolves({});
// same as:
snsMock.resolves({});
`
Specify mock behavior on receiving given command only:
`typescript`
snsMock
.on(PublishCommand)
.resolves({
MessageId: '12345678-1111-2222-3333-111122223333',
});
Specify mock behavior on receiving given command with given payload only:
`typescript`
snsMock
.on(PublishCommand, {
TopicArn: 'arn:aws:sns:us-east-1:111111111111:MyTopic',
Message: 'My message',
})
.resolves({
MessageId: '12345678-4444-5555-6666-111122223333',
});
Not all payload parameters must be defined to match
(you can force strict matching by passing third param strict: true):
`typescript`
snsMock
.on(PublishCommand, {
Message: 'My message',
})
.resolves({
MessageId: '12345678-4444-5555-6666-111122223333',
});
Specify mock behavior on receiving given payload only:
`typescript`
snsMock
.onAnyCommand({
TopicArn: 'arn:aws:sns:us-east-1:111111111111:MyTopic',
Message: 'My message',
})
.resolves({
MessageId: '12345678-4444-5555-6666-111122223333',
});
Multiple behaviors (for different commands and payloads) may be specified
for a single mock:
`typescript`
snsMock
.resolves({ // default for any command
MessageId: '12345678-1111-2222-3333-111122223333'
})
.on(PublishCommand)
.resolves({ // default for PublishCommand
MessageId: '12345678-4444-5555-6666-111122223333'
})
.on(PublishCommand, {
TopicArn: 'arn:aws:sns:us-east-1:111111111111:MyTopic',
Message: 'My message',
})
.resolves({ // for PublishCommand with given input
MessageId: '12345678-7777-8888-9999-111122223333',
});
Specify chained behaviors - next behaviors for consecutive calls:
`typescript`
snsMock
.on(PublishCommand)
.resolvesOnce({ // for the first command call
MessageId: '12345678-1111-1111-1111-111122223333'
})
.resolvesOnce({ // for the second command call
MessageId: '12345678-2222-2222-2222-111122223333'
})
.resolves({ // for further calls
MessageId: '12345678-3333-3333-3333-111122223333'
});
Specify mock throwing an error:
`typescript`
snsMock
.rejects('mocked rejection');
`typescript
const throttlingError = new Error('mocked rejection');
throttlingError.name = 'ThrottlingException';
snsMock
.rejects(throttlingError);
`
In rejects(), you can pass a string, an Error instance,Error
or an object with properties.
In each case, it will be converted to an instance.
Specify custom mock function:
`typescript`
snsMock
.callsFake(input => {
if (input.Message === 'My message') {
return {MessageId: '12345678-1111-2222-3333-111122223333'};
} else {
throw new Error('mocked rejection');
}
});
Specify custom mock function for a specific command (chained behavior):
`typescript`
snsMock
.on(PublishCommand)
.callsFake(input => {
if (input.Message === 'My message') {
return {MessageId: '12345678-1111-2222-3333-111122223333'};
} else {
throw new Error('mocked rejection');
}
});
Specify result based on Client configuration, i.e. region:
`typescript`
snsMock
.on(PublishCommand)
.callsFake(async (input, getClient) => {
const client = getClient();
const region = await client.config.region();
return {MessageId: region.substring(0, 2)};
});
Together with resolvesOnce(), you can also use rejectsOnce() and callsFakeOnce()
to specify consecutive behaviors.
#### DynamoDB DocumentClient
You can mock the DynamoDBDocumentClient just like any other Client:
`typescript
import {DynamoDBClient} from '@aws-sdk/client-dynamodb';
import {DynamoDBDocumentClient, QueryCommand} from '@aws-sdk/lib-dynamodb';
const ddbMock = mockClient(DynamoDBDocumentClient);
ddbMock.on(QueryCommand).resolves({
Items: [{pk: 'a', sk: 'b'}],
});
const dynamodb = new DynamoDBClient({});
const ddb = DynamoDBDocumentClient.from(dynamodb);
const query = await ddb.send(new QueryCommand({
TableName: 'mock',
}));
`
#### Lib Storage Upload
To mock @aws-sdk/lib-storage Upload you need to mock all commands
used under the hood:
`typescript
import {S3Client, CreateMultipartUploadCommand, UploadPartCommand} from '@aws-sdk/client-s3';
import {Upload} from "@aws-sdk/lib-storage";
const s3Mock = mockClient(S3Client);
// for big files upload:
s3Mock.on(CreateMultipartUploadCommand).resolves({UploadId: '1'});
s3Mock.on(UploadPartCommand).resolves({ETag: '1'});
// for small files upload:
s3ClientMock.on(PutObjectCommand).callsFake(async (input, getClient) => {
getClient().config.endpoint = () => ({hostname: ""}) as any;
return {};
});
const s3Upload = new Upload({
client: new S3Client({}),
params: {
Bucket: 'mock',
Key: 'test',
Body: 'x'.repeat(6 1024 1024), // 6 MB
},
});
s3Upload.on('httpUploadProgress', (progress) => {
console.log(progress);
});
await s3Upload.done();
`
This way, the Upload#done() will complete successfuly.
To cause a failure, you need to specify the rejects() behavior@aws-sdk/lib-storage
for one of the AWS SDK Commands used by the .
For uploading a small file (under the defined multipart upload single part size),
lib-storage sends a PutObjectCommand. To make it fail:
`ts`
s3Mock.on(PutObjectCommand).rejects();
For bigger files, it makes a series of calls including CreateMultipartUploadCommand,UploadPartCommand, and CompleteMultipartUploadCommand. Making any of them fail will fail the upload:
`ts`
s3Mock.on(UploadPartCommand).rejects();
#### S3 GetObjectCommand
AWS SDK wraps the stream in the S3 GetObjectCommand result to provide utility methods to parse it.@smithy/util-stream
To mock it, you need to install the packagesdkStreamMixin()
and call the wrapping function on the stream you provide as the command output:
`ts
import {GetObjectCommand, S3Client} from '@aws-sdk/client-s3';
import {sdkStreamMixin} from '@smithy/util-stream';
import {mockClient} from 'aws-sdk-client-mock';
import {Readable} from 'stream';
import {createReadStream} from 'fs';
const s3Mock = mockClient(S3Client);
it('mocks get object', async () => {
// create Stream from string
const stream = new Readable();
stream.push('hello world');
stream.push(null); // end of stream
// alternatively: create Stream from file
// const stream = createReadStream('./test/data.txt');
// wrap the Stream with SDK mixin
const sdkStream = sdkStreamMixin(stream);
s3Mock.on(GetObjectCommand).resolves({Body: sdkStream});
const s3 = new S3Client({});
const getObjectResult = await s3.send(new GetObjectCommand({Bucket: '', Key: ''}));
const str = await getObjectResult.Body?.transformToString();
expect(str).toBe('hello world');
});
`
#### Paginated operations
To mock a paginated operation
results, simply mock the corresponding Command:
`typescript
import {DynamoDBClient, paginateQuery, QueryCommand} from '@aws-sdk/client-dynamodb';
import {marshall} from '@aws-sdk/util-dynamodb';
const dynamodbMock = mockClient(DynamoDBClient);
dynamodbMock.on(QueryCommand).resolves({
Items: [
marshall({pk: 'a', sk: 'b'}),
marshall({pk: 'c', sk: 'd'}),
],
});
const dynamodb = new DynamoDBClient({});
const paginator = paginateQuery({client: dynamodb}, {TableName: 'mock'});
const items = [];
for await (const page of paginator) {
items.push(...page.Items || []);
}
`
#### SDK v2-style mocks
The AWS SDK v3 gives an option to use it similarly to v2 SDK,
with command method call instead of send():
`typescript
import {SNS} from '@aws-sdk/client-sns';
const sns = new SNS({});
const result = await sns.publish({
TopicArn: 'arn:aws:sns:us-east-1:111111111111:MyTopic',
Message: 'My message',
});
`
Although this approach is not recommended by AWS,
those calls can be mocked in the standard way:
`typescript
import {PublishCommand, SNSClient} from '@aws-sdk/client-sns';
const snsMock = mockClient(SNSClient);
snsMock
.on(PublishCommand)
.resolves({
MessageId: '12345678-1111-2222-3333-111122223333',
});
`
Notice that in mocks you still need to use SNSClient, not SNS,Command
as well as classes.
Inspect received calls:
`typescript`
snsMock.calls(); // all received calls
snsMock.call(0); // first received call
Get calls of a specified command:
`typescript`
snsMock.commandCalls(PublishCommand)
Get calls of a specified command with given payload
(you can force strict matching by passing third param strict: true):
`typescript`
snsMock.commandCalls(PublishCommand, {Message: 'My message'})
Under the hood, the library uses Sinon.js stub.
You can get the stub instance to configure and use it directly:
`typescript`
const snsSendStub = snsMock.send;
The Client mock exposes three Sinon.js stub methods:reset(), resetHistory(), and restore().
The reset() method resets the mock state and behavior.on(...).resolves(...)
The Client will continue to be mocked, only now with a clean mock instance,
without any behavior (set with methods like ) and calls history.
**You should call clientMock.reset() before or after every testbeforeEach()
(using / beforeAll() from your test framework)
to keep tests independent from each other.**
The resetHistory() only clear mocked client calls historymockedClient.call(...)
that you access with and mockedClient.calls().
The behavior is preserved.
The restore() removes the mock altogether,client.send()
restoring the normal behavior of .
You can also pass custom Sinon Sandbox
with mockClient(client, { sandbox: mySandbox })
to manage all mocks lifecycle at once.
#### Jest
Custom Jest matchers simplify verification
that the mocked Client was called with given Commands.
Matchers are published as a separate package. Install it:
`bash`
npm install -D aws-sdk-client-mock-jest
Usage (notice the import):
`ts
import 'aws-sdk-client-mock-jest';
// a PublishCommand was sent to SNS
expect(snsMock).toHaveReceivedCommand(PublishCommand);
// at least one command was sent to SNS
expect(snsMock).toHaveReceivedAnyCommand();
// two PublishCommands were sent to SNS
expect(snsMock).toHaveReceivedCommandTimes(PublishCommand, 2);
// a PublishCommand with Message "hello world" was sent to SNS
expect(snsMock).toHaveReceivedCommandWith(
PublishCommand, {Message: 'hello world'}
);
// a PublishCommand with Message containing "hello" was sent to SNS
expect(snsMock).toHaveReceivedCommandWith(
PublishCommand, {Message: expect.stringContaining('hello')}
);
// the second command sent to SNS was a PublishCommand with Message "hello world"
expect(snsMock).toHaveReceivedNthCommandWith(
2, PublishCommand, {Message: 'hello world'}
);
// the second PublishCommand sent to SNS had Message "hello world"
expect(snsMock).toHaveReceivedNthSpecificCommandWith(
2, PublishCommand, {Message: 'hello world'}
);
`
Shorter aliases exist, like toReceiveCommandTimes().
#### Vitest
Use those matchers with Vitest:
`ts
import 'aws-sdk-client-mock-jest/vitest';
import { expect } from 'vitest';
// a PublishCommand was sent to SNS
expect(snsMock).toHaveReceivedCommand(PublishCommand);
`
To use the matchers outside of Jest, you can pull in the expect library separately
and add it to the global scope directly, e.g.:
`ts`
const {expect} = require("expect");
(globalThis as any).expect = expect;
require("aws-sdk-client-mock-jest");
See the full API Reference.
Example below uses Jest as a test framework, but mocks will work with any testing library.
Let's take a simple Lambda function that takes a list of messages,
sends them to SNS topic and returns message IDs:
`typescript
import {PublishCommand, SNSClient} from '@aws-sdk/client-sns';
const snsTopicArn = process.env.SNS_TOPIC_ARN || '';
const sns = new SNSClient({});
export const handler = async (event: Event): Promise
const promises = event.messages.map(async (msg, idx) => {
const publish = await sns.send(new PublishCommand({
TopicArn: snsTopicArn,
Message: msg,
}));
return publish.MessageId!;
});
return await Promise.all(promises);
};
interface Event {
messages: string[];
}
`
Then the tests could look like this:
`typescript
import {mockClient} from 'aws-sdk-client-mock';
import {PublishCommand, SNSClient} from '@aws-sdk/client-sns';
import {handler} from '../src';
const snsMock = mockClient(SNSClient);
/**
* To be sure that unit tests are independent from each other,
* reset mock behavior between the tests.
*/
beforeEach(() => {
snsMock.reset();
});
it('message IDs are returned', async () => {
snsMock.on(PublishCommand).resolves({
MessageId: '12345678-1111-2222-3333-111122223333',
});
const result = await handler({
messages: ['one', 'two', 'three']
});
expect(result).toHaveLength(3);
expect(result[0]).toBe('12345678-1111-2222-3333-111122223333');
});
it('SNS Client is called with PublishCommand', async () => {
snsMock.on(PublishCommand).resolves({
MessageId: '111-222-333',
});
await handler({
messages: ['qq', 'xx']
});
expect(snsMock).toHaveReceivedCommandTimes(PublishCommand, 2);
});
`
For more examples, see the unit tests.
> Note
> Those instructions refer to @smithy/types used by AWS SDK v3.363.0 and above.@aws-sdk/types
> For version below 3.363.0, perform the same steps for the package.
If you have multiple versions of @smithy/types installed in your project,
you can get type errors similar to this:
``
TS2345: Argument of type 'typeof DynamoDBDocumentClient' is not assignable to parameter of type 'InstanceOrClassType
Type 'typeof DynamoDBDocumentClient' is not assignable to type 'ClassType
The types of 'prototype.middlewareStack.concat' are incompatible between these types.
Type '
Types of parameters 'from' and 'from' are incompatible.
Property 'identify' is missing in type 'MiddlewareStack
Run npm ls @smithy/types / pnpm why @smithy/types / yarn why @smithy/types
and check if you have more than one version of the package installed.
To solve this, go through the steps until one works:
- make sure all your @aws-sdk/* packages point to the same version,@aws-sdk/*
- remove all packages from package.json, run npm install / pnpm install / yarn install,@aws-sdk/*
restore packages in package.json, and run install again,@smithy/types
- add to your dev dependencies in the latest version,@smithy/types
- force using single version with npm overrides,npm ls @smithy/types
pnpm overrides, or yarn resolutions,
- if nothing else helped, open an issue including the output of / pnpm why @smithy/types / yarn why @smithy/types.
If you need to explicitly type the mock variable,
you can use AwsClientStub type:
`ts
import {AwsClientStub, mockClient} from 'aws-sdk-client-mock'
import {S3Client} from "@aws-sdk/client-s3";
const mock: AwsClientStub
`
The AwsClientStub type works only with tsconfig optionstrictFunctionTypes=true or (strict=true) in tsconfig.json file.
See details in #167.
Wider Command matchers must be declared first,
otherwise, they will take precedence over previous ones.
In this case, all PublishCommand sends will return message ID 222:
`typescript`
snsMock
.on(PublishCommand, myInput).resolves({MessageId: '111'})
.on(PublishCommand).resolves({MessageId: '222'});
If the order of the declarations is switched, sends with input matching myInput111
will return ID and all others 222.
It works similarly with onAnyCommand().
When you create both a Client type mock
and a specific Client instance mock(s),
you need to declare type mock last.
Otherwise, the other instances will not be mocked.
Right now if you create a mock for the Client type,
and then mock a specific instance of this Client,
with the order of mocking as here:
`typescript
const sns1 = new SNSClient({}); // not mocked
mockClient(SNSClient).resolves({MessageId: '123'});
const sns2 = new SNSClient({}); // mocked
mockClient(sns2).resolves({MessageId: '456'});
const sns3 = new SNSClient({}); // not mocked
`
Declaring mocks in this order will fix it:
`typescript
const sns1 = new SNSClient({}); // mocked - default
const sns2 = new SNSClient({}); // mocked
mockClient(sns2).resolves({MessageId: '456'});
mockClient(SNSClient).resolves({MessageId: '123'});
const sns3 = new SNSClient({}); // mocked - default
`
PRs to fix this are welcome.
When testing with Mocha, call mockClient()beforeEach()` method, not in the global scope,
in the
to prevent overriding the mock between test files.
See this
for more details.