When running `test`: Template did not match as expected
Closed this issue · 2 comments
Hello,
Thank you for the awesome plugin!
Right after generating the app, if I try to run yarn nx test infra
I get the following error:
● Empty Stack
Template did not match as expected. The following mismatches were found:
Missing key 'Resources' at /Resources (using objectLike matcher)
8 | const stack = new AppStack(app, 'infraTestStack');
9 | // THEN
> 10 | Template.fromStack(stack as Stack).templateMatches({
| ^
11 | Resources: {},
12 | });
13 | });
at Template.templateMatches (../../node_modules/aws-cdk-lib/assertions/lib/template.js:1:4182)
at Object.<anonymous> (src/main.test.ts:10:37)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 1.042 s, estimated 3 s
Ran all test suites.
————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
> NX Ran target undefined for project infra (2s)
✖ 1/1 failed
✔ 0/1 succeeded [0 read from cache]
error Command failed with exit code 1.
I believe this is because I did not run deploy
first. But then isn't this what cdk synth
is for?
Hi, I'm not sure from my head. I will check this tomorrow.
Sorry for this late response, life came in between.
So basically, all is correct besides the assertion. the templateMatches
method checks, if the given template in the brackets are equal. in this case, it is the object
{
Resources: {},
}
This is clearly not the case for any project. I will update this test example.
To fix this, I just run expect(Template.fromStack(stack as Stack).toJSON()).toEqual('')
and the i got the generated stack in. the cli. For me, it the updated test looked like this:
Template.fromStack(stack as Stack).templateMatches({
"Parameters": {
"BootstrapVersion": {
"Default": "/cdk-bootstrap/hnb659fds/version",
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]",
"Type": "AWS::SSM::Parameter::Value<String>"
}
},
"Rules": {
"CheckBootstrapVersion": {
"Assertions": [{
"Assert": {"Fn::Not": [{"Fn::Contains": [["1", "2", "3", "4", "5"], {"Ref": "BootstrapVersion"}]}]},
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
}]
}
}
});
Edit:
For the jest test, you should not need to use cdk synth, bootstrap or deploy.