awsdocs / aws-cdk-guide

User guide for the AWS Cloud Development Kit (CDK).
Other
330 stars 222 forks source link

Improve and validate testing topic #305

Open ghost opened 3 years ago

ghost commented 3 years ago

Some API changes have occurred and the code snippets may need updating. Review this entire topic and make sure it works correctly.

Known issues:

expect(SynthUtils.toCloudFormation(stack)).toMatchSnapshot();
Property 'toMatchSnapshot' does not exist on type 'StackInspector'
SynthUtils.toCloudFormation generates an empty object.
Solution: From jest version 24 it requires to use the enzyme-to-json serializer: https://stackoverflow.com/questions/54419342/jest-enzyme-shallowwrapper-is-empty-when-creating-snapshot

expect(stack).toHaveResource - throws an error "Property 'toHaveResource' does not exist on type 'JestMatchersShape<Matchers<void, PipelineSharedMetaStack>, Matchers<Promise<void>, PipelineSharedMetaStack>>'.".
Solution: to replace all references with expectCDK(stack).to(haveResource('XXX'));
ghost commented 3 years ago

Also got this comment:

I would like to know how to test for two resource types that only differ in name. For example I have two Lambda functions with essentially the same Roles, they only differ in name/identifier. Can I test that they are both there?

More detailed information on the various types of expectations you can use for Stack resources. Some examples are the difference between "haveResource()" and "haveResourceLike()". How to use matchers to do fuzzy matching of property values.