You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've also set up "snapshot testing" via jest. This makes it easier to see the impacts of changes that I'm making to my stack without having to run cdk synth manually. It also means if I'm making a change that unexpectedly changes logical IDs or something else unexpected I'm told about it very quickly.
Snapshot testing isn't intended as "this works", but "this did/didn't change". It's also very, very easy to set up: unlike other forms of testing, it will autogenerate the expected output from the first run, and interactively ask you to confirm changes rather than assuming that they count as a "failure".
Given the boilerplate set up by cdk init sample-app, here's what I did to set up a very simple snapshot test around the cdk-stack.ts file:
Ran npm run test and was presented with the following output:
PASS lib/cdk-stack.test.ts
✓ CdkStack matches snapshot (47ms)
› 1 snapshot written.
Snapshot Summary
› 1 snapshot written from 1 test suite.
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 1 written, 1 total
Time: 2.968s
Ran all test suites related to changed files.
This then creates a lib/__snapshots__/cdk-stack.test.ts.snap file containing the synthesized stack, which I add to source control.
As I add things to the stack I expect that snapshot to "break" - jest will prompt me as to whether I want to update the snapshot or treat it as a failure. If I add a new sns.Topic(this, "AnotherTopic"); like to the cdk-stack.ts, I'll then receive this:
Received value does not match stored snapshot "CdkStack matches snapshot 1".
- Snapshot
+ Received
@@ -1,7 +1,10 @@
Object {
"Resources": Object {
+ "AnotherTopicC20D17AD": Object {
+ "Type": "AWS::SNS::Topic",
+ },
Pressing u updates the snapshot for me.
The text was updated successfully, but these errors were encountered:
First, see #1532
I've also set up "snapshot testing" via jest. This makes it easier to see the impacts of changes that I'm making to my stack without having to run
cdk synth
manually. It also means if I'm making a change that unexpectedly changes logical IDs or something else unexpected I'm told about it very quickly.Snapshot testing isn't intended as "this works", but "this did/didn't change". It's also very, very easy to set up: unlike other forms of testing, it will autogenerate the expected output from the first run, and interactively ask you to confirm changes rather than assuming that they count as a "failure".
Given the boilerplate set up by
cdk init sample-app
, here's what I did to set up a very simple snapshot test around thecdk-stack.ts
file:Ran:
Edited
package.json
, added:And added
"test": "jest --watch",
to thescripts
key.Created a
lib/cdk-stack.test.ts
with the following contents:Ran
npm run test
and was presented with the following output:This then creates a
lib/__snapshots__/cdk-stack.test.ts.snap
file containing the synthesized stack, which I add to source control.As I add things to the stack I expect that snapshot to "break" - jest will prompt me as to whether I want to update the snapshot or treat it as a failure. If I add a
new sns.Topic(this, "AnotherTopic");
like to thecdk-stack.ts
, I'll then receive this:Pressing
u
updates the snapshot for me.The text was updated successfully, but these errors were encountered: