Unit testing - toHaveResource() - found no resources instead
See original GitHub issueCommunity Note
- Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
- Please do not leave “+1” or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
- If you are interested in working on this issue or have submitted a pull request, please leave a comment
cdktf & Language Versions
cdktf 0.11.0 (type-script)
Affected Resource(s)
Unit tests not working like I would expect. Could be me though 😃
Debug Output
N/A
Expected Behavior
Test should pass unless I’m doing something wrong.
Actual Behavior
> portal-azure@1.0.0 test
> jest
FAIL __tests__/main-test.ts (9.099 s)
● Configuration › should contain an application
Expected azuread_application with properties {} to be present in synthesised stack.
Found no azuread_application resources instead
28 | new PortalStack(scope, 'test');
29 | })
> 30 | ).toHaveResource(Application);
| ^
31 | });
at Object.<anonymous> (__tests__/main-test.ts:30:7)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 0 todo, 0 passed, 1 total
Snapshots: 0 total
Time: 4.642 s
Steps to Reproduce
import "cdktf/lib/testing/adapters/jest";
import { Testing } from "cdktf";
import { PortalStack } from "../main";
import { Application } from "@cdktf/provider-azuread";
describe("Configuration", () => {
it("should contain an application", () => {
expect(
Testing.synthScope((scope) => {
new PortalStack(scope, 'test');
})
).toHaveResource(Application);
});
});
Important Factoids
cdktf synth/diff/deploy
work perfectly finecdk.tf.json
undercdktf.out
contains the following:
[
[...]
"resource": {
"azuread_application": {
"xxxauthportal_portalapp_3876DFED": {
"//": {
"metadata": {
[...]
]
References (main.ts)
import { Construct } from "constructs";
import {
App,
TerraformStack,
RemoteBackend,
} from "cdktf";
import {
AzureadProvider,
ApplicationFeatureTags,
Application,
} from "./.gen/providers/azuread";
export class PortalStack extends TerraformStack {
constructor(scope: Construct, name: string) {
super(scope, name);
new AzureadProvider(this, "AzureAd", {});
this.AzureAdApp();
}
AzureAdApp(this: PortalStack) {
const samlIdUri = "";
const samlReplyUrl = "";
const samlLogoutUrl = "";
const appFeatureTags: ApplicationFeatureTags = {
enterprise: true,
};
new Application(this, "portal_app", {
displayName: `AuthPortal`,
featureTags: [appFeatureTags],
identifierUris: [samlIdUri],
web: {
redirectUris: [samlReplyUrl],
logoutUrl: samlLogoutUrl,
},
preventDuplicateNames: true,
});
}
}
const app = new App();
const stack = new PortalStack(app, "auth-portal");
new RemoteBackend(stack, {
hostname: "app.terraform.io",
organization: "xxx",
workspaces: {
name: `auth-portal`,
}
});
app.synth();
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:9 (2 by maintainers)
Top Results From Across the Web
jestjs - In CDK testing, what is the difference between ...
According to the code here and here the only difference is that toHaveResource requires that values for the keys passed must match exactly, ......
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.
Read more >Infrastructure Tests with CDK - kreuzwerker
This article gives an overview of the main testing functionalities provided by the AWS Cloud Development Kit (CDK) and shares some of our ......
Read more >@aws-cdk/assert - npm
We recommend you use the @aws-cdk/assertions module instead. This library contains helpers for writing unit tests and integration tests for CDK ...
Read more >Assertion Tests | AWS CDK Workshop
lib/hitcounter'; test('DynamoDB Table Created', () => { const stack = new cdk.Stack(); // WHEN new HitCounter(stack, 'MyTestConstruct', { downstream: new ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Can you folks help understand the solution here?
If I pass in a
Construct
instead of aStack
, I still don’t get any generated resources fromsynthScope
.This is how I’ve modified the above example –
@sebolabs Currently you can’t pass a stack instance to
Testing.synthScope
. You can work around by making your class extend fromConstruct
instead. Take a look here for more info.