(@aws-cdk/assert): Unable to pass test if a resource doesn't exist
See original GitHub issue❓ General Issue
The Question
I want to write validation tests and make it a library so that every stack can follow best security practices, naming convention, … for my cdk stacks.
I’ve been looking at aws-assert for it and it seems not possible to run test only if a resource exists in the stack.
For example, if I were to write:
import { haveResourceLike,expect } from "@aws-cdk/assert";
import { mystack } from "..."
test('Bucket versioning must be enabled', () => {
expect(mystack).to(haveResourceLike("AWS::S3::Bucket", {
VersioningConfiguration: {
Status: "Enabled"
}
}));
})
without any bucket in the stack then that would fail as expected.
The issue https://github.com/aws/aws-cdk/issues/12452 had similar problem as mine and I tried the proposed solution which was
import { haveResourceLike,expect } from "@aws-cdk/assert";
import { mystack } from "..."
test('Bucket versioning must be enabled', () => {
if (haveResourceLike("AWS::S3::Bucket"))
expect(mystack).to(haveResourceLike("AWS::S3::Bucket", {
VersioningConfiguration: {
Status: "Enabled"
}
}));
})
However, the test didn’t pass.
Is there anyway to bypass this limitation?
Environment
- CDK CLI Version: 1.109
- Module Version: 1.109.0
- Node.js Version: v12.18.3
- OS: Windows 10
- Language (Version): TypeScript (4.3.3)
Other information
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:6 (5 by maintainers)
Top Results From Across the Web
aws-cdk/aws-assert: Can I run a test against a resource ONLY ...
You can run tests on resources only if they exist in the stack! To do this, you'll have to implement a bit of...
Read more >Testing constructs - AWS Cloud Development Kit (AWS CDK) v2
Fine-grained assertions test specific aspects of the generated AWS CloudFormation template, such as "this resource has this property with this value." These ...
Read more >Troubleshooting common AWS CDK issues
This topic describes how to troubleshoot the following issues with the AWS CDK.
Read more >Testing constructs - AWS Cloud Development Kit (AWS CDK) v1
Fine-grained assertions test specific aspects of the generated AWS CloudFormation template, such as "this resource has this property with this value." These ...
Read more >aws-cdk/assertions module
This matcher will fail if when the target location has null-ish values (i.e., null or undefined ). This matcher can be combined with...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hmm I’m finding the same thing as you. The if statement isn’t working, I’ll research this and get back to you
⚠️COMMENT VISIBILITY WARNING⚠️
Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.