Open ncaq opened 2 years ago
The reason why I use the same App all the time is that in the actual production code, I create a stack of ECR repositories and reference the fields to the stack of ECS repositories. When I did this with a new App, I got a different error.
@ncaq did you ever figure this out? I've run into the same issue.
@amirmishani It has not been resolved. I am using the previous deprecated package for now.
Try creating both stacks first, and only after that create the Template objects and assertions. Somehow fixed things for me, still no idea why 🤷
When Template.toStack()
is called, it calls synth()
on the underlying App
instance.
synth()
caches the result of the synthesis (note the if (!this.assembly
on L182. (Note that App
extends Stage
.)
So when you reuse the App
instance and call Template.toStack()
multiple times, you'll only get the first result, which will not have any stacks that you've subsequently added. This behaviour could possibly change, but it's likely to regress test performance negatively when fromStack()
is called repeatedly.
The answer to this is:
App
instances across tests. This probably applies in the current case: there are two Stack
s, use separate tests with different App
instances.So in the current example, I suggest doing something like (comments optional):
import { App, Stack } from "aws-cdk-lib";
import { Template } from "aws-cdk-lib/assertions";
test("snapshot foo", () => {
// arrange
const app = new App();
// act
const fooStack = new Stack(app, "FooStack");
const fooTemplate = Template.fromStack(fooStack);
// assert
expect(fooTemplate.toJSON()).toMatchSnapshot("FooStack");
});
test("snapshot bar", () => {
const app = new App();
const barStack = new Stack(app, "BarStack");
const barTemplate = Template.fromStack(barStack);
expect(barTemplate.toJSON()).toMatchSnapshot("BarStack");
});
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
paste.
What did you expect to happen?
This is a snapshot test, so we expect it to succeed unconditionally the first time.
What actually happened?
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?