I created an AWS CDK app in Typescript that creates an IAM Role. Here is my code:
export class AccountCreatorIamRoleStack extends cdk.Stack {
public create(): iam.Role {
const iamRole = some logic to create iam role goes here;
return iamRole;
}
}
I have confirmed that this code works by creating an IAM Role like this:
var roleCreator = new AccountCreatorIamRoleStack(app, 'AccountCreatorIamRoleStack');
roleCreator.create();
However, when I run this jest unit test:
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"));
});
It fails with the following error:
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)
I can see it is pointing to something on that line, but what exactly is it pointing to that it thinks is an error? Thoughts?