Writing unit checks for code that interacts with the AWS JavaScript SDK v3 comes with two main advantages. Clearly, writing unit checks ensures you catch bugs early and due to this fact enhance the standard of your code. Additionally, writing unit checks lets you run your code domestically with out the necessity to attain out to the AWS service APIs. However how do you write unit checks for code interacting with the AWS JavaScript SDK v3?
Within the following, I’ll share my learnings from writing unit checks by utilizing aws-sdk-client-mock by Maciej Radzikowski.
aws-sdk-client-mock is straightforward to make use of!
Let’s begin with a easy instance.
The next code snippet reveals the index.js
file containing a handler()
operate, which may very well be deployed as a Lambda operate. The handler()
operate lists all buckets belonging to an AWS account.
import { S3Client, ListBucketsCommand } from '@aws-sdk/client-s3'; |
So, how do I write a unit take a look at? I desire utilizing the take a look at framework mocha to put in writing JavaScript checks. The next snippet reveals the take a look at/take a look at.index.js
file containing the skeleton to implement a take a look at.
import { deepStrictEqual } from 'node:assert'; |
When executing the take a look at, the handler()
operate will ship a request to the S3 API. However doing so shouldn’t be possible for unit testing, as it is vitally difficult to make sure the response matches the assumptions within the unit take a look at.
As an alternative of sending requests to the AWS APIs use a typical testing method known as mocking. A mock simulates a dependency. So let’s mock the AWS Java Script SDK v3 by extending the take a look at/take a look at.index.js
file.
import { S3Client, ListBucketsCommand } from '@aws-sdk/client-s3'; |
Need to run the instance your self? Right here is the bundle.json
that it’s essential to setup the instance.
{ |
Lastly, run the take a look at.
The testing framework outputs the next outcomes.
> aws-mock-demo@1.0.0 take a look at |
Subsequent, let me share a some classes discovered.
Making a mock with aws-sdk-client-mock
It took me a bit of bit to grasp that there are two methods to create a mock.
The next code creates a mock for a given shopper occasion.
const s3Client = new S3Client({}); |
Nevertheless, in lots of eventualities, you don’t have entry to the AWS SDK shopper cases. In these eventualities, right here is the way you globally mock a shopper.
const s3Mock = mockClient(S3Client); |
The AWS JavaScript SDK v3 comes with built-in paginators. The next snippet reveals methods to web page via all gadgets saved in a DynamoDB desk.
import { DynamoDBClient, paginateScan } from '@aws-sdk/client-dynamodb'; |
To put in writing a unit take a look at override the underlying command, ScanCommand
on this instance.
import { DynamoDBClient, ScanCommand } from '@aws-sdk/client-dynamodb'; |
Mocks vs. real-world
The tough half when writing mocks for the AWS SDK is to make sure comatiblity with the real-world. That’s why I don’t depend on unit testing. On prime of that, integration testing towards the AWS APIs is critical.
Abstract
aws-sdk-client-mock is a helpful device in relation to writing unit checks for code that interacts with the AWS JavaScript SDK v3. It has by no means been simpler to put in writing unit checks!