Skip to content
Testing Constructs

Testing Constructs

Testing Constructs (Optional)

The CDKTN Developer Guide has a good guide on testing constructs.

CDKTN assert Library

We will be using the CDKTN Jest Matchers throughout this section. The library contains several helper functions for writing unit and integration tests.

For this workshop we will mostly be using the toHaveResource and toHaveResourceWithProperties Jest matchers. This helper is used when you only care that a resource of a particular type exists (regardless of its logical identfier), and that some properties are set to specific values.

Example:

// NOTE: terraconstructs requires stack.prepareStack()
// due to many of its late binding features.
stack.prepareStack();
const template = Testing.synth(stack, runValidations)
expect(template).toHaveResourceWithProperties(instance.Instance, {
    ami: "ami-xxxxx",
    // Note: some properties omitted here
});

expect(template).not.toHaveResourceWithProperties(instance.Instance, {
    availability_zone: expect.anything(), // should not have this property set
});

To see the rest of the documentation, please read the docs here.

Edit this page