Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

AWS Community Builders  profile imageDakota Lewallen
Dakota Lewallen forAWS Community Builders

Posted on • Originally published atdakotalewallen.substack.com

     

Three Tips for Writing CDK Tests

1. Use the assertions module

Something of a no-brainer, but use the tools you’re given! One of the best parts of the CDK library is the assertions module. It contains two sections. One for “fine-grained” assertions, like what you would write during unit testing. The other is for performing “snapshot” tests, in which you compare the new template against one that is already deployed. It’s a section of the library that can be easily missed but provides so much value when used correctly.

2. Make any Nested Stacks easily accessible

One of the benefits of using CDK is the ability to create declarative dependency trees. But, testing the implemented Nested Stacks can be tricky. One of the best ways I’ve found to simplify this is to add Nested Stacks as properties of the Stack class that handles deploying them. For example…

importSubStackfrom'substack-stack.ts';exportclassTopStackextendsStack{constructor(scope,id,props){constsubStack=newSubStack(this,'substack',props);}}
Enter fullscreen modeExit fullscreen mode

Is pretty standard. While it is possible to test this, you can simplify the process by “stashing” a reference.

importSubStackfrom'substack-stack.ts';exportclassTopStackextendsStack{subStack:SubStack;constructor(scope,id,props){this.subStack=newSubStack(this,'substack',props);}}
Enter fullscreen modeExit fullscreen mode

Now accessing this for fine-grained assertions can look like…

import{Capture,Match,Template}from"aws-cdk-lib/assertions";import*ascdkfrom"aws-cdk-lib";import{TopStack}from"top-stack";describe("TopStack",()=>{test("has the substack",()=>{constapp=newcdk.App();consttopStack=newTopStack(app,"topStack",{});const{subStack}=topStack;// we pull the substack through the property// Pass the subStack to template.consttemplate=Template.fromStack(subStack);}}
Enter fullscreen modeExit fullscreen mode

3. Write your template to a file (sometimes)

While writing assertions, you can find situations where the tests are failing for reasons that may not make sense. When this happens, one of the first things I will do is write the CloudFormation template out into a file. This way you can get a cold hard copy of what it is you are building in your hand. Rather than trying to parse through complex logs or mysterious error messages. Not a guarantee but often getting to see the complete output can solve many common testing pitfalls.

Conclusion

Testing is great! Testing your infrastructure… Even better! If you’re just getting started with CDK or if you’ve been around since the early days, I hope these tips can help you along your journey! If you have any tips you'd like to add, share them in the comments. Always looking to learn more!


Find me onLinkedIn |Github

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Build On!

Would you like to become an AWS Community Builder? Learn more about the program and apply to join when applications are open next.

More fromAWS Community Builders

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp