Skip to content

Commit d352300

Browse files
committed
simplify unit tests
1 parent 5604abc commit d352300

File tree

1 file changed

+50
-193
lines changed

1 file changed

+50
-193
lines changed

packages/@aws-cdk/aws-stepfunctions/test/task-base.test.ts

+50-193
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,36 @@
11
import '@aws-cdk/assert/jest';
2+
import { Metric } from '@aws-cdk/aws-cloudwatch';
23
import * as iam from '@aws-cdk/aws-iam';
34
import * as cdk from '@aws-cdk/core';
45
import * as sfn from '../lib';
56

67
describe('Task base', () => {
7-
test('instantiate a concrete implementation', () => {
8-
// GIVEN
9-
const stack = new cdk.Stack();
8+
let stack: cdk.Stack;
9+
let task: sfn.TaskStateBase;
1010

11+
beforeEach(() => {
12+
// GIVEN
13+
stack = new cdk.Stack();
14+
task = new FakeTask(stack, 'my-task', {
15+
metrics: {
16+
metricPrefixPlural: '',
17+
metricPrefixSingular: '',
18+
},
19+
});
20+
});
21+
test('instantiate a concrete implementation with properties', () => {
1122
// WHEN
12-
const task = new ConcreteTask(stack, 'my-task', {
23+
task = new FakeTask(stack, 'my-exciting-task', {
1324
comment: 'my exciting task',
1425
heartbeat: cdk.Duration.seconds(10),
1526
timeout: cdk.Duration.minutes(10),
1627
});
1728

1829
// THEN
1930
expect(render(task)).toEqual({
20-
StartAt: 'my-task',
31+
StartAt: 'my-exciting-task',
2132
States: {
22-
'my-task': {
33+
'my-exciting-task': {
2334
End: true,
2435
Type: 'Task',
2536
Comment: 'my exciting task',
@@ -34,8 +45,6 @@ describe('Task base', () => {
3445

3546
test('add catch configuration', () => {
3647
// GIVEN
37-
const stack = new cdk.Stack();
38-
const task = new ConcreteTask(stack, 'my-task');
3948
const failure = new sfn.Fail(stack, 'failed', {
4049
error: 'DidNotWork',
4150
cause: 'We got stuck',
@@ -68,10 +77,6 @@ describe('Task base', () => {
6877
});
6978

7079
test('add retry configuration', () => {
71-
// GIVEN
72-
const stack = new cdk.Stack();
73-
const task = new ConcreteTask(stack, 'my-task');
74-
7580
// WHEN
7681
task.addRetry({ errors: ['HTTPError'], maxAttempts: 2 })
7782
.addRetry(); // adds default retry
@@ -100,270 +105,107 @@ describe('Task base', () => {
100105
});
101106

102107
test('add a next state to the task in the chain', () => {
103-
// GIVEN
104-
const stack = new cdk.Stack();
105-
const task = new ConcreteTask(stack, 'mytask');
106-
107108
// WHEN
108109
task.next(new sfn.Pass(stack, 'passState'));
109110

110111
// THEN
111112
expect(render(task)).toEqual({
112-
StartAt: 'mytask',
113+
StartAt: 'my-task',
113114
States: {
114-
mytask: {
115+
'my-task': {
115116
Next: 'passState',
116117
Type: 'Task',
117118
Resource: 'my-resource',
118119
Parameters: { MyParameter: 'myParameter' },
119120
},
120-
passState: { Type: 'Pass', End: true },
121+
'passState': { Type: 'Pass', End: true },
121122
},
122123
});
123124
});
124125

125126
test('get named metric for this task', () => {
126-
// GIVEN
127-
const stack = new cdk.Stack();
128-
const task = new ConcreteTask(stack, 'mytask', {});
129-
130127
// WHEN
131-
const metric = stack.resolve(task.metric('my metric'));
128+
const metric = stack.resolve(task.metric('my-metric'));
132129

133130
// THEN
134-
expect(metric).toStrictEqual({
135-
period: {
136-
amount: 5,
137-
unit: {
138-
label: 'minutes',
139-
inMillis: 60000,
140-
},
141-
},
142-
namespace: 'AWS/States',
143-
metricName: 'my metric',
144-
statistic: 'Sum'});
131+
verifyMetric(metric, 'my-metric', 'Sum');
145132
});
146133

147134
test('add metric for task state run time', () => {
148-
// GIVEN
149-
const stack = new cdk.Stack();
150-
const taskMetrics: sfn.TaskMetricsConfig = { metricPrefixSingular: '' };
151-
const task = new ConcreteTask(stack, 'mytask', { metrics: taskMetrics });
152-
153135
// WHEN
154136
const metric = stack.resolve(task.metricRunTime());
155137

156138
// THEN
157-
expect(metric).toStrictEqual({
158-
period: {
159-
amount: 5,
160-
unit: {
161-
label: 'minutes',
162-
inMillis: 60000,
163-
},
164-
},
165-
namespace: 'AWS/States',
166-
metricName: 'RunTime',
167-
statistic: 'Average',
168-
});
139+
verifyMetric(metric, 'RunTime', 'Average');
169140
});
170141

171142
test('add metric for task schedule time', () => {
172-
// GIVEN
173-
const stack = new cdk.Stack();
174-
const taskMetrics: sfn.TaskMetricsConfig = { metricPrefixSingular: '' };
175-
const task = new ConcreteTask(stack, 'mytask', { metrics: taskMetrics });
176-
177143
// WHEN
178144
const metric = stack.resolve(task.metricScheduleTime());
179145

180146
// THEN
181-
expect(metric).toStrictEqual({
182-
period: {
183-
amount: 5,
184-
unit: {
185-
label: 'minutes',
186-
inMillis: 60000,
187-
},
188-
},
189-
namespace: 'AWS/States',
190-
metricName: 'ScheduleTime',
191-
statistic: 'Average',
192-
});
147+
verifyMetric(metric, 'ScheduleTime', 'Average');
193148
});
194149

195150
test('add metric for time between task being scheduled to closing', () => {
196-
// GIVEN
197-
const stack = new cdk.Stack();
198-
const taskMetrics: sfn.TaskMetricsConfig = { metricPrefixSingular: '' };
199-
const task = new ConcreteTask(stack, 'mytask', { metrics: taskMetrics });
200-
201151
// WHEN
202152
const metric = stack.resolve(task.metricTime());
203153

204154
// THEN
205-
expect(metric).toStrictEqual({
206-
period: {
207-
amount: 5,
208-
unit: {
209-
label: 'minutes',
210-
inMillis: 60000,
211-
},
212-
},
213-
namespace: 'AWS/States',
214-
metricName: 'Time',
215-
statistic: 'Average',
216-
});
155+
verifyMetric(metric, 'Time', 'Average');
217156
});
218157

219158
test('add metric for number of times the task is scheduled', () => {
220-
// GIVEN
221-
const stack = new cdk.Stack();
222-
const taskMetrics: sfn.TaskMetricsConfig = { metricPrefixPlural: '' };
223-
const task = new ConcreteTask(stack, 'mytask', { metrics: taskMetrics });
224-
225159
// WHEN
226160
const metric = stack.resolve(task.metricScheduled());
227161

228162
// THEN
229-
expect(metric).toStrictEqual({
230-
period: {
231-
amount: 5,
232-
unit: {
233-
label: 'minutes',
234-
inMillis: 60000,
235-
},
236-
},
237-
namespace: 'AWS/States',
238-
metricName: 'Scheduled',
239-
statistic: 'Sum',
240-
});
163+
verifyMetric(metric, 'Scheduled', 'Sum');
241164
});
242165

243166
test('add metric for number of times the task times out', () => {
244-
// GIVEN
245-
const stack = new cdk.Stack();
246-
const taskMetrics: sfn.TaskMetricsConfig = { metricPrefixPlural: '' };
247-
const task = new ConcreteTask(stack, 'mytask', { metrics: taskMetrics });
248-
249167
// WHEN
250168
const metric = stack.resolve(task.metricTimedOut());
251169

252170
// THEN
253-
expect(metric).toStrictEqual({
254-
period: {
255-
amount: 5,
256-
unit: {
257-
label: 'minutes',
258-
inMillis: 60000,
259-
},
260-
},
261-
namespace: 'AWS/States',
262-
metricName: 'TimedOut',
263-
statistic: 'Sum',
264-
});
171+
verifyMetric(metric, 'TimedOut', 'Sum');
265172
});
266173

267174
test('add metric for number of times the task was started', () => {
268-
// GIVEN
269-
const stack = new cdk.Stack();
270-
const taskMetrics: sfn.TaskMetricsConfig = { metricPrefixPlural: '' };
271-
const task = new ConcreteTask(stack, 'mytask', { metrics: taskMetrics });
272-
273175
// WHEN
274176
const metric = stack.resolve(task.metricStarted());
275177

276178
// THEN
277-
expect(metric).toStrictEqual({
278-
period: {
279-
amount: 5,
280-
unit: {
281-
label: 'minutes',
282-
inMillis: 60000,
283-
},
284-
},
285-
namespace: 'AWS/States',
286-
metricName: 'Started',
287-
statistic: 'Sum',
288-
});
179+
verifyMetric(metric, 'Started', 'Sum');
289180
});
290181

291182
test('add metric for number of times the task succeeded', () => {
292-
// GIVEN
293-
const stack = new cdk.Stack();
294-
const taskMetrics: sfn.TaskMetricsConfig = { metricPrefixPlural: '' };
295-
const task = new ConcreteTask(stack, 'mytask', { metrics: taskMetrics });
296-
297183
// WHEN
298184
const metric = stack.resolve(task.metricSucceeded());
299185

300186
// THEN
301-
expect(metric).toStrictEqual({
302-
period: {
303-
amount: 5,
304-
unit: {
305-
label: 'minutes',
306-
inMillis: 60000,
307-
},
308-
},
309-
namespace: 'AWS/States',
310-
metricName: 'Succeeded',
311-
statistic: 'Sum',
312-
});
187+
verifyMetric(metric, 'Succeeded', 'Sum');
313188
});
314189

315190
test('add metric for number of times the task failed', () => {
316-
// GIVEN
317-
const stack = new cdk.Stack();
318-
const taskMetrics: sfn.TaskMetricsConfig = { metricPrefixPlural: '' };
319-
const task = new ConcreteTask(stack, 'mytask', { metrics: taskMetrics });
320-
321191
// WHEN
322192
const metric = stack.resolve(task.metricFailed());
323193

324194
// THEN
325-
expect(metric).toStrictEqual({
326-
period: {
327-
amount: 5,
328-
unit: {
329-
label: 'minutes',
330-
inMillis: 60000,
331-
},
332-
},
333-
namespace: 'AWS/States',
334-
metricName: 'Failed',
335-
statistic: 'Sum',
336-
});
195+
verifyMetric(metric, 'Failed', 'Sum');
337196
});
338197

339198
test('add metric for number of times the metrics heartbeat timed out', () => {
340-
// GIVEN
341-
const stack = new cdk.Stack();
342-
const taskMetrics: sfn.TaskMetricsConfig = { metricPrefixPlural: '' };
343-
const task = new ConcreteTask(stack, 'mytask', { metrics: taskMetrics });
344-
345199
// WHEN
346200
const metric = stack.resolve(task.metricHeartbeatTimedOut());
347201

348202
// THEN
349-
expect(metric).toStrictEqual({
350-
period: {
351-
amount: 5,
352-
unit: {
353-
label: 'minutes',
354-
inMillis: 60000,
355-
},
356-
},
357-
namespace: 'AWS/States',
358-
metricName: 'HeartbeatTimedOut',
359-
statistic: 'Sum',
360-
});
203+
verifyMetric(metric, 'HeartbeatTimedOut', 'Sum');
361204
});
362205

363206
test('metrics must be configured to use metric* APIs', () => {
364207
// GIVEN
365-
const stack = new cdk.Stack();
366-
const task = new ConcreteTask(stack, 'mytask', {});
208+
task = new FakeTask(stack, 'mytask', {});
367209

368210
// THEN
369211
expect(() => {
@@ -422,21 +264,36 @@ describe('Task base', () => {
422264
});
423265
});
424266

267+
function verifyMetric(metric: Metric, metricName: string, statistic: string) {
268+
expect(metric).toStrictEqual({
269+
period: {
270+
amount: 5,
271+
unit: {
272+
label: 'minutes',
273+
inMillis: 60000,
274+
},
275+
},
276+
namespace: 'AWS/States',
277+
metricName,
278+
statistic,
279+
});
280+
}
281+
425282
function render(sm: sfn.IChainable) {
426283
return new cdk.Stack().resolve(
427284
new sfn.StateGraph(sm.startState, 'Test Graph').toGraphJson(),
428285
);
429286
}
430287

431-
interface ConcreteTaskProps extends sfn.TaskStateBaseProps {
288+
interface FakeTaskProps extends sfn.TaskStateBaseProps {
432289
readonly metrics?: sfn.TaskMetricsConfig;
433290
}
434291

435-
class ConcreteTask extends sfn.TaskStateBase {
292+
class FakeTask extends sfn.TaskStateBase {
436293
protected readonly taskMetrics?: sfn.TaskMetricsConfig;
437294
protected readonly taskPolicies?: iam.PolicyStatement[];
438295

439-
constructor(scope: cdk.Construct, id: string, props: ConcreteTaskProps = {}) {
296+
constructor(scope: cdk.Construct, id: string, props: FakeTaskProps = {}) {
440297
super(scope, id, props);
441298
this.taskMetrics = props.metrics;
442299
}

0 commit comments

Comments
 (0)