Testing Constructs (Optional) #
The CDK for Terraform Developer Guide has a good guide on testing constructs.
CDKTF assert Library #
We will be using the CDKTF Jest Matchers throughout this section. The library contains several helper functions for writing unit and integration tests.
More advanced assertion helpers are available in TerraConstructs and may be published for easier set up in the future.
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.