@@ -38,23 +38,17 @@ let createProperty=(name, value)=>{
38
38
convertNameToId = ( obj ) => {
39
39
return obj . replace ( / / g, '-' ) . toLowerCase ( ) ;
40
40
} ;
41
-
42
-
43
41
44
42
class JUnitFormatter extends Formatter {
45
43
/** @param {Options } options */
46
44
constructor ( options ) {
47
45
super ( options ) ;
48
46
this . _result = [ ] ;
49
-
50
- options . eventBroadcaster . on ( 'test-run-started' , ( ) => {
51
- this . _result = [ ] ;
52
- } ) ;
53
-
54
- options . eventBroadcaster . on ( 'test-case-finished' , ( { sourceLocation } ) => {
47
+
48
+ let scenarioAsSuite = ( { sourceLocation } ) => {
55
49
const { gherkinDocument, pickle, testCase } = options . eventDataCollector . getTestCaseData ( sourceLocation ) ;
56
50
let testSuiteId = `${ convertNameToId ( gherkinDocument . feature . name ) } ;${ convertNameToId ( pickle . name ) } ` ;
57
- let attr = { name :testSuiteId , tests :0 , failures :0 , skipped :0 , errors :0 , time :( ( testCase . result . duration || 0 ) / 1000 ) . toFixed ( 3 ) } ,
51
+ let attr = { name :testSuiteId , tests :0 , failures :0 , skipped :0 , errors :0 , time :testCase . result . duration || 0 } ,
58
52
testSuite = [ { _attr :attr } ] ;
59
53
60
54
if ( options . withPackage ) {
@@ -102,6 +96,10 @@ class JUnitFormatter extends Formatter {
102
96
testCase . push ( { skipped : [ ] } ) ;
103
97
attr . skipped += 1 ;
104
98
break ;
99
+ case 'ambiguous' :
100
+ testCase . push ( createFailureOrError ( "error" , result . exception ) ) ;
101
+ attr . errors += 1 ;
102
+ break ;
105
103
default :
106
104
break ;
107
105
// testCase.push(createFailure(`Unknown status - ${step.result.status}`));
@@ -112,10 +110,82 @@ class JUnitFormatter extends Formatter {
112
110
} ) ;
113
111
this . _result . push ( { testsuite :testSuite } ) ;
114
112
113
+ } ;
114
+
115
+ let scenarioAsStep = ( { sourceLocation } ) => {
116
+ const { gherkinDocument, pickle, testCase } = options . eventDataCollector . getTestCaseData ( sourceLocation ) ;
117
+ let testSuiteId = `${ convertNameToId ( gherkinDocument . feature . name ) } ` ,
118
+ testCaseId = `${ convertNameToId ( pickle . name ) } ` ;
119
+ let attr , testSuite ;
120
+ if ( this . _result . length && this . _result [ this . _result . length - 1 ] . testsuite [ 0 ] . _attr . name === testSuiteId ) {
121
+ testSuite = this . _result [ this . _result . lenght - 1 ] . testsuite ;
122
+ attr = testSuite [ 0 ] . _attr ;
123
+ }
124
+ else {
125
+ attr = { name :testSuiteId , tests :0 , failures :0 , skipped :0 , errors :0 , time :0 } ;
126
+ testSuite = [ { _attr :attr } ] ;
127
+ this . _result . push ( { testsuite :testSuite } ) ;
128
+ }
129
+ attr . time += testCase . result . duration ;
130
+ attr . tests += 1 ;
131
+ let testCaseTag = [
132
+ {
133
+ _attr :{
134
+ classname :testCaseId ,
135
+ name :pickle . name ,
136
+ time :( ( testCase . result . duration || 0 ) / 1000 ) . toFixed ( 3 )
137
+ }
138
+ }
139
+ ] ;
140
+
141
+ testCase . steps . every ( ( step , index ) => {
142
+ const { gherkinKeyword, pickleStep } = options . eventDataCollector . getTestStepData ( { testCase :testCase , index :index } ) ;
143
+ if ( gherkinKeyword || ( step . result && step . result . status === 'failed' ) ) {
144
+ let result = step . result || { } ;
145
+ switch ( result . status ) {
146
+ case 'passed' :
147
+ break ;
148
+ case 'failed' :
149
+ testCaseTag . push ( createFailureOrError ( gherkinKeyword ?"failure" :"error" , result . exception ) ) ;
150
+ attr [ gherkinKeyword ?"failures" :"errors" ] += 1 ;
151
+ return false ;
152
+ case 'pending' :
153
+ case 'undefined' :
154
+ testCaseTag . push ( createFailure ( result . status === 'pending' ? 'Pending'
155
+ : `Undefined step. Implement with the following snippet:\n ${ gherkinKeyword . trim ( ) } (/^${ pickleStep . text } $/, function(callback) {\n // Write code here that turns the phrase above into concrete actions\n callback(null, 'pending');\n });`
156
+ ) ) ;
157
+ attr . failures += 1 ;
158
+ return false ;
159
+ case 'skipped' :
160
+ testCaseTag . push ( { skipped : [ ] } ) ;
161
+ attr . skipped += 1 ;
162
+ return false ;
163
+ case 'ambiguous' :
164
+ testCaseTag . push ( createFailureOrError ( "error" , result . exception ) ) ;
165
+ attr . errors += 1 ;
166
+ return false ;
167
+ default :
168
+ break ;
169
+ // testCase.push(createFailure(`Unknown status - ${step.result.status}`));
170
+ }
171
+ }
172
+ return true ;
173
+ } ) ;
174
+ testSuite . push ( { testcase :testCaseTag } ) ;
175
+
176
+ } ;
177
+
178
+ options . eventBroadcaster . on ( 'test-run-started' , ( ) => {
179
+ this . _result = [ ] ;
115
180
} ) ;
116
181
182
+ options . eventBroadcaster . on ( 'test-case-finished' , options . scenarioAsStep ?scenarioAsStep :scenarioAsSuite ) ;
183
+
117
184
118
185
options . eventBroadcaster . on ( 'test-run-finished' , ( ) => {
186
+ this . _result . forEach ( ( testSuite ) => {
187
+ testSuite . testsuite [ 0 ] . _attr . time = ( testSuite . testsuite [ 0 ] . _attr . time / 1000 ) . toFixed ( 3 ) ;
188
+ } ) ;
119
189
this . log ( xml ( { testsuites : this . _result } , { indent :' ' , declaration : { encoding : 'UTF-8' } } ) ) ;
120
190
} ) ;
121
191
}
0 commit comments