Skip to content

Commit

Permalink
feat: ignore metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
webratz committed Aug 14, 2024
1 parent ecfb3e5 commit 962b5ad
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 3 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,27 @@ test('propertyMatchers', () => {
});
```

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

```typescript
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,
});
});
```

## License

[MIT](LICENSE)
12 changes: 12 additions & 0 deletions src/__tests__/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,18 @@ exports[`ignore current version 1`] = `
}
`;
exports[`metadata should not be included if skipped 1`] = `
{
"Resources": {
"FooDFE0DD70": {
"DeletionPolicy": "Retain",
"Type": "AWS::S3::Bucket",
"UpdateReplacePolicy": "Retain",
},
},
}
`;
exports[`multiple resources 1`] = `
{
"Resources": {
Expand Down
40 changes: 39 additions & 1 deletion src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { CfnOutput, CfnParameter, Stack } from "aws-cdk-lib";
import {
Aspects,
CfnOutput,
CfnParameter,
CfnResource,
IAspect,
Stack,
} from "aws-cdk-lib";
import { Code, Function, Runtime } from "aws-cdk-lib/aws-lambda";
import { AccountPrincipal } from "aws-cdk-lib/aws-iam";
import { Bucket } from "aws-cdk-lib/aws-s3";
Expand Down Expand Up @@ -143,3 +150,34 @@ test("show bootstrap version if ignoreBootstrapVersion is explicitly false", ()
ignoreBootstrapVersion: false,
});
});

test("metadata should not be included if skipped", () => {
const stack = new Stack();
// There is different methods and places where metadata can be added. We want to ensure none leave a trace
// adding metadata via Aspect and cfnOptions
const dataAdder: IAspect = {
visit: (node) => {
if (CfnResource.isCfnResource(node)) {
node.cfnOptions.metadata = {
...node.cfnOptions.metadata,
["dummyKey"]: "dummyValue",
};
}
},
};
Aspects.of(stack).add(dataAdder);

// Add metadata on Stack level
stack.addMetadata("stackMeta", "dummy");

// Add metadata on different resource levels
const bucket = new Bucket(stack, "Foo");
bucket.node.addMetadata("nodeMetadata", "dummy");
if (bucket.node.defaultChild) {
bucket.node.defaultChild.node.addMetadata("resourceMetadata", "dummy");
}

expect(stack).toMatchCdkSnapshot({
ignoreMetadata: true,
});
});
23 changes: 21 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ export type Options = StageSynthesisOptions & {
* Defaults to `true`.
*/
ignoreBootstrapVersion?: boolean;

/**
* Ignore Metadata
*/
ignoreMetadata?: boolean;
};

const currentVersionRegex = /^(.+CurrentVersion[0-9A-F]{8})[0-9a-f]{32}$/;
Expand All @@ -68,7 +73,7 @@ export const toMatchCdkSnapshot = function (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this: any,
received: Stack,
options: Options = {},
options: Options = {}
) {
const matcher = toMatchSnapshot.bind(this);
const { propertyMatchers, ...convertOptions } = options;
Expand Down Expand Up @@ -121,6 +126,7 @@ const convertStack = (stack: Stack, options: Options = {}) => {
ignoreAssets = false,
ignoreBootstrapVersion = true,
ignoreCurrentVersion = false,
ignoreMetadata = false,
subsetResourceTypes,
subsetResourceKeys,
...synthOptions
Expand Down Expand Up @@ -184,6 +190,19 @@ const convertStack = (stack: Stack, options: Options = {}) => {
}
}

if (ignoreMetadata && template.Metadata) {
delete template.Metadata;
}

if (ignoreMetadata && template.Resources) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Object.values(template.Resources).forEach((resource: any) => {
if (resource?.Metadata) {
delete resource.Metadata;
}
});
}

return yaml ? jsYaml.safeDump(template) : template;
};

Expand All @@ -193,6 +212,6 @@ if (expect !== undefined) {
console.error(
"Unable to find Jest's global expect." +
"\nPlease check you have added jest-cdk-snapshot correctly." +
"\nSee https://github.com/hupe1980/jest-cdk-snapshot for help.",
"\nSee https://github.com/hupe1980/jest-cdk-snapshot for help."
);
}

0 comments on commit 962b5ad

Please sign in to comment.