Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs(assertions): add more detail about migrating from the old assert library #25618
docs(assertions): add more detail about migrating from the old assert library #25618
Changes from all commits
adad222
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
Assertions
If you're migrating from the old
assert
library, the migration guide can be found in our GitHub repository.If you're migrating from the old
@aws-cdk/assert
library, first use this migration guide to migrate from@aws-cdk/assert
to@aws-cdk/assertions
found in our GitHub repository. Then, you can migrate your application to AWS CDK v2 in order to use this library using this guide.Functions for writing test asserting against CDK applications, with focus on CloudFormation templates.
The
Template
class includes a set of methods for writing assertions against CloudFormation templates. Use one of theTemplate.fromXxx()
static methods to create an instance of this class.To create
Template
from CDK stack, start off with:Alternatively, assertions can be run on an existing CloudFormation template -
Cyclical Resources Note
If allowing cyclical references is desired, for example in the case of unprocessed Transform templates, supply TemplateParsingOptions and set skipCyclicalDependenciesCheck to true. In all other cases, will fail on detecting cyclical dependencies.
Full Template Match
The simplest assertion would be to assert that the template matches a given template.
By default, the
templateMatches()
API will use the an 'object-like' comparison, which means that it will allow for the actual template to be a superset of the given expectation. See Special Matchers for details on how to change this.Snapshot testing is a common technique to store a snapshot of the output and compare it during future changes. Since CloudFormation templates are human readable, they are a good target for snapshot testing.
The
toJSON()
method on theTemplate
can be used to produce a well formatted JSON of the CloudFormation template that can be used as a snapshot.See Snapshot Testing in Jest and Snapshot Testing in Java.
Counting Resources
This module allows asserting the number of resources of a specific type found in a template.
You can also count the number of resources of a specific type whose
Properties
section contains the specified properties:Resource Matching & Retrieval
Beyond resource counting, the module also allows asserting that a resource with specific properties are present.
The following code asserts that the
Properties
section of a resource of typeFoo::Bar
contains the specified properties -You can also assert that the
Properties
section of all resources of typeFoo::Bar
contains the specified properties -Alternatively, if you would like to assert the entire resource definition, you can use the
hasResource()
API.You can also assert the definitions of all resources of a type using the
allResources()
API.Beyond assertions, the module provides APIs to retrieve matching resources. The
findResources()
API is complementary to thehasResource()
API, except, instead of asserting its presence, it returns the set of matching resources.By default, the
hasResource()
andhasResourceProperties()
APIs perform deep partial object matching. This behavior can be configured using matchers. See subsequent section on special matchers.Output and Mapping sections
The module allows you to assert that the CloudFormation template contains an Output that matches specific properties. The following code asserts that a template contains an Output with a
logicalId
ofFoo
and the specified properties -If you want to match against all Outputs in the template, use
*
as thelogicalId
.findOutputs()
will return a set of outputs that match thelogicalId
andprops
, and you can use the'*'
special case as well.The APIs
hasMapping()
,findMappings()
,hasCondition()
, andhasCondtions()
provide similar functionalities.Special Matchers
The expectation provided to the
hasXxx()
,findXxx()
andtemplateMatches()
APIs, besides carrying literal values, as seen in the above examples, also accept special matchers.They are available as part of the
Match
class.Object Matchers
The
Match.objectLike()
API can be used to assert that the target is a superset object of the provided pattern. This API will perform a deep partial match on the target. Deep partial matching is where objects are matched partially recursively. At each level, the list of keys in the target is a subset of the provided pattern.The
Match.objectEquals()
API can be used to assert a target as a deep exact match.Presence and Absence
The
Match.absent()
matcher can be used to specify that a specific value should not exist on the target. This can be used withinMatch.objectLike()
or outside of any matchers.The
Match.anyValue()
matcher can be used to specify that a specific value should be found at the location. This matcher will fail if when the target location has null-ish values (i.e.,null
orundefined
).This matcher can be combined with any of the other matchers.
Array Matchers
The
Match.arrayWith()
API can be used to assert that the target is equal to or a subset of the provided pattern array. This API will perform subset match on the target.Note: The list of items in the pattern array should be in order as they appear in the target array. Out of order will be recorded as a match failure.
Alternatively, the
Match.arrayEquals()
API can be used to assert that the target is exactly equal to the pattern array.String Matchers
The
Match.stringLikeRegexp()
API can be used to assert that the target matches the provided regular expression.Not Matcher
The not matcher inverts the search pattern and matches all patterns in the path that does not match the pattern specified.
Serialized JSON
Often, we find that some CloudFormation Resource types declare properties as a string, but actually expect JSON serialized as a string. For example, the
BuildSpec
property ofAWS::CodeBuild::Project
, theDefinition
property ofAWS::StepFunctions::StateMachine
, to name a couple.The
Match.serializedJson()
matcher allows deep matching within a stringified JSON.Capturing Values
The matcher APIs documented above allow capturing values in the matching entry (Resource, Output, Mapping, etc.). The following code captures a string from a matching resource.
With captures, a nested pattern can also be specified, so that only targets that match the nested pattern will be captured. This pattern can be literals or further Matchers.
When multiple resources match the given condition, each
Capture
defined in the condition will capture all matching values. They can be paged through using thenext()
API. The following example illustrates this -Asserting Annotations
In addition to template matching, we provide an API for annotation matching. Annotations can be added via the Aspects API. You can learn more about Aspects here.
Say you have a
MyAspect
and aMyStack
that usesMyAspect
:We can then assert that the stack contains the expected Error:
Here are the available APIs for
Annotations
:hasError()
,hasNoError()
, andfindError()
hasWarning()
,hasNoWarning()
, andfindWarning()
hasInfo()
,hasNoInfo()
, andfindInfo()
The corresponding
findXxx()
API is complementary to thehasXxx()
API, except instead of asserting its presence, it returns the set of matching messages.In addition, this suite of APIs is compatible with
Matchers
for more fine-grained control. For example, the following assertion works as well: