Aws-cdk: Feature Request: jest-based "snapshot testing" in boilerplate

Created on 13 Jan 2019  Â·  1Comment  Â·  Source: aws/aws-cdk

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 the cdk-stack.ts file:

Ran:

npm i ts-jest jest @types/jest

Edited package.json, added:

  "jest": {
    "transform": {
      "^.+\\.tsx?$": "ts-jest"
    },
    "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json",
      "node"
    ],
    "globals": {
      "ts-jest": {
        "diagnostics": {
          "warnOnly": true
        }
      },
    "testEnvironment": "node"
    }
  }

And added "test": "jest --watch", to the scripts key.

Created a lib/cdk-stack.test.ts with the following contents:

import { CdkStack } from "./cdk-stack";
import cdk = require("@aws-cdk/cdk");

test("CdkStack matches snapshot", () => {
  const stack = new CdkStack(new cdk.App(), "CdkStack", {});
  expect(stack.toCloudFormation()).toMatchSnapshot();
});

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.

feature-request

Most helpful comment

Thanks for this.
Duplicate: #300 - I've pasted your description there as a reference implementation.

>All comments

Thanks for this.
Duplicate: #300 - I've pasted your description there as a reference implementation.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PaulMaddox picture PaulMaddox  Â·  3Comments

pepastach picture pepastach  Â·  3Comments

cybergoof picture cybergoof  Â·  3Comments

peterdeme picture peterdeme  Â·  3Comments

artyom-melnikov picture artyom-melnikov  Â·  3Comments