-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
test_case_runner.ts
352 lines (332 loc) · 11.4 KB
/
test_case_runner.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import { getAmbiguousStepException } from './helpers'
import AttachmentManager from './attachment_manager'
import StepRunner from './step_runner'
import * as messages from '@cucumber/messages'
import { getWorstTestStepResult, IdGenerator } from '@cucumber/messages'
import { EventEmitter } from 'events'
import {
ISupportCodeLibrary,
ITestCaseHookParameter,
ITestStepHookParameter,
} from '../support_code_library_builder/types'
import TestCaseHookDefinition from '../models/test_case_hook_definition'
import TestStepHookDefinition from '../models/test_step_hook_definition'
import { IDefinition } from '../models/definition'
import { doesHaveValue, doesNotHaveValue } from '../value_checker'
import { ITestRunStopwatch } from './stopwatch'
import StepDefinition from '../models/step_definition'
export interface INewTestCaseRunnerOptions {
eventBroadcaster: EventEmitter
stopwatch: ITestRunStopwatch
gherkinDocument: messages.GherkinDocument
newId: IdGenerator.NewId
pickle: messages.Pickle
testCase: messages.TestCase
retries: number
skip: boolean
supportCodeLibrary: ISupportCodeLibrary
worldParameters: any
}
export default class TestCaseRunner {
private readonly attachmentManager: AttachmentManager
private currentTestCaseStartedId: string
private currentTestStepId: string
private readonly eventBroadcaster: EventEmitter
private readonly stopwatch: ITestRunStopwatch
private readonly gherkinDocument: messages.GherkinDocument
private readonly newId: IdGenerator.NewId
private readonly pickle: messages.Pickle
private readonly testCase: messages.TestCase
private readonly maxAttempts: number
private readonly skip: boolean
private readonly supportCodeLibrary: ISupportCodeLibrary
private testStepResults: messages.TestStepResult[]
private world: any
private readonly worldParameters: any
constructor({
eventBroadcaster,
stopwatch,
gherkinDocument,
newId,
pickle,
testCase,
retries = 0,
skip,
supportCodeLibrary,
worldParameters,
}: INewTestCaseRunnerOptions) {
this.attachmentManager = new AttachmentManager(({ data, media }) => {
if (doesNotHaveValue(this.currentTestStepId)) {
throw new Error(
'Cannot attach when a step/hook is not running. Ensure your step/hook waits for the attach to finish.'
)
}
const attachment: messages.Envelope = {
attachment: {
body: data,
contentEncoding: media.encoding,
mediaType: media.contentType,
testCaseStartedId: this.currentTestCaseStartedId,
testStepId: this.currentTestStepId,
},
}
this.eventBroadcaster.emit('envelope', attachment)
})
this.eventBroadcaster = eventBroadcaster
this.stopwatch = stopwatch
this.gherkinDocument = gherkinDocument
this.maxAttempts = 1 + (skip ? 0 : retries)
this.newId = newId
this.pickle = pickle
this.testCase = testCase
this.skip = skip
this.supportCodeLibrary = supportCodeLibrary
this.worldParameters = worldParameters
this.resetTestProgressData()
}
resetTestProgressData(): void {
this.world = new this.supportCodeLibrary.World({
attach: this.attachmentManager.create.bind(this.attachmentManager),
log: this.attachmentManager.log.bind(this.attachmentManager),
parameters: this.worldParameters,
})
this.testStepResults = []
}
getBeforeStepHookDefinitions(): TestStepHookDefinition[] {
return this.supportCodeLibrary.beforeTestStepHookDefinitions.filter(
(hookDefinition) => hookDefinition.appliesToTestCase(this.pickle)
)
}
getAfterStepHookDefinitions(): TestStepHookDefinition[] {
return this.supportCodeLibrary.afterTestStepHookDefinitions
.slice(0)
.reverse()
.filter((hookDefinition) => hookDefinition.appliesToTestCase(this.pickle))
}
getWorstStepResult(): messages.TestStepResult {
if (this.testStepResults.length === 0) {
return {
status: this.skip
? messages.TestStepResultStatus.SKIPPED
: messages.TestStepResultStatus.PASSED,
duration: messages.TimeConversion.millisecondsToDuration(0),
}
}
return getWorstTestStepResult(this.testStepResults)
}
async invokeStep(
step: messages.PickleStep,
stepDefinition: IDefinition,
hookParameter?: any
): Promise<messages.TestStepResult> {
return await StepRunner.run({
defaultTimeout: this.supportCodeLibrary.defaultTimeout,
hookParameter,
step,
stepDefinition,
world: this.world,
})
}
isSkippingSteps(): boolean {
return (
this.getWorstStepResult().status !== messages.TestStepResultStatus.PASSED
)
}
shouldSkipHook(isBeforeHook: boolean): boolean {
return this.skip || (this.isSkippingSteps() && isBeforeHook)
}
async aroundTestStep(
testStepId: string,
runStepFn: () => Promise<messages.TestStepResult>
): Promise<void> {
const testStepStarted: messages.Envelope = {
testStepStarted: {
testCaseStartedId: this.currentTestCaseStartedId,
testStepId,
timestamp: this.stopwatch.timestamp(),
},
}
this.eventBroadcaster.emit('envelope', testStepStarted)
this.currentTestStepId = testStepId
const testStepResult = await runStepFn()
this.currentTestStepId = null
this.testStepResults.push(testStepResult)
const testStepFinished: messages.Envelope = {
testStepFinished: {
testCaseStartedId: this.currentTestCaseStartedId,
testStepId,
testStepResult,
timestamp: this.stopwatch.timestamp(),
},
}
this.eventBroadcaster.emit('envelope', testStepFinished)
}
async run(): Promise<messages.TestStepResultStatus> {
for (let attempt = 0; attempt < this.maxAttempts; attempt++) {
const moreAttemptsRemaining = attempt + 1 < this.maxAttempts
this.currentTestCaseStartedId = this.newId()
const testCaseStarted: messages.Envelope = {
testCaseStarted: {
attempt,
testCaseId: this.testCase.id,
id: this.currentTestCaseStartedId,
timestamp: this.stopwatch.timestamp(),
},
}
this.eventBroadcaster.emit('envelope', testCaseStarted)
// used to determine whether a hook is a Before or After
let didWeRunStepsYet = false
for (const testStep of this.testCase.testSteps) {
await this.aroundTestStep(testStep.id, async () => {
if (doesHaveValue(testStep.hookId)) {
const hookParameter: ITestCaseHookParameter = {
gherkinDocument: this.gherkinDocument,
pickle: this.pickle,
testCaseStartedId: this.currentTestCaseStartedId,
}
if (didWeRunStepsYet) {
hookParameter.result = this.getWorstStepResult()
hookParameter.willBeRetried =
this.getWorstStepResult().status ===
messages.TestStepResultStatus.FAILED && moreAttemptsRemaining
}
return await this.runHook(
findHookDefinition(testStep.hookId, this.supportCodeLibrary),
hookParameter,
!didWeRunStepsYet
)
} else {
const pickleStep = this.pickle.steps.find(
(pickleStep) => pickleStep.id === testStep.pickleStepId
)
const testStepResult = await this.runStep(pickleStep, testStep)
didWeRunStepsYet = true
return testStepResult
}
})
}
const willBeRetried =
this.getWorstStepResult().status ===
messages.TestStepResultStatus.FAILED && moreAttemptsRemaining
const testCaseFinished: messages.Envelope = {
testCaseFinished: {
testCaseStartedId: this.currentTestCaseStartedId,
timestamp: this.stopwatch.timestamp(),
willBeRetried,
},
}
this.eventBroadcaster.emit('envelope', testCaseFinished)
if (!willBeRetried) {
break
}
this.resetTestProgressData()
}
return this.getWorstStepResult().status
}
async runHook(
hookDefinition: TestCaseHookDefinition,
hookParameter: ITestCaseHookParameter,
isBeforeHook: boolean
): Promise<messages.TestStepResult> {
if (this.shouldSkipHook(isBeforeHook)) {
return {
status: messages.TestStepResultStatus.SKIPPED,
duration: messages.TimeConversion.millisecondsToDuration(0),
}
}
return await this.invokeStep(null, hookDefinition, hookParameter)
}
async runStepHooks(
stepHooks: TestStepHookDefinition[],
pickleStep: messages.PickleStep,
stepResult?: messages.TestStepResult
): Promise<messages.TestStepResult[]> {
const stepHooksResult = []
const hookParameter: ITestStepHookParameter = {
gherkinDocument: this.gherkinDocument,
pickle: this.pickle,
pickleStep,
testCaseStartedId: this.currentTestCaseStartedId,
testStepId: this.currentTestStepId,
result: stepResult,
}
for (const stepHookDefinition of stepHooks) {
stepHooksResult.push(
await this.invokeStep(null, stepHookDefinition, hookParameter)
)
}
return stepHooksResult
}
async runStep(
pickleStep: messages.PickleStep,
testStep: messages.TestStep
): Promise<messages.TestStepResult> {
const stepDefinitions = testStep.stepDefinitionIds.map(
(stepDefinitionId) => {
return findStepDefinition(stepDefinitionId, this.supportCodeLibrary)
}
)
if (stepDefinitions.length === 0) {
return {
status: messages.TestStepResultStatus.UNDEFINED,
duration: messages.TimeConversion.millisecondsToDuration(0),
}
} else if (stepDefinitions.length > 1) {
return {
message: getAmbiguousStepException(stepDefinitions),
status: messages.TestStepResultStatus.AMBIGUOUS,
duration: messages.TimeConversion.millisecondsToDuration(0),
}
} else if (this.isSkippingSteps()) {
return {
status: messages.TestStepResultStatus.SKIPPED,
duration: messages.TimeConversion.millisecondsToDuration(0),
}
}
let stepResult
let stepResults = await this.runStepHooks(
this.getBeforeStepHookDefinitions(),
pickleStep
)
if (
getWorstTestStepResult(stepResults).status !==
messages.TestStepResultStatus.FAILED
) {
stepResult = await this.invokeStep(pickleStep, stepDefinitions[0])
stepResults.push(stepResult)
}
const afterStepHookResults = await this.runStepHooks(
this.getAfterStepHookDefinitions(),
pickleStep,
stepResult
)
stepResults = stepResults.concat(afterStepHookResults)
const finalStepResult = getWorstTestStepResult(stepResults)
let finalDuration = messages.TimeConversion.millisecondsToDuration(0)
for (const result of stepResults) {
finalDuration = messages.TimeConversion.addDurations(
finalDuration,
result.duration
)
}
finalStepResult.duration = finalDuration
return finalStepResult
}
}
function findHookDefinition(
id: string,
supportCodeLibrary: ISupportCodeLibrary
): TestCaseHookDefinition {
return [
...supportCodeLibrary.beforeTestCaseHookDefinitions,
...supportCodeLibrary.afterTestCaseHookDefinitions,
].find((definition) => definition.id === id)
}
function findStepDefinition(
id: string,
supportCodeLibrary: ISupportCodeLibrary
): StepDefinition {
return supportCodeLibrary.stepDefinitions.find(
(definition) => definition.id === id
)
}