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.

Can't read context in aws cdk construct when unit testing

See original GitHub issue

❓ General Issue

Can’t read context in aws cdk construct when unit testing Here is the type script code in cdk constuct:

const ENV_NAME = this.node.tryGetContext("ENV_NAME") ?
this.node.tryGetContext("ENV_NAME") : ``;
        if (_.isEmpty(ENV_NAME)) {
            throw new Error('ENV_NAME context is required...')
        }

Following is the error when unit testing from my jest unit test after run…

FAIL test/cdk.predeploy.test.ts ✕ Empty Stack (5ms)

● Empty Stack

ENV_NAME context is required...

Need help! Thanks.

The Question

Environment

  • CDK CLI Version:
  • Module Version:
  • OS:
  • Language:

Other information

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
bbrfkrcommented, Mar 23, 2020

I hope to load context of cdk.json when npm test run!

5reactions
msmjackcommented, Feb 17, 2021

I never figured out how to do this with some of my nested context values, but I did get my testing to work in Python when I refactored like this. May not be the best option for you, but it works.

def my_method()
    app = core.App()

    app.node.try_get_context("thing")
    app.node.try_get_context("thing2")

And the mocking…

self.expected_app = MagicMock()
self.mock_app = patch("aws_cdk.core.App")
self.mock_app.return_value = self.expected_app
self.expected_app.node.try_get_context.side_effect = [
    "thing",
    "thing2"
]
Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing constructs - AWS Cloud Development Kit (AWS CDK) v2
The standard approach to testing AWS CDK apps uses the AWS CDK's assertions module and popular test frameworks like Jest for TypeScript and...
Read more >
Get a value from a context variable - AWS Documentation
To create a command line context variable, use the --context (-c) option, as shown in the following example. cdk synth -c bucket_name=mygroovybucket. To...
Read more >
Testing constructs - AWS Cloud Development Kit (AWS CDK) v1
This article illustrates the standard approach to testing AWS CDK apps using the AWS CDK's assertions module and popular test frameworks such as...
Read more >
Runtime context - AWS Cloud Development Kit (AWS CDK) v2
Context values that are set by the AWS CDK Toolkit (the cdk command) can be set automatically, from a file, or from the...
Read more >
Best practices for developing and deploying cloud ...
With the AWS CDK, developers or administrators can define their cloud infrastructure ... To consistently run a full suite of unit tests at...
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