Documentation: AWS-CDK custom constructs unit and integration testing best-practices and examples
Please provide some information/best-practices/examples for AWS-CDK custom constructs unit and integration testing
What is the proper way to unit-test and integration-test my newly built constructs?
What should be actually tested? How should it be tested?
How does the construct.validate method should be properly used and what should be validated
https://stackoverflow.com/questions/53566862/aws-cdk-unit-testing-and-integration-testing
Hey @deromka, answered on Stack Overflow, but pasting here for posterity:
As far as unit tests are concerned, we've got a library (currently only in TypeScript) to make assertions against synthesized CloudFormation templates. It's called @aws-cdk/assert. So what we usually do in unit tests is define a stack, add our construct, interact with it and then make assertions against the synthesized template using the assertion library.
Here's a sketch:
import { expect, haveResource } from '@aws-cdk/assert';
import cdk = require('@aws-cdk/cdk');
const stack = new cdk.Stack();
const myConstruct = new MySpecialBucket(stack, 'Test');
myConstruct.doSomething();
expect(stack).to(haveResource('AWS::S3::Bucket', {
Prop: 1234
});
You can find many examples in the AWS CDK GitHub repository (look for "test" directories).
Integration tests are a bit more tricky. What we've been doing is basically write little CDK apps as integration tests (for example) and compare the result of cdk synth to a checked-in expectation file. If they differ, we fail the build and request that the user manually deploy the app and update the checked-in file. This approach stems from the assumption that if the CFN template did not change, the resulting behavior would not change. That assumption has so far held quite well.
Hope that helps.
Related: #860, #300
Added this to the CDK Guide project. https://github.com/awsdocs/aws-cdk-guide/issues/98
This can be closed.