Skip to main content

Documentation 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.

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 the 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 in package.json under devDependencies:
{
  "devDependencies": {
    "jest": "^26.4.2",
    "ts-jest": "^26.2.0",
    "@types/jest": "^26.0.10"
  }
}
PackageVersionPurpose
jest^26.4.2Test runner and assertion framework
ts-jest^26.2.0TypeScript preprocessor so Jest understands .ts files
@types/jest^26.0.10TypeScript type definitions for Jest globals (test, expect, etc.)
Jest is configured in jest.config.js at the project root:
module.exports = {
  testEnvironment: 'node',
  roots: ['<rootDir>/test'],
  testMatch: ['**/*.test.ts'],
  transform: {
    '^.+\.tsx?$': 'ts-jest'
  }
};
  • testEnvironment: 'node' — runs tests in a Node.js context (not a browser DOM).
  • roots — Jest only looks for tests inside the test/ directory.
  • testMatch — only files ending in .test.ts are treated as test suites.
  • transform — any .ts or .tsx file is transpiled by ts-jest on the fly, so you never need a separate compile step before running tests.

Running tests

Use the test script from package.json:
npm run test
Or invoke Jest directly:
npx jest
Expected output when all tests pass:
PASS  test/aws-cdk-pipeline.test.ts
 SQS Queue Created (2 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        3.241 s
Ran all test suites.
Run npm run watch in one terminal to keep TypeScript compiling on every save, and npm run test in a second terminal to re-run tests immediately. Together they provide a tight feedback loop during development.

The existing test

The scaffolded test file lives at test/aws-cdk-pipeline.test.ts:
// import * as cdk from 'aws-cdk-lib';
// import { Template } from 'aws-cdk-lib/assertions';
// import * as AwsCdkPipeline from '../lib/aws-cdk-pipeline-stack';

// example test. To run these tests, uncomment this file along with the
// example resource in lib/aws-cdk-pipeline-stack.ts
test('SQS Queue Created', () => {
//   const app = new cdk.App();
//     // WHEN
//   const stack = new AwsCdkPipeline.AwsCdkPipelineStack(app, 'MyTestStack');
//     // THEN
//   const template = Template.fromStack(stack);

//   template.hasResourceProperties('AWS::SQS::Queue', {
//     VisibilityTimeout: 300
//   });
});
The test body is completely commented out, so it passes trivially as a shell. The pattern it demonstrates — 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

The AwsCdkPipelineStack 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:
import * as cdk from 'aws-cdk-lib';
import { Template } from 'aws-cdk-lib/assertions';
import * as AwsCdkPipeline from '../lib/aws-cdk-pipeline-stack';

test('Pipeline stack creates CodePipeline', () => {
  const app = new cdk.App();
  const stack = new AwsCdkPipeline.AwsCdkPipelineStack(app, 'TestStack');
  const template = Template.fromStack(stack);
  template.hasResourceProperties('AWS::CodePipeline::Pipeline', {
    Name: 'TestPipeline'
  });
});
Walk-through of each line:
  1. new cdk.App() — creates a throwaway CDK app context for the test.
  2. new AwsCdkPipelineStack(app, 'TestStack') — instantiates the stack under test; CDK synthesizes it immediately.
  3. Template.fromStack(stack) — captures the synthesized CloudFormation template as an in-memory Template object.
  4. template.hasResourceProperties(...) — asserts that at least one AWS::CodePipeline::Pipeline resource exists whose properties include Name: 'TestPipeline'. The test fails (with a descriptive diff) if no matching resource is found.

CDK assertions API

The Template class from aws-cdk-lib/assertions provides several useful assertion methods:
MethodDescription
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.

Build docs developers (and LLMs) love