aws / aws/aws-cdk

assertions: Snapshot testing for Nested Stacks currently not supported

Open
#24,445 7 comments 11 reactions 0 assignees View on GitHub
@aws-cdk/assertions effort/medium feature-request p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the feature

While doing snapshot testing for stacks, NestedStack does not get synth'd in the snapshot. Instead, we just see a pointer. We would like to render the root stack and all nested stacks for the snapshot tests.

### Use Case

Nested stack template does not get synthesized locally so the snapshot test does not work on it. Instead of locally synthesizing it, it's uploaded to CDK staging bucket:

From https://docs.aws.amazon.com/cdk/v2/guide/stacks.html#stack_nesting
At synthesis time, the nested stack is synthesized to its own AWS CloudFormation template, which is uploaded to the AWS CDK staging bucket at deployment. Nested stacks are bound to their parent stack and are not treated as independent deployment artifacts. They aren't listed by cdk list, and they can't be deployed by cdk deploy.

If I use Template.fromStack(myNestedStack), then I get something like Nested stacks must be defined within scope of another non-nested stack

FAIL test/nested_stack.test.ts
✕ Topic is created (4 ms)

● Topic is created

Nested stacks must be defined within scope of another non-nested stack

7 |
8 | constructor(scope: Construct, id: string, props?: NestedStackProps) {
> 9 | super(scope, id, props);
| ^
10 |
11 | const myCdkNestedTopic = new sns.Topic(this, 'myCdkNestedTopic');
12 |

at findParentStack (node_modules/aws-cdk-lib/core/lib/nested-stack.js:1:4089)
at new NestedStack (node_modules/aws-cdk-lib/core/lib/nested-stack.js:1:1091)
at new MyNestedStack (lib/my-nested-stack.ts:9:7)
at Object. (test/nested_stack.test.ts:13:18)

Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 3.455 s, estimated 4 s
Ran all test suites.

### Proposed Solution

describe("DeadLetterQueue", () => {
test("matches the snapshot", () => {
const stack = new cdk.Stack();
new DeadLetterQueue(stack, "DeadLetterQueue");

const template = Template.fromStack(stack);
expect(template.toJSON()).toMatchSnapshot();
});
});

In an example such as above, Template.fromStack should be able to take a NestedStack construct as an input.

### Other Information

_No response_

### Acknowledgements

- [ ] I may be able to implement this feature request
- [ ] This feature might incur a breaking change

### CDK version used

2.65.0

### Environment details (OS name and version, etc.)

Any

Contributor guide

Open the contributing guide

Research direction

Start with Template.fromStack and the failing test in test/nested_stack.test.ts; the example also shows the nested construct in lib/my-nested-stack.ts. The change is complete when a snapshot test can accept a NestedStack and includes both the root stack and its nested stack template rather than only a pointer.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
cloud, infrastructure
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.