Skip to content

(assertions): Can't make templates from stacks with cross-stack references #23814

Open
@ymhiroki

Description

@ymhiroki

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

% npm run test -- -u

> [email protected] 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.<anonymous> (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.

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.

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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    @aws-cdk/assertionsRelated to the @aws-cdk/assertv2 packagebugThis issue is a bug.effort/mediumMedium work item – several days of effortfeature-requestA feature should be added or improved.p2

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions