typescript-cdk-testing

Write snapshot and fine-grained assertion tests for AWS CDK infrastructure with Vitest.

138|5|Updated Sep 19, 2024
One-click install
npx skills add https://github.com/macalbert/envilder --skill typescript-cdk-testing-macalbert
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: typescript-cdk-testing
Source: https://github.com/macalbert/envilder/tree/main/.github/skills/typescript-cdk-testing
Command: npx skills add https://github.com/macalbert/envilder --skill typescript-cdk-testing-macalbert

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing reliable tests for AWS CDK infrastructure is tricky: synthesized CloudFormation templates contain non-deterministic values like asset hashes, and teams struggle to choose between snapshot tests and explicit property assertions, leading to flaky tests or missed security-critical misconfigurations. ## Core Features & Use Cases - Snapshot Testing with Normalization: Capture full stack baselines with Template.fromStack() while normalizing non-deterministic values like S3Key and SourceObjectKeys to prevent false failures. - Fine-Grained Assertions: Verify specific resource properties using hasResourceProperties, resourceCountIs, and Match helpers for security-critical settings like encryption, IAM, and dead-letter queues. - Decision Guidance: Clear rules for when to use snapshots versus assertions, plus naming conventions and anti-patterns to avoid. - Use Case: When adding a new SQS stack with a dead-letter queue, generate a snapshot test for the overall template plus an explicit assertion verifying RedrivePolicy maxReceiveCount, then update snapshots safely after intentional changes. ## Quick Start Ask the AI to write CDK tests for a new stack following the snapshot and assertion patterns in this skill.

Frequently Asked Questions about typescript-cdk-testing

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write snapshot tests for AWS CDK stacks?

Synthesize the stack with Template.fromStack() from aws-cdk-lib/assertions and call expect(template).toMatchSnapshot(). Normalize non-deterministic values like Lambda S3Key and asset hashes before snapshotting to avoid false failures.

When should I use snapshot tests vs fine-grained assertions in CDK?

Use snapshots for new stacks and refactors to capture full baselines and detect drift. Use explicit assertions like hasResourceProperties for security-critical properties such as encryption, IAM rules, and resource counts that must never be missed.

Why do my CDK snapshot tests fail without code changes?

Failures usually come from non-deterministic values like Lambda deployment artifact hashes in S3Key or SourceObjectKeys. Normalize these properties to fixed placeholders in the template JSON before snapshotting.

How do I update CDK snapshots after intentional infrastructure changes?

Run the test suite with the -u flag, for example pnpm test -- -u. Always review the snapshot diff carefully before committing, and never update snapshots just to suppress unexplained failures.

What assertion methods does aws-cdk-lib/assertions provide?

Template provides hasResourceProperties, hasResource, resourceCountIs, and hasOutput. The Match helper offers objectLike, arrayWith, stringLikeRegexp, not, and absent for flexible partial matching on resource properties.