(assertions): Can't make templates from stacks with cross-stack references
- Dominant language
- TypeScript
- Stars
- 12.9k
- Forks
- 4.6k
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 83
Description
### Describe the bug
I try to test a stack that receives resources from other stacks as props.
I received an error when generating a template from a stack and couldn't make its snapshots. It seems to be derived from cross-stack references.
### Expected Behavior
Snapshots of a stack with cross-stack references are generated successfully.
### Current Behavior
```sh
% npm run test -- -u
> cross-stack-snapshot-test@0.1.0 test
> jest -u
FAIL test/cross-stack-snapshot-test.test.ts (9.66 s)
✕ Cross stack snapshot test (75 ms)
● Cross stack snapshot test
Unable to calculate a unique id for an empty set of components
12 | const stack = new SampleInstanceStack(app, 'MyTestStack', { vpc });
13 |
> 14 | const template = Template.fromStack(stack);
| ^
15 | expect(template).toMatchSnapshot();
16 | });
17 |
at Object.makeUniqueId (node_modules/aws-cdk-lib/core/lib/private/uniqueid.js:1:400)
at Function.uniqueId (node_modules/aws-cdk-lib/core/lib/names.js:1:626)
at SampleInstanceStack._addAssemblyDependency (node_modules/aws-cdk-lib/core/lib/stack.js:1:9885)
at operateOnDependency (node_modules/aws-cdk-lib/core/lib/deps.js:1:1657)
at Object.addDependency (node_modules/aws-cdk-lib/core/lib/deps.js:1:341)
at SampleInstanceStack.addDependency (node_modules/aws-cdk-lib/core/lib/stack.js:1:6829)
at resolveValue (node_modules/aws-cdk-lib/core/lib/private/refs.js:1:3243)
at Object.resolveReferences (node_modules/aws-cdk-lib/core/lib/private/refs.js:1:890)
at Object.prepareApp (node_modules/aws-cdk-lib/core/lib/private/prepare-app.js:1:585)
at Object.synthesize (node_modules/aws-cdk-lib/core/lib/private/synthesis.js:1:626)
at App.synth (node_modules/aws-cdk-lib/core/lib/stage.js:1:1942)
at toTemplate (node_modules/aws-cdk-lib/assertions/lib/template.js:2:305)
at Function.fromStack (node_modules/aws-cdk-lib/assertions/lib/template.js:1:1157)
at Object. (test/cross-stack-snapshot-test.test.ts:14:29)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 9.886 s
Ran all test suites.
```
### Reproduction Steps
Add a test: `test/sample-stack.test.ts`.
```typescript
import * as cdk from 'aws-cdk-lib';
import { Template } from 'aws-cdk-lib/assertions';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import { SampleInstanceStack } from '../lib/sample-stack';
test('Cross stack snapshot test', () => {
const app = new cdk.App();
const mockStack = new cdk.Stack(app);
const vpc = new ec2.Vpc(mockStack, 'Vpc');
const stack = new SampleInstanceStack(app, 'MyTestStack', { vpc });
const template = Template.fromStack(stack);
expect(template).toMatchSnapshot();
});
```
Define a stack: `lib/cross-stack-snapshot-test.test.ts`.
```typescript
import * as cdk from "aws-cdk-lib";
import * as ec2 from "aws-cdk-lib/aws-ec2";
import { Construct } from "constructs";
interface SampleInstanceStackProps extends cdk.StackProps {
readonly vpc: ec2.Vpc;
}
export class SampleInstanceStack extends cdk.Stack {
constructor(scope: Construct, id: string, props: SampleInstanceStackProps) {
super(scope, id, props);
const { vpc } = props;
new ec2.Instance(this, 'Instance', {
vpc,
instanceType: new ec2.InstanceType('t3.small'),
machineImage: new ec2.AmazonLinuxImage({ generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2 }),
});
}
}
```
Then, run tests: `npm run test -- -u`.
### Possible Solution
_No response_
### Additional Information/Context
_No response_
### CDK CLI Version
2.61.1 (build d319d9c)
### Framework Version
_No response_
### Node.js Version
v16.16.0
### OS
MacOS Monterey version 12.6.2 (21G320)
### Language
Typescript
### Language Version
_No response_
### Other information
Related issues:
* [(assertions): Can't do snapshot testing of Stacks using the same App](https://github.com/aws/aws-cdk/issues/18847)
Contributor guide
Research direction
Start with the reproduction in test/sample-stack.test.ts and the referenced lib/cross-stack-snapshot-test.test.ts, then run npm run test -- -u. Trace Template.fromStack(stack) through the cross-stack reference and dependency resolution shown in the failure. Done means the snapshot for a stack receiving a VPC from another stack is generated successfully.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- testing
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100