question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unit testing - toHaveResource() - found no resources instead

See original GitHub issue

Community 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 fine
  • cdk.tf.json under cdktf.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:open
  • Created a year ago
  • Reactions:1
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
paambaaticommented, Jul 20, 2022

Can you folks help understand the solution here?

If I pass in a Construct instead of a Stack, I still don’t get any generated resources from synthScope.

This is how I’ve modified the above example –

import "cdktf/lib/testing/adapters/jest";
import { Testing } from "cdktf";
+import { Construct } from "constructs";
import { PortalStack } from "../main";
import { Application } from "@cdktf/provider-azuread";

+ class PortalConstruct extends Construct {
+  constructor(scope: Construct, options: Record<PropertyKey, unknown>) {
+   super(scope, `${options.environment}-construct`)
+    new PortalStack(scope, environment)
+   }
+}

describe("Configuration", () => {
  it("should contain an application", () => {
    expect(
      Testing.synthScope((scope) => {
+        new PortalConstruct(scope, 'test');
-        new PortalStack(scope, 'test');
      })
    ).toHaveResource(Application);
  });
});
1reaction
jsteinichcommented, Jun 13, 2022

@sebolabs Currently you can’t pass a stack instance to Testing.synthScope. You can work around by making your class extend from Construct instead. Take a look here for more info.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found