Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: jest-based "snapshot testing" in boilerplate #1533

Closed
mipearson opened this issue Jan 13, 2019 · 1 comment
Closed

Feature Request: jest-based "snapshot testing" in boilerplate #1533

mipearson opened this issue Jan 13, 2019 · 1 comment
Labels
feature-request A feature should be added or improved.

Comments

@mipearson
Copy link

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.

@rix0rrr rix0rrr added the feature-request A feature should be added or improved. label Jan 17, 2019
@eladb
Copy link
Contributor

eladb commented Jan 29, 2019

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

@eladb eladb closed this as completed Jan 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request A feature should be added or improved.
Projects
None yet
Development

No branches or pull requests

3 participants