Skip to content

Commit bdd91cb

Browse files
authored
chore(s3): migrate tests to assertions (#18079)
---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 6a6bf65 commit bdd91cb

9 files changed

+123
-283
lines changed

packages/@aws-cdk/aws-s3/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
},
8080
"license": "Apache-2.0",
8181
"devDependencies": {
82-
"@aws-cdk/assert-internal": "0.0.0",
82+
"@aws-cdk/assertions": "0.0.0",
8383
"@aws-cdk/cdk-build-tools": "0.0.0",
8484
"@aws-cdk/cdk-integ-tools": "0.0.0",
8585
"@aws-cdk/cfn2ts": "0.0.0",

packages/@aws-cdk/aws-s3/test/aspect.test.ts

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
1-
import '@aws-cdk/assert-internal/jest';
2-
import { SynthUtils } from '@aws-cdk/assert-internal';
31
import * as cdk from '@aws-cdk/core';
42
import { IConstruct } from 'constructs';
53
import * as s3 from '../lib';
64

75
describe('aspect', () => {
86
test('bucket must have versioning: failure', () => {
97
// GIVEN
10-
const stack = new cdk.Stack();
8+
const app = new cdk.App();
9+
const stack = new cdk.Stack(app);
1110
new s3.Bucket(stack, 'MyBucket');
1211

1312
// WHEN
1413
cdk.Aspects.of(stack).add(new BucketVersioningChecker());
1514

1615
// THEN
17-
const assembly = SynthUtils.synthesize(stack);
16+
const assembly = app.synth().getStackArtifact(stack.artifactId);
1817
const errorMessage = assembly.messages.find(m => m.entry.data === 'Bucket versioning is not enabled');
1918
expect(errorMessage).toBeDefined();
20-
21-
2219
});
2320

2421
test('bucket must have versioning: success', () => {
2522
// GIVEN
26-
const stack = new cdk.Stack();
23+
const app = new cdk.App();
24+
const stack = new cdk.Stack(app);
2725
new s3.Bucket(stack, 'MyBucket', {
2826
versioned: true,
2927
});
@@ -32,10 +30,8 @@ describe('aspect', () => {
3230
cdk.Aspects.of(stack).add(new BucketVersioningChecker());
3331

3432
// THEN
35-
const assembly = SynthUtils.synthesize(stack);
33+
const assembly = app.synth().getStackArtifact(stack.artifactId);
3634
expect(assembly.messages.length).toEqual(0);
37-
38-
3935
});
4036
});
4137

packages/@aws-cdk/aws-s3/test/bucket-policy.test.ts

+4-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import '@aws-cdk/assert-internal/jest';
1+
import { Template } from '@aws-cdk/assertions';
22
import { AnyPrincipal, PolicyStatement } from '@aws-cdk/aws-iam';
33
import { RemovalPolicy, Stack, App } from '@aws-cdk/core';
44
import * as s3 from '../lib';
@@ -20,7 +20,7 @@ describe('bucket policy', () => {
2020
principals: [new AnyPrincipal()],
2121
}));
2222

23-
expect(stack).toHaveResource('AWS::S3::BucketPolicy', {
23+
Template.fromStack(stack).hasResourceProperties('AWS::S3::BucketPolicy', {
2424
Bucket: {
2525
'Ref': 'MyBucketF68F3FF0',
2626
},
@@ -36,8 +36,6 @@ describe('bucket policy', () => {
3636
],
3737
},
3838
});
39-
40-
4139
});
4240

4341
test('when specifying a removalPolicy at creation', () => {
@@ -54,7 +52,7 @@ describe('bucket policy', () => {
5452
principals: [new AnyPrincipal()],
5553
}));
5654

57-
expect(stack).toMatchTemplate({
55+
Template.fromStack(stack).templateMatches({
5856
'Resources': {
5957
'MyBucketF68F3FF0': {
6058
'Type': 'AWS::S3::Bucket',
@@ -84,8 +82,6 @@ describe('bucket policy', () => {
8482
},
8583
},
8684
});
87-
88-
8985
});
9086

9187
test('when specifying a removalPolicy after creation', () => {
@@ -99,7 +95,7 @@ describe('bucket policy', () => {
9995
}));
10096
myBucket.policy?.applyRemovalPolicy(RemovalPolicy.RETAIN);
10197

102-
expect(stack).toMatchTemplate({
98+
Template.fromStack(stack).templateMatches({
10399
'Resources': {
104100
'MyBucketF68F3FF0': {
105101
'Type': 'AWS::S3::Bucket',
@@ -129,8 +125,6 @@ describe('bucket policy', () => {
129125
},
130126
},
131127
});
132-
133-
134128
});
135129

136130
test('fails if bucket policy has no actions', () => {
@@ -143,8 +137,6 @@ describe('bucket policy', () => {
143137
}));
144138

145139
expect(() => app.synth()).toThrow(/A PolicyStatement must specify at least one \'action\' or \'notAction\'/);
146-
147-
148140
});
149141

150142
test('fails if bucket policy has no IAM principals', () => {
@@ -157,7 +149,5 @@ describe('bucket policy', () => {
157149
}));
158150

159151
expect(() => app.synth()).toThrow(/A PolicyStatement used in a resource-based policy must specify at least one IAM principal/);
160-
161-
162152
});
163153
});

0 commit comments

Comments
 (0)