CDK for Terraform 0.6 Adds Unit Testing

The latest release of CDK for Terraform includes TypeScript unit testing via Jest, improved handling of native Terraform functions, and Bash and zsh shell completion.

We are excited to announce the release of Cloud Development Kit (CDK) for HashiCorp Terraform 0.6. With CDK for Terraform, you can write Terraform configurations in your choice of C#, Python, TypeScript, or Java (Go experimental), and still benefit from the full ecosystem of Terraform providers and modules. CDK for Terraform 0.6 adds an opinionated unit-testing workflow for testing your infrastructure code.

Key improvements in the CDK for Terraform 0.6 include:

  • Unit testing: Jest-based unit testing
  • Terraform functions: A wrapper library for using Terraform functions to calculate values based on data sources and other runtime values
  • Bug fixes: Hide sensitive output
  • Shell completions: Bash and zsh shell completions for cdktf-cli

»Jest for Snapshot Tests and Assertions

CDK for Terraform provides a workflow where writing infrastructure as code can feel like writing software. Since testing is a key part of writing high quality software, this release adds support for unit-testing TypeScript infrastructure code using Jest to do snapshot tests and assertions. Support for testing in other languages will be added in future releases.

»Snapshot Tests

Snapshot tests compare the synthesized Terraform configuration to a previously saved “snapshot” version. During refactoring, this provides a high level of confidence that no unintended changes were introduced. Snapshot tests run very quickly, and provide a high level of test coverage with a few test cases.

import { Testing } from "cdktf";import { Image, Container } from "../.gen/providers/docker";import MyApplicationsAbstraction from "../app"; describe("Unit testing using snapshots", () => {  it("Tests a custom abstraction", () => {    expect(      Testing.synthScope((stack) => {        const app = new MyApplicationsAbstraction(scope, "my-app", {});        app.addEndpoint("127.0.0.1");      })    ).toMatchSnapshot();  });});

»Assertions

Assertions allow fine-grained testing of your generated Terraform configurations. For example, the following tests first confirm that the generated code has a Container resource at all, then validates that the container image is ubuntu:latest:

import { Testing } from "cdktf";import { Image, Container } from "../.gen/providers/docker";import MyApplicationsAbstraction from "../app"; describe("Unit testing using assertions", () => {  it("should contain a container", () => {    expect(      Testing.synthScope((scope) => {        new MyApplicationsAbstraction(scope, "my-app", {});      })    ).toHaveResource(Container);  });   it("should use an ubuntu image", () => {    expect(      Testing.synthScope((scope) => {        new MyApplicationsAbstraction(scope, "my-app", {});      })    ).toHaveResourceWithProperties(Image, { name: "ubuntu:latest" });  });});

Finally, you can automate validation that the resulting code plans successfully:

it("check if this can be planned", () => {  const app = Testing.app();  const stack = new TerraformStack(app, "test");   const app = new MyApplicationsAbstraction(stack, "my-app", {});  app.addEndpoint("127.0.0.1");   expect(Testing.fullSynth(app)).toPlanSuccessfully();});

Internally, the testing is powered by Jest, a popular JavaScript testing framework. Jest automates snapshot management and should be familiar to most TypeScript developers.

For more details on how to test your infrastructure code, see the CDK for Terraform Testing documentation on GitHub.

»Other Improvements

CDK for Terraform 0.6 also improves handling of native Terraform functions, adds Bash and zsh shell completion, and includes a variety of other improvements and bug fixes. See the function documentation for details on using Terraform functions. The CDK for Terraform changelog contains a comprehensive list of enhancements and bug fixes included in this release.

Please note that CDK for Terraform 0.6 contains breaking changes that will require code updates if you use Aspects, construct.node.addInfo(), construct.node.addWarning(), construct.node.Error(), construct.onPrepare(), or construct.prepare(). Please see the CHANGELOG and 0.6 Upgrade Guide for details.

»Try CDK for Terraform

If you’re new to the project, the tutorials for CDK for Terraform on HashiCorp Learn are the best way to get started. We also have language-specific guides and documentation in our GitHub repository.

Check out the recording of our Terraform Community Office Hours for a demo and detailed explanation of the new testing tools.

Whether you’re experimenting or actively using CDK for Terraform, we’d love to hear from you. We’re particularly looking for feedback on the new testing functionality — please file any bugs you encounter, let us know about your feature requests (for example, if you’d find Python testing useful), and share other questions, thoughts, and experiences in the CDK for Terraform discussion forum.


Sign up for the latest HashiCorp news

By submitting this form, you acknowledge and agree that HashiCorp will process your personal information in accordance with the Privacy Policy.