@horietakehiro/aws-cdk-utul 0.2.2 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +84 -10
- package/docs/type-hinting-1.png +0 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,14 +1,88 @@
|
|
|
1
|
-
#
|
|
1
|
+
# AWS CDK Unit Test Utility Library
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<p align="left">
|
|
4
|
+
<a href="https://www.npmjs.com/package/@horietakehiro/aws-cdk-utul?activeTab=readme" >
|
|
5
|
+
<img alt="NPM Version" src="https://img.shields.io/npm/v/%40horietakehiro%2Faws-cdk-utul">
|
|
6
|
+
</a>
|
|
7
|
+
</p>
|
|
4
8
|
|
|
5
|
-
|
|
9
|
+
**aws-cdk-utul(unit test utility library) makes it faster, more efficient, and less mistaken for you to code AWS CDK unit tests.**
|
|
6
10
|
|
|
7
|
-
|
|
11
|
+
---
|
|
8
12
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
## Features
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
### TypedTemplate
|
|
18
|
+
|
|
19
|
+
When you code AWS CDK unit tests in some IDE(e.g. VSCode), `TypedTemplate` class provides you a type hinting and type validation for (alomost) all AWS CloudFormation resource types.
|
|
20
|
+
|
|
21
|
+

|
|
22
|
+
|
|
23
|
+
The key concepts are:
|
|
24
|
+
- You can define unit tests for your AWS CloudFormation templates by using well-type-defined methods via `TypedTemplate` class - a proxy for original `Template` class.
|
|
25
|
+
- Returned values from methods of `TypedTemplate` are also well-type-defined.
|
|
26
|
+
- You can still use `Matcher` and other arbitrary objects too.
|
|
27
|
+
|
|
28
|
+
So you can quickly code AWS CDK unit tests without any trivial mistakes and googling.
|
|
29
|
+
```js
|
|
30
|
+
import { Stack } from "aws-cdk-lib";
|
|
31
|
+
import { Template } from "aws-cdk-lib/assertions";
|
|
32
|
+
|
|
33
|
+
import { TypedTemplate } from "@horietakehiro/aws-cdk-utul/lib/assertions";
|
|
34
|
+
import { AWS_EC2_SUBNET, AWS_EC2_VPC } from "@horietakehiro/aws-cdk-utul/lib/types/cfn-resource-types";
|
|
35
|
+
const stack = new Stack()
|
|
36
|
+
|
|
37
|
+
// just wrap an original `Template` instance of aws-cdk-lib/assertions
|
|
38
|
+
const template = new TypedTemplate(Template.fromStack(stack))
|
|
39
|
+
// you can execute all method implemented in original `Template` instance
|
|
40
|
+
template.hasResource(AWS_EC2_VPC({Properties: {
|
|
41
|
+
CidrBlock: "10.0.0.0/16"
|
|
42
|
+
}}))
|
|
43
|
+
// you can still use original `Matcher` class too.
|
|
44
|
+
const subnets = template.findResources(AWS_EC2_SUBNET({Properties: {
|
|
45
|
+
Tags: Match.arrayWith([
|
|
46
|
+
{Key: "Name", Value: Match.stringLikeRegexp("Public")}
|
|
47
|
+
])
|
|
48
|
+
}}))
|
|
49
|
+
// you can access resources with more efficient way
|
|
50
|
+
subnets.forEach((sn) => {
|
|
51
|
+
expect(sn.def.Properties?.CidrBlock?.endsWith("/24")).toBe(true)
|
|
52
|
+
})
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
---
|
|
56
|
+
|
|
57
|
+
### ExtraMatch
|
|
58
|
+
|
|
59
|
+
`ExtraMatch` class provides you some kind of a syntax sugar for original `Match` class.
|
|
60
|
+
|
|
61
|
+
```js
|
|
62
|
+
import { ExtraMatch } from "@horietakehiro/aws-cdk-utul/lib/assertions"
|
|
63
|
+
// get just vpc's logical id
|
|
64
|
+
const [{id}] = template.findResources(AWS_EC2_VPC({}))
|
|
65
|
+
template.allResources(AWS_EC2_SUBNET({
|
|
66
|
+
// Equals to {VpcId: {Ref: id}}
|
|
67
|
+
Properties: {VpcId: ExtraMatch.ref(id)}
|
|
68
|
+
}))
|
|
69
|
+
template.hasOutput("VPCARN", {
|
|
70
|
+
// Equals to {Value: {"Fn::GetAtt": [id, "Arn"]}}
|
|
71
|
+
Value: ExtraMatch.getAttArn(id)
|
|
72
|
+
})
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
---
|
|
76
|
+
|
|
77
|
+
## Install
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
npm install @horietakehiro/aws-cdk-utul
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
---
|
|
84
|
+
|
|
85
|
+
## Some notes
|
|
86
|
+
|
|
87
|
+
- Schemas of AWS CloudFormation resource types used in this library are based on [those at `us-east-1`](https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/resource-type-schemas.html)
|
|
88
|
+
- Compatible with AWS CDK v2.0.0 or greater.
|
|
Binary file
|