Snapshot Testing AWS CDK Constructs

Lab Steps

lock
Working with Jest and AWS CDK Constructs
lock
Comparing CloudFormation Templates with Snapshot Tests

The hands-on lab is part of this learning path

Ready for the real environment experience?

DifficultyIntermediate
Time Limit30m
Students10
Ratings
5/5
starstarstarstarstar

Description

The AWS Cloud Development Kit can provide infrastructure engineers with the same predictability and repeatability seen in application code development. One way the AWS CDK achieves this is by incorporating the use of construct assertions with the popular Jest JavaScript testing framework. The AWS CDK Assert library contains helpers for writing tests to evaluate the CloudFormation templates that are generated by the CDK project. These testing modules, paired with the practical testing capabilities provided by Jest, allow developers to implement various types of tests including snapshot tests.

A CDK snapshot test begins by rendering an AWS CloudFormation template in JSON to be used as a reference snapshot. Whenever a snapshot test runs, a new template is generated and compared with the initial reference snapshot. If they do not match the test will fail, which can mean one of two things:

  • An update was made to the construct definition that caused an unexpected change in the resulting template
  • The initial reference snapshot needs to be updated to reflect the current version of the construct.

This type of testing is useful as you write your own CDK constructs and allows you to define expectations early on in development, and maintain those requirements as the application grows. 

In this lab, you will discover how the Jest framework works within a CDK project and learn how to implement snapshot tests.

Learning Objectives

Upon completion of this intermediate-level lab, you will be able to:

  • Run and evaluate test results generated with the Jest framework
  • Write snapshot tests to compare stack templates against a baseline

Intended Audience

  • Cloud Architects
  • DevOps Engineers
  • Software Engineers

Prerequisites

Familiarity with the following will be beneficial:

  • AWS Cloud Development Kit
  • TypeScript Programming
  • JavaScript Object Notation (JSON)

The following labs can be used to fulfill the prerequisite:

Environment before
PREVIEW
arrow_forward
Environment after
PREVIEW
About the Author
Students12750
Labs36
Learning paths1

Jun is a Cloud Labs Developer with previous experience as a Software Engineer and Cloud Developer. He holds the AWS Certified Solutions Architect and DevOps Engineer Professional certifications. He also holds the AWS Certified Solutions Architect, Developer, and SysOps Administrator Associate certifications. 

Jun is focused on giving back to the growing cloud community by sharing his knowledge and experience with students and creating engaging content.