Skip to content

aws-cdk-lib/assertions: Unable to find artifact with id when creating two instances of the same Stack in Jest test #24689

@kadishmal

Description

@kadishmal

Describe the bug

I'm migrating from CDK v1 to CDK v2. Have fixed all the syntax issues and now fixing unit tests.

In V1 tests I have the following code which I migrated to v2:

describe('Website Stack', () => {
  const app = new App();
  const envOregon = { region: 'us-west-2' };

  const stages = [
    {
      stage: STAGE.PROD,
    },
    {
      stage: STAGE.BETA,
    },
  ];

  stages.forEach(({ stage }) => {
    const stack = new WebsiteStack(app, `${stage}WebsiteStack`, {
      env: envOregon,
      subdomain: stage,
    });

    // Here it fails with `Unable to find artifact with id`
    const template = Template.fromStack(stack);

It fails to find the the second instance on the last line with Template.fromStack(). It always can't find the second stack even when I change the order of the stack definition.

Expected Behavior

Previously in V1 the test used to work by finding the second stack as well.

Current Behavior

Internally I see that Template.fromStack() calls the following code:

return new Template(toTemplate(stack), templateParsingOptions);

... which in turn calls:

return stack.nestedStackParent
    ? JSON.parse(fs.readFileSync(path.join(assembly.directory, stack.templateFile)).toString('utf-8'))
    : assembly.getStackArtifact(stack.artifactId).template;

In the above code fs.readFileSync(path.join(assembly.directory, stack.templateFile)) fails because for some reason the test does not compile/synthesize the second instance. In the assembly.directory I only see the first instance, whichever is the first.

$ ls -lat "/private/var/folders/l7/p9ksylw17x34lc5_xstwndmr0000gp/T/cdk.out4nhobd"
cdk.out
manifest.json
tree.json
WebsiteStack.assets.json
WebsiteStack.template.json

The directory has no template for the BetaWebsiteStack.

Reproduction Steps

  • Followed the migration guide to move to CDK v2.
  • All syntax errors are fixed.
  • All deprecated constructs are migrated.
  • Run npm test in CDK v2 project.

Possible Solution

I managed to fix the test by creating a separate instance of the App for each stack as follows:

describe('Website Stack', () => {
  const envOregon = { region: 'us-west-2' };

  const stages = [
    {
      stage: STAGE.PROD,
    },
    {
      stage: STAGE.BETA,
    },
  ];

  stages.forEach(({ stage }) => {
    const app = new App();

    const stack = new WebsiteStack(app, `${stage}WebsiteStack`, {
      env: envOregon,
      subdomain: stage,
    });

    // Here it fails with `Unable to find artifact with id`
    const template = Template.fromStack(stack);

Additional Information/Context

No response

CDK CLI Version

2.69.0 (build 60a5b2a)

Framework Version

2.69.0

Node.js Version

v16.15.0

OS

macOS 13.2.1

Language

Typescript

Language Version

5.0.2

Other information

No response

Metadata

Metadata

Assignees

Labels

@aws-cdk/assertionsRelated to the @aws-cdk/assertv2 packagebugThis issue is a bug.effort/mediumMedium work item – several days of effortp1

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions