Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Aspects): Aspects do not work during unit tests #24220

Closed
mvs5465 opened this issue Feb 17, 2023 · 3 comments
Closed

(Aspects): Aspects do not work during unit tests #24220

mvs5465 opened this issue Feb 17, 2023 · 3 comments
Labels
@aws-cdk/aws-s3 Related to Amazon S3 bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. package/tools Related to AWS CDK Tools or CLI

Comments

@mvs5465
Copy link

mvs5465 commented Feb 17, 2023

Describe the bug

Aspects work as expected when attempting to synth or deploy from an entrypoint file like src/main.ts. However, it appears that something is interrupting this when the same code is used in a unit test.

Expected Behavior

Aspect annotations should appear on stacks and templates when used in a unit test.

Current Behavior

Aspects have no effect when used in a unit test.

Reproduction Steps

Add this code to your src/main.ts file and run cdk synth. It will log a number of issues to the console.

However if you add it during a unit test, eg src/test/Bucket.test.ts, it appears that Aspects no longer function.

import * as cdk from 'aws-cdk-lib';
import * as cdk_nag from 'cdk-nag';

const app = new cdk.App();
const stack = new cdk.Stack(app, 'test-stack');

cdk.Aspects.of(stack).add(new cdk_nag.AwsSolutionsChecks({ verbose: true }));

new cdk.aws_s3.Bucket(stack, 'bucket');

app.synth().stacks.forEach(curStack => {
  console.log(JSON.stringify(curStack.template, null, 2));
});

This will output a JSON representation of the stack and no annotations have been added. Creating a basic S3 bucket like this should light up the AwsSolutionsChecks with many errors (missing replication, encryption, logging, etc). However, nothing occurs.

Possible Solution

No response

Additional Information/Context

Related issues:

CDK CLI Version

2.65.0

Framework Version

No response

Node.js Version

v16.14.0

OS

na

Language

Typescript

Language Version

TypeScript (4.8.4)

Other information

No response

@mvs5465 mvs5465 added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Feb 17, 2023
@github-actions github-actions bot added @aws-cdk/aws-s3 Related to Amazon S3 package/tools Related to AWS CDK Tools or CLI labels Feb 17, 2023
@mvs5465
Copy link
Author

mvs5465 commented Feb 17, 2023

I'm not sure why, but wrapping the calls inside of the jest test wrappers appears to fix it?

      beforeAll(() => {
        cdk.Aspects.of(stack).add(new cdk_nag.AwsSolutionsChecks({ verbose: true }));
      });

      test('No unsuppressed Warnings', () => {
        const warnings = Annotations.fromStack(this.stack).findWarning(
          '*',
          Match.stringLikeRegexp('AwsSolutions-.*'),
        );
        if (warnings.length > 0) {
          error(warnings);
        }
        expect(warnings).toHaveLength(0);
      });

@mvs5465
Copy link
Author

mvs5465 commented Feb 17, 2023

Sorry, this is incorrect.

@mvs5465 mvs5465 closed this as completed Feb 17, 2023
@github-actions
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-s3 Related to Amazon S3 bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. package/tools Related to AWS CDK Tools or CLI
Projects
None yet
Development

No branches or pull requests

1 participant