Testing CDK infrastructure code is an essential step in any CI/CD workflow. Because CDK stacks synthesize to CloudFormation templates, you can assert against that output using theDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/okruti-raghuraj/aws-1/llms.txt
Use this file to discover all available pages before exploring further.
aws-cdk-lib/assertions module — catching misconfigured resources before anything touches your AWS account. The aws-cdk-pipeline project comes pre-wired with Jest and ts-jest so you can write TypeScript tests with no extra configuration.
Test setup
The test dependencies are declared inpackage.json under devDependencies:
| Package | Version | Purpose |
|---|---|---|
jest | ^26.4.2 | Test runner and assertion framework |
ts-jest | ^26.2.0 | TypeScript preprocessor so Jest understands .ts files |
@types/jest | ^26.0.10 | TypeScript type definitions for Jest globals (test, expect, etc.) |
jest.config.js at the project root:
testEnvironment: 'node'— runs tests in a Node.js context (not a browser DOM).roots— Jest only looks for tests inside thetest/directory.testMatch— only files ending in.test.tsare treated as test suites.transform— any.tsor.tsxfile is transpiled byts-jeston the fly, so you never need a separate compile step before running tests.
Running tests
Use thetest script from package.json:
The existing test
The scaffolded test file lives attest/aws-cdk-pipeline.test.ts:
new cdk.App() → new Stack() → Template.fromStack(stack) → template.hasResourceProperties(...) — is the standard CDK unit-testing pattern. You uncomment the imports and the body, then swap the resource type and properties to match what your stack actually creates.
CDK tests are unit tests — they validate the synthesized CloudFormation JSON in memory. No AWS credentials are needed, and no real resources are created or billed. This makes them extremely fast and safe to run in any environment.
Writing a real stack test
TheAwsCdkPipelineStack defined in lib/aws-cdk-pipeline-stack.ts creates a CodePipeline construct named 'TestPipeline'. Here is a complete, runnable test that asserts the pipeline resource is present in the synthesized template:
new cdk.App()— creates a throwaway CDK app context for the test.new AwsCdkPipelineStack(app, 'TestStack')— instantiates the stack under test; CDK synthesizes it immediately.Template.fromStack(stack)— captures the synthesized CloudFormation template as an in-memoryTemplateobject.template.hasResourceProperties(...)— asserts that at least oneAWS::CodePipeline::Pipelineresource exists whose properties includeName: 'TestPipeline'. The test fails (with a descriptive diff) if no matching resource is found.
CDK assertions API
TheTemplate class from aws-cdk-lib/assertions provides several useful assertion methods:
| Method | Description |
|---|---|
hasResourceProperties(type, props) | Asserts that a resource of the given CloudFormation type exists with the specified property subset. |
resourceCountIs(type, count) | Asserts that exactly count resources of the given type exist in the template. |
hasOutput(logicalId, props) | Asserts that a CloudFormation output with the given logical ID and property subset is present. |
findResources(type, props?) | Returns a map of all resources matching the type and optional property filter — useful for debugging. |
toJSON() | Returns the full synthesized template as a plain JavaScript object for snapshot testing. |