question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

(@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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
peterwoodworthcommented, Jun 17, 2021

Hmm I’m finding the same thing as you. The if statement isn’t working, I’ll research this and get back to you

0reactions
github-actions[bot]commented, Jun 21, 2021

⚠️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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found