‫So here is a short lecture
‫around testing in the CDK.
‫Well, because you are using code with CDK
‫you can actually test your code the same way
‫you would test normal standard Python code
‫or JavaScript code, and so on.
‫So in the CDK apps you have what's called
‫the CDK assertion modules,
‫which will contain popular test frameworks
‫such as Jest for JavaScript or Pytest for Python.
‫And with this assertion modules,
‫we can verify if specific resources are what we need
‫or rules or conditions or parameters, and so on.
‫So here is a simple test where we are going to have a look
‫whether or not this synthesizes correctly.
‫That means that the CloudFormation template
‫that comes out of it contains what we need.
‫So there are two types of tests in CDK.
‫The first one is called a fine-grained assertion
‫which is the most common,
‫where you're going to test whether or not
‫some specific resources have some specific properties.
‫For example, in here we test whether or not
‫our Lambda function has a proper handler
‫and has a runtime of nodejs14.x.
‫And then we look at whether or not
‫our SNS topic subscription is only one, okay?
‫So this is fine-grained assertions.
‫And then we have snapshot tests
‫where we test the CloudFormation template
‫against a previously stored baseline templates,
‫for whatever reason, it's called a snapshot test.
‫So, this is very interesting
‫because now you can make sure that, of course,
‫your dynamic-db table is still here
‫with some, I don't know, common properties.
‫Whatever you want really.
‫And how do we test the templates?
‫We have two options.
‫The first one is to do template fromStack, MyStack,
‫and this is what we do right here
‫where we actually import a stack that we defined in CDK
‫and so we do templates fromStack, MyStack,
‫but the other way is
‫if your template does not live in CDK yet,
‫you can do an import by doing template.fromstring.
‫MyString in the string is your file.
‫And the stack is going to be built outside of CDK
‫so your CloudFormation template is outside of CDK
‫and you can still run test against
‫an external CloudFormation templates.
‫And these two commands,
‫these two methods are very important
‫to remember from an exam standpoint.
‫So fromStack and fromString.
‫All right, that's it for this lecture.
‫I hope you liked it.
‫And I will see you in the next lecture.