1
- /* eslint-disable @typescript-eslint/no-non-null-assertion */
1
+ import * as assert from 'node:assert'
2
+
2
3
import {
3
4
Envelope ,
4
5
getWorstTestStepResult ,
6
+ Pickle ,
5
7
TestCase ,
6
8
TestCaseFinished ,
7
9
TestCaseStarted ,
@@ -15,48 +17,82 @@ import { Query as CucumberQuery } from '@cucumber/query'
15
17
export class ExtendedQuery extends CucumberQuery {
16
18
private testRunStarted : TestRunStarted
17
19
private testRunFinished : TestRunFinished
20
+ private testCaseStarted : Array < TestCaseStarted > = [ ]
21
+ private readonly pickleById : Map < string , Pickle > = new Map ( )
18
22
private readonly testCaseById : Map < string , TestCase > = new Map ( )
19
- private readonly testCaseStartedById : Map < string , TestCaseStarted > = new Map ( )
20
23
private readonly testCaseFinishedByTestCaseStartedId : Map < string , TestCaseFinished > = new Map ( )
21
- private readonly finalAttemptByTestCaseId : Map <
22
- string ,
23
- [ TestCase , TestCaseStarted , TestCaseFinished ]
24
- > = new Map ( )
25
24
26
25
update ( envelope : Envelope ) {
27
26
super . update ( envelope )
28
27
28
+ if ( envelope . pickle ) {
29
+ this . pickleById . set ( envelope . pickle . id , envelope . pickle )
30
+ }
29
31
if ( envelope . testRunStarted ) {
30
32
this . testRunStarted = envelope . testRunStarted
31
33
}
32
34
if ( envelope . testCase ) {
33
35
this . testCaseById . set ( envelope . testCase . id , envelope . testCase )
34
36
}
35
37
if ( envelope . testCaseStarted ) {
36
- this . testCaseStartedById . set ( envelope . testCaseStarted . id , envelope . testCaseStarted )
38
+ this . updateTestCaseStarted ( envelope . testCaseStarted )
37
39
}
38
40
if ( envelope . testCaseFinished ) {
39
- this . testCaseFinishedByTestCaseStartedId . set (
40
- envelope . testCaseFinished . testCaseStartedId ,
41
- envelope . testCaseFinished
42
- )
43
- if ( ! envelope . testCaseFinished . willBeRetried ) {
44
- const testCaseStarted = this . testCaseStartedById . get (
45
- envelope . testCaseFinished . testCaseStartedId
46
- ) !
47
- const testCase = this . testCaseById . get ( testCaseStarted . testCaseId ) !
48
- this . finalAttemptByTestCaseId . set ( testCase . id , [
49
- testCase ,
50
- testCaseStarted ,
51
- envelope . testCaseFinished ,
52
- ] )
53
- }
41
+ this . updateTestCaseFinished ( envelope . testCaseFinished )
54
42
}
55
43
if ( envelope . testRunFinished ) {
56
44
this . testRunFinished = envelope . testRunFinished
57
45
}
58
46
}
59
47
48
+ private updateTestCaseStarted ( testCaseStarted : TestCaseStarted ) {
49
+ // ensure this replaces any previous attempt for the same test case
50
+ this . testCaseStarted = [
51
+ ...this . testCaseStarted . filter (
52
+ ( existing ) => existing . testCaseId !== testCaseStarted . testCaseId
53
+ ) ,
54
+ testCaseStarted ,
55
+ ]
56
+ }
57
+
58
+ private updateTestCaseFinished ( testCaseFinished : TestCaseFinished ) {
59
+ this . testCaseFinishedByTestCaseStartedId . set (
60
+ testCaseFinished . testCaseStartedId ,
61
+ testCaseFinished
62
+ )
63
+ }
64
+
65
+ findPickleBy ( testCaseStarted : TestCaseStarted ) {
66
+ const testCase = this . findTestCaseBy ( testCaseStarted )
67
+ if ( ! testCase ) {
68
+ return undefined
69
+ }
70
+ return this . pickleById . get ( testCase . pickleId )
71
+ }
72
+
73
+ findAllTestCaseStarted ( ) : ReadonlyArray < TestCaseStarted > {
74
+ return [ ...this . testCaseStarted ]
75
+ }
76
+
77
+ findTestCaseBy ( testCaseStarted : TestCaseStarted ) {
78
+ return this . testCaseById . get ( testCaseStarted . testCaseId )
79
+ }
80
+
81
+ findTestCaseFinishedBy ( testCaseStarted : TestCaseStarted ) {
82
+ return this . testCaseFinishedByTestCaseStartedId . get ( testCaseStarted . id )
83
+ }
84
+
85
+ findTestCaseDurationBy ( testCaseStarted : TestCaseStarted ) {
86
+ const testCaseFinished = this . findTestCaseFinishedBy ( testCaseStarted )
87
+ if ( ! testCaseFinished ) {
88
+ return undefined
89
+ }
90
+ return TimeConversion . millisecondsToDuration (
91
+ TimeConversion . timestampToMillisecondsSinceEpoch ( testCaseFinished . timestamp ) -
92
+ TimeConversion . timestampToMillisecondsSinceEpoch ( testCaseStarted . timestamp )
93
+ )
94
+ }
95
+
60
96
findTestRunDuration ( ) {
61
97
if ( ! this . testRunStarted || ! this . testRunFinished ) {
62
98
return undefined
@@ -77,7 +113,9 @@ export class ExtendedQuery extends CucumberQuery {
77
113
[ TestStepResultStatus . UNDEFINED ] : 0 ,
78
114
[ TestStepResultStatus . UNKNOWN ] : 0 ,
79
115
}
80
- for ( const [ testCase ] of this . finalAttemptByTestCaseId . values ( ) ) {
116
+ for ( const testCaseStarted of this . testCaseStarted ) {
117
+ const testCase = this . findTestCaseBy ( testCaseStarted )
118
+ assert . ok ( testCase , 'Expected to find TestCase for TestCaseStarted' )
81
119
const statusesFromSteps = testCase . testSteps . map ( ( testStep ) => {
82
120
return getWorstTestStepResult ( this . getTestStepResults ( testStep . id ) )
83
121
} )
0 commit comments