npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jest-cdk-snapshot

v2.2.5

Published

Jest matcher for cdk cloudformation comparisons.

Downloads

21,780

Readme

jest-cdk-snapshot

Build Status

Jest matcher for cdk cloudformation comparisons.

Install

npm i -D jest-cdk-snapshot

How to use

Note that this targets of CDK v2. If you are still using v1 you must use v1 of this library.

import { Stack } from 'aws-cdk-lib/core';
import { Bucket } from 'aws-cdk-lib/aws-s3';
import 'jest-cdk-snapshot';

test('default setup', () => {
  const stack = new Stack();

  new Bucket(stack, 'Foo');

  expect(stack).toMatchCdkSnapshot();
});

Ignore Assets

import * as path from 'path';
import { Stack } from 'aws-cdk-lib';
import { Code, Function, Runtime } from 'aws-cdk-lib/aws-lambda';

test('ignore assets', () => {
  const stack = new Stack();
  
  new Function(stack, 'Function', {
    code: Code.fromAsset(path.join(__dirname, 'fixtures', 'lambda')),
    runtime: Runtime.NODEJS_12_X,
    handler: 'index.handler'
  });

  expect(stack).toMatchCdkSnapshot({
    ignoreAssets: true,
  });
});

Ignore CurrentVersions

With this enabled, hash-parts in Lambda's current version and in their references get masked. So the snapshot test will not fail if the hash changes. This is handy when you have concurrent pull requests that change hash.

import * as path from 'path';
import { Stack } from 'aws-cdk-lib';
import { Code, Function, Runtime } from 'aws-cdk-lib/aws-lambda';

test('ignore current version', () => {
  const stack = new Stack();
  
  new Function(stack, 'Function', {
    code: Code.fromAsset(path.join(__dirname, 'fixtures', 'lambda')),
    runtime: Runtime.NODEJS_12_X,
    handler: 'index.handler'
  });

  expect(stack).toMatchCdkSnapshot({
      ignoreCurrentVersion: true,
  });
});

Use YAML as snapshot format

import { Stack } from 'aws-cdk-lib';
import { Bucket } from 'aws-cdk-lib/aws-s3';
import 'jest-cdk-snapshot';

test('default setup', () => {
  const stack = new Stack();
  new Bucket(stack, 'Foo');

  expect(stack).toMatchCdkSnapshot({ yaml: true });
});

Show CDK bootstrap versions

CDK v2 includes bootstrap versions in CloudFormation template. These fields are generally not interesting, so ignored by default. To include these fields in snapshots, set ignoreBootstrapVersion: false explicitly.

import { Stack } from 'aws-cdk-lib';
import { Bucket } from 'aws-cdk-lib/aws-s3';
import 'jest-cdk-snapshot';

test('default setup', () => {
  const stack = new Stack();
  new Bucket(stack, 'Foo');

  expect(stack).toMatchCdkSnapshot({ ignoreBootstrapVersion: false });
});

Match only resources of given types/keys

If you only want to test certain parts of your stack, jest-cdk-snapshot offers the possibility to create a subset for specific types. Snapshots are created only for this subset.

import { Stack } from 'aws-cdk-lib';
import { Bucket } from 'aws-cdk-lib/aws-s3';
import { Topic } from 'aws-cdk-lib/aws-sns';
import 'jest-cdk-snapshot';

test('subsetResourceTypes', () => {
  const stack = new Stack();
  new Bucket(stack, 'Ignore');

  new Topic(stack, 'Topic');

  expect(stack).toMatchCdkSnapshot({
    subsetResourceTypes: ['AWS::SNS::Topic']
  });
});

test('subsetResourceKeys', () => {
  const stack = new Stack();
  new Bucket(stack, 'Ignore');

  new Topic(stack, 'Topic'); // => TopicBFC7AF6E

  expect(stack).toMatchCdkSnapshot({
    subsetResourceKeys: ['TopicBFC7AF6E'],
  });
});

PropertyMatchers

Often there are fields in the stack you want to snapshot which are generated (like Artifact hashes). If you try to snapshot these stacks, they will force the snapshot to fail on every run. For these cases, jest-cdk-snapshot allows providing an asymmetric matcher for any property. These matchers are checked before the snapshot is written or tested, and then saved to the snapshot file instead of the received value. Any given value that is not a matcher will be checked exactly and saved to the snapshot:

import { Stack } from 'aws-cdk-lib';
import { Bucket } from 'aws-cdk-lib/aws-s3';
import 'jest-cdk-snapshot';

test('propertyMatchers', () => {
  const stack = new Stack();
  new Bucket(stack, 'Random', {
    websiteIndexDocument: 'test.html',
    bucketName: `random${Math.floor(Math.random() * 20)}name`
  });

  expect(stack).toMatchCdkSnapshot({
    propertyMatchers: {
      Resources: {
        RandomF1C596BC: {
          Properties: {
            BucketName: expect.any(String), // matcher
            WebsiteConfiguration: {
              IndexDocument: 'test.html' // given value
            }
          }
        }
      }
    }
  });
});

Ignore Metadata

With this enabled CloudFormation metadata both on Stack and Resource level will be ignored. Metadata is often added by Aspects or other libraries.

import { Stack } from 'aws-cdk-lib/core';
import { Bucket } from 'aws-cdk-lib/aws-s3';
import 'jest-cdk-snapshot';

test('default setup', () => {
  const stack = new Stack();

  new Bucket(stack, 'Foo');

  expect(stack).toMatchCdkSnapshot({
    ignoreMetadata: true,
  });
});

Ignore Resource Tags

With this enabled, tags on all resources that have tags configured are ignored. This can be useful in situations where tags contain changing informatione like the commit or a pipeline id.

  expect(stack).toMatchCdkSnapshot({
    ignoreTags: true,
  });

License

MIT