Unit tests with AWS CDK

Β·

3 min read

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.

Create a project

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

With a SQS queue

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.

Conclusion

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 ! πŸ€–

Did you find this article valuable?

Support Sonia Manoubi by becoming a sponsor. Any amount is appreciated!