Skip to content

Commit

Permalink
chore: migrate sns, lambda-event-sources, cloudwatch to jest (#15606)
Browse files Browse the repository at this point in the history
Also migrate sns and lambda-event-sources to the new
'assertions' module.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
Niranjan Jayakar authored Jul 18, 2021
1 parent fc68df4 commit 8668e15
Show file tree
Hide file tree
Showing 32 changed files with 1,354 additions and 1,440 deletions.
2 changes: 0 additions & 2 deletions packages/@aws-cdk/aws-apigatewayv2-integrations/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,8 @@
"license": "Apache-2.0",
"devDependencies": {
"@types/jest": "^26.0.24",
"@types/nodeunit": "^0.0.32",
"cdk-build-tools": "0.0.0",
"cdk-integ-tools": "0.0.0",
"nodeunit": "^0.11.3",
"pkglint": "0.0.0",
"@aws-cdk/assert-internal": "0.0.0"
},
Expand Down
3 changes: 2 additions & 1 deletion packages/@aws-cdk/aws-cloudwatch/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ nyc.config.js
*.snk
!.eslintrc.js

junit.xml
junit.xml
!jest.config.js
3 changes: 2 additions & 1 deletion packages/@aws-cdk/aws-cloudwatch/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ tsconfig.json
**/cdk.out
junit.xml
test/
!*.lit.ts
!*.lit.ts
jest.config.js
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-cloudwatch/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const baseConfig = require('../../../tools/cdk-build-tools/config/jest.config');
module.exports = baseConfig;
5 changes: 3 additions & 2 deletions packages/@aws-cdk/aws-cloudwatch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
},
"cdk-build": {
"cloudformation": "AWS::CloudWatch",
"jest": true,
"env": {
"AWSLINT_BASE_CONSTRUCT": "true"
}
Expand All @@ -72,11 +73,11 @@
},
"license": "Apache-2.0",
"devDependencies": {
"@types/nodeunit": "^0.0.32",
"@types/jest": "^26.0.24",
"cdk-build-tools": "0.0.0",
"cdk-integ-tools": "0.0.0",
"cfn2ts": "0.0.0",
"nodeunit": "^0.11.3",
"jest": "^26.6.3",
"pkglint": "0.0.0",
"@aws-cdk/assert-internal": "0.0.0"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Stack } from '@aws-cdk/core';
import { Test } from 'nodeunit';
import { Metric, Alarm, AlarmStatusWidget } from '../lib';
export = {
'alarm status widget'(test: Test) {
describe('Alarm Status Widget', () => {
test('alarm status widget', () => {
// GIVEN
const stack = new Stack();
const metric = new Metric({ namespace: 'CDK', metricName: 'Test' });
Expand All @@ -18,7 +17,7 @@ export = {
});

// THEN
test.deepEqual(stack.resolve(widget.toJson()), [
expect(stack.resolve(widget.toJson())).toEqual([
{
type: 'alarm',
width: 6,
Expand All @@ -30,6 +29,6 @@ export = {
},
]);

test.done();
},
};

});
});
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { ABSENT, expect, haveResource } from '@aws-cdk/assert-internal';
import '@aws-cdk/assert-internal/jest';
import { ABSENT } from '@aws-cdk/assert-internal';
import { Duration, Stack } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { Test } from 'nodeunit';
import { Alarm, IAlarm, IAlarmAction, Metric, MathExpression, IMetric } from '../lib';

const testMetric = new Metric({
namespace: 'CDK/Test',
metricName: 'Metric',
});

export = {
describe('Alarm', () => {

'alarm does not accept a math expression with more than 10 metrics'(test: Test) {
test('alarm does not accept a math expression with more than 10 metrics', () => {

const stack = new Stack();

Expand All @@ -30,19 +30,19 @@ export = {
usingMetrics,
});

test.throws(() => {
expect(() => {

new Alarm(stack, 'Alarm', {
metric: math,
threshold: 1000,
evaluationPeriods: 3,
});

}, /Alarms on math expressions cannot contain more than 10 individual metrics/);
}).toThrow(/Alarms on math expressions cannot contain more than 10 individual metrics/);

test.done();
},
'non ec2 instance related alarm does not accept EC2 action'(test: Test) {

});
test('non ec2 instance related alarm does not accept EC2 action', () => {

const stack = new Stack();
const alarm = new Alarm(stack, 'Alarm', {
Expand All @@ -51,12 +51,12 @@ export = {
evaluationPeriods: 2,
});

test.throws(() => {
expect(() => {
alarm.addAlarmAction(new Ec2TestAlarmAction('arn:aws:automate:us-east-1:ec2:reboot'));
}, /EC2 alarm actions requires an EC2 Per-Instance Metric. \(.+ does not have an 'InstanceId' dimension\)/);
test.done();
},
'can make simple alarm'(test: Test) {
}).toThrow(/EC2 alarm actions requires an EC2 Per-Instance Metric. \(.+ does not have an 'InstanceId' dimension\)/);

});
test('can make simple alarm', () => {
// GIVEN
const stack = new Stack();

Expand All @@ -68,20 +68,20 @@ export = {
});

// THEN
expect(stack).to(haveResource('AWS::CloudWatch::Alarm', {
expect(stack).toHaveResource('AWS::CloudWatch::Alarm', {
ComparisonOperator: 'GreaterThanOrEqualToThreshold',
EvaluationPeriods: 3,
MetricName: 'Metric',
Namespace: 'CDK/Test',
Period: 300,
Statistic: 'Average',
Threshold: 1000,
}));
});

test.done();
},

'override metric period in Alarm'(test: Test) {
});

test('override metric period in Alarm', () => {
// GIVEN
const stack = new Stack();

Expand All @@ -94,20 +94,20 @@ export = {
});

// THEN
expect(stack).to(haveResource('AWS::CloudWatch::Alarm', {
expect(stack).toHaveResource('AWS::CloudWatch::Alarm', {
ComparisonOperator: 'GreaterThanOrEqualToThreshold',
EvaluationPeriods: 3,
MetricName: 'Metric',
Namespace: 'CDK/Test',
Period: 600,
Statistic: 'Average',
Threshold: 1000,
}));
});

test.done();
},

'override statistic Alarm'(test: Test) {
});

test('override statistic Alarm', () => {
// GIVEN
const stack = new Stack();

Expand All @@ -120,7 +120,7 @@ export = {
});

// THEN
expect(stack).to(haveResource('AWS::CloudWatch::Alarm', {
expect(stack).toHaveResource('AWS::CloudWatch::Alarm', {
ComparisonOperator: 'GreaterThanOrEqualToThreshold',
EvaluationPeriods: 3,
MetricName: 'Metric',
Expand All @@ -129,12 +129,12 @@ export = {
Statistic: 'Maximum',
ExtendedStatistic: ABSENT,
Threshold: 1000,
}));
});


test.done();
},
});

'can use percentile in Alarm'(test: Test) {
test('can use percentile in Alarm', () => {
// GIVEN
const stack = new Stack();

Expand All @@ -147,7 +147,7 @@ export = {
});

// THEN
expect(stack).to(haveResource('AWS::CloudWatch::Alarm', {
expect(stack).toHaveResource('AWS::CloudWatch::Alarm', {
ComparisonOperator: 'GreaterThanOrEqualToThreshold',
EvaluationPeriods: 3,
MetricName: 'Metric',
Expand All @@ -156,12 +156,12 @@ export = {
Statistic: ABSENT,
ExtendedStatistic: 'p99',
Threshold: 1000,
}));
});


test.done();
},
});

'can set DatapointsToAlarm'(test: Test) {
test('can set DatapointsToAlarm', () => {
// GIVEN
const stack = new Stack();

Expand All @@ -174,7 +174,7 @@ export = {
});

// THEN
expect(stack).to(haveResource('AWS::CloudWatch::Alarm', {
expect(stack).toHaveResource('AWS::CloudWatch::Alarm', {
ComparisonOperator: 'GreaterThanOrEqualToThreshold',
EvaluationPeriods: 3,
DatapointsToAlarm: 2,
Expand All @@ -183,12 +183,12 @@ export = {
Period: 300,
Statistic: 'Average',
Threshold: 1000,
}));
});


test.done();
},
});

'can add actions to alarms'(test: Test) {
test('can add actions to alarms', () => {
// GIVEN
const stack = new Stack();

Expand All @@ -204,16 +204,16 @@ export = {
alarm.addOkAction(new TestAlarmAction('C'));

// THEN
expect(stack).to(haveResource('AWS::CloudWatch::Alarm', {
expect(stack).toHaveResource('AWS::CloudWatch::Alarm', {
AlarmActions: ['A'],
InsufficientDataActions: ['B'],
OKActions: ['C'],
}));
});

test.done();
},

'can make alarm directly from metric'(test: Test) {
});

test('can make alarm directly from metric', () => {
// GIVEN
const stack = new Stack();

Expand All @@ -226,20 +226,20 @@ export = {
});

// THEN
expect(stack).to(haveResource('AWS::CloudWatch::Alarm', {
expect(stack).toHaveResource('AWS::CloudWatch::Alarm', {
ComparisonOperator: 'GreaterThanOrEqualToThreshold',
EvaluationPeriods: 2,
MetricName: 'Metric',
Namespace: 'CDK/Test',
Period: 10,
Statistic: 'Minimum',
Threshold: 1000,
}));
});

test.done();
},

'can use percentile string to make alarm'(test: Test) {
});

test('can use percentile string to make alarm', () => {
// GIVEN
const stack = new Stack();

Expand All @@ -251,13 +251,13 @@ export = {
});

// THEN
expect(stack).to(haveResource('AWS::CloudWatch::Alarm', {
expect(stack).toHaveResource('AWS::CloudWatch::Alarm', {
ExtendedStatistic: 'p99.9',
}));
});

test.done();
},
};

});
});

class TestAlarmAction implements IAlarmAction {
constructor(private readonly arn: string) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { expect, haveResource } from '@aws-cdk/assert-internal';
import '@aws-cdk/assert-internal/jest';
import { Stack } from '@aws-cdk/core';
import { Test } from 'nodeunit';
import { Alarm, AlarmRule, AlarmState, CompositeAlarm, Metric } from '../lib';

export = {
'test alarm rule expression builder'(test: Test) {
describe('CompositeAlarm', () => {
test('test alarm rule expression builder', () => {
const stack = new Stack();

const testMetric = new Metric({
Expand Down Expand Up @@ -60,7 +59,7 @@ export = {
alarmRule,
});

expect(stack).to(haveResource('AWS::CloudWatch::CompositeAlarm', {
expect(stack).toHaveResource('AWS::CloudWatch::CompositeAlarm', {
AlarmName: 'CompositeAlarm',
AlarmRule: {
'Fn::Join': [
Expand Down Expand Up @@ -105,9 +104,9 @@ export = {
],
],
},
}));
});


test.done();
},
});

};
});
Loading

0 comments on commit 8668e15

Please sign in to comment.