amazon-web-services - AWS CDK 单元测试失败 - jest ts

标签 amazon-web-services jestjs amazon-iam aws-cdk

我在创建 IAM 角色的 Typescript 中创建了一个 AWS CDK 应用程序。这是我的代码:

export class AccountCreatorIamRoleStack extends cdk.Stack {
    public create(): iam.Role {
        const iamRole = some logic to create iam role goes here;
        return iamRole;
    }
}

我已经通过创建一个像这样的 IAM 角色确认此代码有效:

var roleCreator = new AccountCreatorIamRoleStack(app, 'AccountCreatorIamRoleStack');
roleCreator.create();

但是,当我运行这个 Jest 单元测试时:

test('Test IAM Role Created', () => {
  const app = new cdk.App();
  // WHEN
  var stack = new AccountCreatorIamRoleStack(app, 'TestAccountCreatorIamRoleStack').create()
  // THEN
  expectCDK(stack).to(haveResource("AWS::IAM::Role"));
});

失败并出现以下错误:

 FAIL  test/account-creation-iam-role-stack.test.ts
  ✕ Test IAM Role Created (32 ms)

  ● Test IAM Role Created

    None of 0 resources matches resource 'AWS::IAM::Role' with {
      "$anything": true
    }.

      16 |   var stack = new AccountCreatorIamRoleStack(app, 'TestAccountCreatorIamRoleStack').create()
      17 |   // THEN
    > 18 |   expectCDK(stack).to(haveResource("AWS::IAM::Role"));
     |                    ^
      19 | });

      at HaveResourceAssertion.assertOrThrow (node_modules/@aws-cdk/assert/lib/assertions/have-resource.ts:100:13)
      at StackInspector._to (node_modules/@aws-cdk/assert/lib/inspector.ts:25:15)
      at StackInspector.to (node_modules/@aws-cdk/assert/lib/inspector.ts:15:14)
      at Object.<anonymous> (test/account-creation-iam-role-stack.test.ts:18:20)

我可以看到它指向那条线上的东西,但它究竟指向什么它认为是错误的?想法?

最佳答案

您的代码断言 AWS::IAM::Role 具有 "VisibilityTimeout": 300 但它没有这样的属性。当您的代码实际创建它时,它应该单独传递给 haveResource("AWS::IAM::Role")(没有其他属性)。

以下是 IAM 角色的当前属性:https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-iam-role.html

{
  "Type" : "AWS::IAM::Role",
  "Properties" : {
      "AssumeRolePolicyDocument" : Json,
      "Description" : String,
      "ManagedPolicyArns" : [ String, ... ],
      "MaxSessionDuration" : Integer,
      "Path" : String,
      "PermissionsBoundary" : String,
      "Policies" : [ Policy, ... ],
      "RoleName" : String,
      "Tags" : [ Tag, ... ]
    }
}```

关于amazon-web-services - AWS CDK 单元测试失败 - jest ts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65878646/

相关文章:

java - 适用于 Lambda 的 AWS 开发工具包 V2 Maven 库是什么?

amazon-web-services - 在 AWS 用户名和非 identity_id 下的 S3 中的 AWS Amplify 存储

javascript - 使用 Jest 测试对 API 的真实请求的状态代码

amazon-web-services - terraform:有没有办法动态创建 iam 策略声明?

amazon-web-services - 为什么将条件应用于 ec2 :DescribeInstances in an IAM policy fail?

amazon-web-services - aws cli s3 同步,排除不工作

amazon-web-services - 更有效地使用 aws s3 同步?

javascript - 访问占位符文本以使用 jest 进行验证

docker - 来自 docker 的调试 Jest

amazon-web-services - 无法通过 IAM 用户访问 AWS