Unit tests with AWS CDK

I am a french Developer, working for a french startup :) i like video games, computers and drinking RicorΓ©
Search for a command to run...

I am a french Developer, working for a french startup :) i like video games, computers and drinking RicorΓ©
No comments yet. Be the first to comment.
AWS Bedrock AgentCore lets you deploy agents as managed containerized runtimes. This guide shows how to automate this deployment using Pulumi and Docker. What You'll Deploy Docker container with your agent code Amazon ECR to store the image Bedroc...

Introduction This guide shows how to deploy AWS Bedrock Agents using Pulumi and TypeScript. Prerequisites AWS Account with Bedrock access Node.js and npm installed Pulumi CLI installed Project Setup Generate Project with Pulumi pulumi new aws-t...

Starting My Homelab Journey with a Raspberry Pi 4B

I recently discovered Docker compose profiles and wanted to share it as it can be useful to someone else. Docker Compose profiles let you control which services run without modifying your docker-compose.yml. Theyβre useful for separating optional s...

These days, iβm using AWS Bedrock to create Agents.And i faced this error when testing my agents many times so i decided to write down the workaround that i found. An error occurred (dependencyFailedException) when calling the InvokeAgent operation: ...

Lately, I have been using the AWS CDK a lot to set up infrastructure on AWS, and the fact that they have integrated solutions to unit test the code is really great.
I will show how to use the CDK assertions (aws-cdk-lib/assertions) library to write unit tests about your stacks. The library (aws-cdk-lib/assertions) contains several helper functions for writing unit and integration tests.
mkdir unitTestCdk
cd unitTestCdk
cdk init app --language typescript
yarn add @aws-cdk/assert
The command generates an empty stack.
Go to lib/unit_test_cdk-stack.ts
import { Stack, StackProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';
export class UnitTestCdkStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
// The code that defines your stack goes here
ππππππππ
}
}
Let's write a test for that.
Go to test/unit_test_cdk.test.ts, you will already have the structure to write your first test. Let's write our test :
import * as cdk from 'aws-cdk-lib';
import '@aws-cdk/assert/jest';
import * as UnitTestCdk from '../lib/unit_test_cdk-stack';
test('Empty stack', () => {
const app = new cdk.App();
const stack = new UnitTestCdk.UnitTestCdkStack(app, 'MyTestStack');
expect(stack).not.toHaveResource('AWS::SQS::Queue');
});
Let's run our test
yarn test
$ jest
PASS test/unit_test_cdk.test.ts (7.442 s)
β Empty stack (25 ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 7.488 s
Ran all test suites.
β¨ Done in 8.23s.
Great ! Now let's add a resource , and update our test
import { Stack, StackProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as sqs from 'aws-cdk-lib/aws-sqs';
export class UnitTestCdkStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
// The code that defines your stack goes here
// example resource
const queue = new sqs.Queue(this, 'UnitTestCdkQueue', {
queueName: "sqs-test"
});
}
}
Let's update our test now
import * as cdk from 'aws-cdk-lib';
import '@aws-cdk/assert/jest';
import * as UnitTestCdk from '../lib/unit_test_cdk-stack';
test('stack with SQS queue', () => {
const app = new cdk.App();
const stack = new UnitTestCdk.UnitTestCdkStack(app, 'MyTestStack');
expect(stack).toHaveResource('AWS::SQS::Queue');
});
And run the test
yarn test
PASS test/unit_test_cdk.test.ts (10.276 s)
β stack with SQS queue (29 ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 10.346 s
Ran all test suites.
β¨ Done in 11.81s.
We can even test check the queue name :
import * as cdk from 'aws-cdk-lib';
import '@aws-cdk/assert/jest';
import * as UnitTestCdk from '../lib/unit_test_cdk-stack';
test('stack with SQS queue', () => {
const app = new cdk.App();
const stack = new UnitTestCdk.UnitTestCdkStack(app, 'MyTestStack');
expect(stack).toHaveResource('AWS::SQS::Queue', {
QueueName: 'sqs-test'
});
});
You can look further in the AWS Documentation to find helpers to test your stack the way you need it.
Now, you know how to unit test your infrastructure created with AWS CDK !
If you have any questions, feel free to ask in the comments, i'll do my best to answer !
See you next in the next one ! π€