项目作者: hupe1980

项目描述 :
Jest matcher for cdk cloudformation comparisons.
高级语言: TypeScript
项目地址: git://github.com/hupe1980/jest-cdk-snapshot.git
创建时间: 2019-02-09T16:49:42Z
项目社区:https://github.com/hupe1980/jest-cdk-snapshot

开源协议:MIT License

下载


jest-cdk-snapshot

Build Status

Jest matcher for cdk cloudformation comparisons.

Install

  1. 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.

  1. import { Stack } from 'aws-cdk-lib/core';
  2. import { Bucket } from 'aws-cdk-lib/aws-s3';
  3. import 'jest-cdk-snapshot';
  4. test('default setup', () => {
  5. const stack = new Stack();
  6. new Bucket(stack, 'Foo');
  7. expect(stack).toMatchCdkSnapshot();
  8. });

Ignore Assets

  1. import * as path from 'path';
  2. import { Stack } from 'aws-cdk-lib';
  3. import { Code, Function, Runtime } from 'aws-cdk-lib/aws-lambda';
  4. test('ignore assets', () => {
  5. const stack = new Stack();
  6. new Function(stack, 'Function', {
  7. code: Code.fromAsset(path.join(__dirname, 'fixtures', 'lambda')),
  8. runtime: Runtime.NODEJS_12_X,
  9. handler: 'index.handler'
  10. });
  11. expect(stack).toMatchCdkSnapshot({
  12. ignoreAssets: true,
  13. });
  14. });

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.

  1. import * as path from 'path';
  2. import { Stack } from 'aws-cdk-lib';
  3. import { Code, Function, Runtime } from 'aws-cdk-lib/aws-lambda';
  4. test('ignore current version', () => {
  5. const stack = new Stack();
  6. new Function(stack, 'Function', {
  7. code: Code.fromAsset(path.join(__dirname, 'fixtures', 'lambda')),
  8. runtime: Runtime.NODEJS_12_X,
  9. handler: 'index.handler'
  10. });
  11. expect(stack).toMatchCdkSnapshot({
  12. ignoreCurrentVersion: true,
  13. });
  14. });

Use YAML as snapshot format

  1. import { Stack } from 'aws-cdk-lib';
  2. import { Bucket } from 'aws-cdk-lib/aws-s3';
  3. import 'jest-cdk-snapshot';
  4. test('default setup', () => {
  5. const stack = new Stack();
  6. new Bucket(stack, 'Foo');
  7. expect(stack).toMatchCdkSnapshot({ yaml: true });
  8. });

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.

  1. import { Stack } from 'aws-cdk-lib';
  2. import { Bucket } from 'aws-cdk-lib/aws-s3';
  3. import 'jest-cdk-snapshot';
  4. test('default setup', () => {
  5. const stack = new Stack();
  6. new Bucket(stack, 'Foo');
  7. expect(stack).toMatchCdkSnapshot({ ignoreBootstrapVersion: false });
  8. });

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.

  1. import { Stack } from 'aws-cdk-lib';
  2. import { Bucket } from 'aws-cdk-lib/aws-s3';
  3. import { Topic } from 'aws-cdk-lib/aws-sns';
  4. import 'jest-cdk-snapshot';
  5. test('subsetResourceTypes', () => {
  6. const stack = new Stack();
  7. new Bucket(stack, 'Ignore');
  8. new Topic(stack, 'Topic');
  9. expect(stack).toMatchCdkSnapshot({
  10. subsetResourceTypes: ['AWS::SNS::Topic']
  11. });
  12. });
  13. test('subsetResourceKeys', () => {
  14. const stack = new Stack();
  15. new Bucket(stack, 'Ignore');
  16. new Topic(stack, 'Topic'); // => TopicBFC7AF6E
  17. expect(stack).toMatchCdkSnapshot({
  18. subsetResourceKeys: ['TopicBFC7AF6E'],
  19. });
  20. });

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:

  1. import { Stack } from 'aws-cdk-lib';
  2. import { Bucket } from 'aws-cdk-lib/aws-s3';
  3. import 'jest-cdk-snapshot';
  4. test('propertyMatchers', () => {
  5. const stack = new Stack();
  6. new Bucket(stack, 'Random', {
  7. websiteIndexDocument: 'test.html',
  8. bucketName: `random${Math.floor(Math.random() * 20)}name`
  9. });
  10. expect(stack).toMatchCdkSnapshot({
  11. propertyMatchers: {
  12. Resources: {
  13. RandomF1C596BC: {
  14. Properties: {
  15. BucketName: expect.any(String), // matcher
  16. WebsiteConfiguration: {
  17. IndexDocument: 'test.html' // given value
  18. }
  19. }
  20. }
  21. }
  22. }
  23. });
  24. });

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.

  1. import { Stack } from 'aws-cdk-lib/core';
  2. import { Bucket } from 'aws-cdk-lib/aws-s3';
  3. import 'jest-cdk-snapshot';
  4. test('default setup', () => {
  5. const stack = new Stack();
  6. new Bucket(stack, 'Foo');
  7. expect(stack).toMatchCdkSnapshot({
  8. ignoreMetadata: true,
  9. });
  10. });

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.

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

License

MIT