Skip to content

Commit

Permalink
Use aws-cdk-lib/assertions
Browse files Browse the repository at this point in the history
This is in the docs and lets you add fine-grained assertions on top.

https://docs.aws.amazon.com/cdk/v2/guide/testing.html#testing_snapshot
  • Loading branch information
72636c committed Jan 7, 2024
1 parent 2c8cec9 commit c7929c1
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

exports[`returns expected CloudFormation stack for dev 1`] = `
{
"Parameters": {
"BootstrapVersion": {
"Default": "/cdk-bootstrap/hnb659fds/version",
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]",
"Type": "AWS::SSM::Parameter::Value<String>",
},
},
"Resources": {
"kmskey49FBC3B3": {
"DeletionPolicy": "Retain",
Expand Down Expand Up @@ -358,11 +365,45 @@ exports[`returns expected CloudFormation stack for dev 1`] = `
"UpdateReplacePolicy": "Delete",
},
},
"Rules": {
"CheckBootstrapVersion": {
"Assertions": [
{
"Assert": {
"Fn::Not": [
{
"Fn::Contains": [
[
"1",
"2",
"3",
"4",
"5",
],
{
"Ref": "BootstrapVersion",
},
],
},
],
},
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI.",
},
],
},
},
}
`;

exports[`returns expected CloudFormation stack for prod 1`] = `
{
"Parameters": {
"BootstrapVersion": {
"Default": "/cdk-bootstrap/hnb659fds/version",
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]",
"Type": "AWS::SSM::Parameter::Value<String>",
},
},
"Resources": {
"kmskey49FBC3B3": {
"DeletionPolicy": "Retain",
Expand Down Expand Up @@ -719,5 +760,32 @@ exports[`returns expected CloudFormation stack for prod 1`] = `
"UpdateReplacePolicy": "Delete",
},
},
"Rules": {
"CheckBootstrapVersion": {
"Assertions": [
{
"Assert": {
"Fn::Not": [
{
"Fn::Contains": [
[
"1",
"2",
"3",
"4",
"5",
],
{
"Ref": "BootstrapVersion",
},
],
},
],
},
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI.",
},
],
},
},
}
`;
6 changes: 4 additions & 2 deletions template/lambda-sqs-worker-cdk/infra/appStack.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SynthUtils } from '@aws-cdk/assert';
import { App } from 'aws-cdk-lib';
import { Template } from 'aws-cdk-lib/assertions';

import cdkJson from '../cdk.json';

Expand All @@ -24,7 +24,9 @@ it.each(contexts)(

const stack = new AppStack(app, 'appStack');

const json = JSON.stringify(SynthUtils.toCloudFormation(stack)).replace(
const template = Template.fromStack(stack);

const json = JSON.stringify(template.toJSON()).replace(
/"S3Key":"([0-9a-f]+)\.zip"/g,
(_, hash) => `"S3Key":"${'x'.repeat(hash.length)}.zip"`,
);
Expand Down
1 change: 0 additions & 1 deletion template/lambda-sqs-worker-cdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"zod": "^3.19.1"
},
"devDependencies": {
"@aws-cdk/assert": "^2.24.0",
"@types/aws-lambda": "^8.10.82",
"@types/node": "^20.9.0",
"aws-cdk": "^2.109.0",
Expand Down

0 comments on commit c7929c1

Please sign in to comment.