Skip to content

(assertions): Can't do snapshot testing of Stacks using the same App #18847

Open
@ncaq

Description

@ncaq

What is the problem?

I migrate AWS CDK v1 to v2.
And I use assertion from assert because assert is deprecated.

My project have snapshot test.
When I rewrote this into assertions, I received an error that was difficult to resolve.
I thought that the complicated configuration I had done so far was bad, but it was reproduced when I created a new AWS CDK project and wrote test code.

Reproduction Steps

mkdir foo
cd foo
cdk init app --language typescript
emacsclient test/snapshot.test.ts

paste.

import { App, Stack } from "aws-cdk-lib";
import { Template } from "aws-cdk-lib/assertions";

test("snapshot", () => {
  const app = new App();

  const fooStack = new Stack(app, "FooStack");
  const fooTemplate = Template.fromStack(fooStack);
  expect(fooTemplate.toJSON()).toMatchSnapshot("FooStack");

  const barStack = new Stack(app, "BarStack");
  const barTemplate = Template.fromStack(barStack);
  expect(barTemplate.toJSON()).toMatchSnapshot("BarStack");
});
npm run test

What did you expect to happen?

This is a snapshot test, so we expect it to succeed unconditionally the first time.

What actually happened?

> [email protected] test
> jest

 PASS  test/foo.test.ts
 FAIL  test/snapshot.test.ts (7.841 s)
  ● snapshot

    Unable to find artifact with id "BarStack"

      10 |
      11 |   const barStack = new Stack(app, "BarStack");
    > 12 |   const barTemplate = Template.fromStack(barStack);
         |                                ^
      13 |   expect(barTemplate.toJSON()).toMatchSnapshot("BarStack");
      14 | });
      15 |

      at CloudAssembly.getStackArtifact (node_modules/aws-cdk-lib/cx-api/lib/cloud-assembly.ts:79:31)
      at toTemplate (node_modules/aws-cdk-lib/assertions/lib/template.ts:150:19)
      at Function.fromStack (node_modules/aws-cdk-lib/assertions/lib/template.ts:28:17)
      at Object.<anonymous> (test/snapshot.test.ts:12:32)

 › 1 snapshot written.
Snapshot Summary
 › 1 snapshot written from 1 test suite.

Test Suites: 1 failed, 1 passed, 2 total
Tests:       1 failed, 1 passed, 2 total
Snapshots:   1 written, 1 total
Time:        8.175 s
Ran all test suites.

CDK CLI Version

2.10.0 (build e5b301f)

Framework Version

No response

Node.js Version

v16.13.1

OS

Linux strawberry 5.15.19-gentoo #1 SMP Sat Feb 5 10:10:29 JST 2022 x86_64 AMD Ryzen Threadripper 1950X 16-Core Processor AuthenticAMD GNU/Linux

Language

Typescript

Language Version

3.9.7

Other information

relation?

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 effortp3

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions