Aws-cdk: Confusion around jest expect vs aws-cdk assert expect function in snapshot testing

Created on 21 May 2020  ·  5Comments  ·  Source: aws/aws-cdk

Regarding this page here: https://docs.aws.amazon.com/cdk/latest/guide/testing.html

I spent a lot of time today trying to debug an error I had by changing the test that comes with a newly inited CDK stack which gives the following (plain JS, TS is similar I believe):

const { expect, matchTemplate, MatchStyle } = require('@aws-cdk/assert');
const cdk = require('@aws-cdk/core');
const Testjs = require('../lib/testjs-stack');

test('Empty Stack', () => {
    const app = new cdk.App();
    // WHEN
    const stack = new Testjs.TestjsStack(app, 'MyTestStack');
    // THEN
    expect(stack).to(matchTemplate({
      "Resources": {}
    }, MatchStyle.EXACT))
});

When altering this to the snapshot testing in the documentation above I made a (arguably) simple mistake of changing this to the following (this is wrong and does not work):

const { expect, SynthUtils } = require('@aws-cdk/assert');
const cdk = require('@aws-cdk/core');
const Testjs = require('../lib/testjs-stack');

test('Empty Stack', () => {
    const app = new cdk.App();
    // WHEN
    const stack = new Testjs.TestjsStack(app, 'MyTestStack');
    // THEN
    expect(SynthUtils.toCloudFormation(stack)).toMatchSnapshot()
});

The problem is that I'm picking up expect from the @aws-cdk/assert library and in this case it wants to use the expect function from jest so simply removing that import fixes this. Previously however I was getting the following error and could find no information on this (most of google threw up old Angular stuff):

> [email protected] test /Users/strotter/work/aws/cdk/testjs
> jest

 FAIL  test/testjs.test.js
  ✕ Empty Stack (122ms)

  ● Empty Stack

    TypeError: Cannot read property 'root' of undefined

       8 |     const stack = new Testjs.TestjsStack(app, 'MyTestStack');
       9 |     // THEN
    > 10 |     expect(SynthUtils.toCloudFormation(stack)).toMatchSnapshot()
         |     ^
      11 | });
      12 |

      at Function._synthesizeWithNested (node_modules/@aws-cdk/assert/lib/synth-utils.ts:54:29)
      at expect (node_modules/@aws-cdk/assert/lib/expect.ts:8:90)
      at Object.<anonymous> (test/testjs.test.js:10:5)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        1.214s
Ran all test suites.

Whilst clearly this was my error it seems it would be helpful (even if this issue just gets closed immediately) for others hitting this to have some hint as to how to fix it. So I propose one of the following steps:

  • Better documentation around this to ensure the differences between the two expect functions are explained
  • Alter the CDK assert expect function to be compatible with this and the problem goes away (no idea how much effort that would be)
  • Give a better error message saying that the expect function being used is most likely the wrong one.

Any of these might have saved me hours of debugging this afternoon so I offer them as suggestions to stop anyone else having that problem.

@aws-cdassert documentation efformedium feature-request p2 testing

Most helpful comment

Wanted to chime in to say I ran into this same issue last night. Definitely confusing for new CDK users and double or triply so for developers new to Typescript and/or Jest. Speaking as one of those developers, I had no idea Jest's expect is included in the namespace by default; it probably would have taken me quite a while to figure this out if I hadn't found this issue. Thanks @strottos

All 5 comments

@strottos - we apologize that this has caused you to lose an afternoon.

I agree that something better can be done here. Accepting this as a feature request.

@nija-at , just seen this but no problem at all. I love CDK generally, just offering as a suggestion for improvement (which I'm pleased to see being taken up). It's not common I've had problems with CDK and anything you expect some issues with 👍

Wanted to chime in to say I ran into this same issue last night. Definitely confusing for new CDK users and double or triply so for developers new to Typescript and/or Jest. Speaking as one of those developers, I had no idea Jest's expect is included in the namespace by default; it probably would have taken me quite a while to figure this out if I hadn't found this issue. Thanks @strottos

Ugh. This wasted too much of my time.

I had no idea Jest's expect is included in the namespace by default

In newer version of CDK in init they show

import { expect as expectCDK, matchTemplate, MatchStyle } from '@aws-cdk/assert';

Perhaps to do the same for Jest

import {expect as expectJest, beforeAll} from '@jest/globals';
Was this page helpful?
0 / 5 - 0 ratings