|
1 |
| -const { Formatter, formatterHelpers } = require('cucumber'); |
| 1 | +const {Formatter} = require('cucumber'); |
2 | 2 | const xml = require('xml');
|
3 |
| - |
4 |
| -// Next two methods taken from https://github.com/stjohnjohnson/cucumber-junit/blob/master/lib/cucumber_junit.js |
5 |
| -let createProperty=(name, value)=>{ |
6 |
| - return { |
7 |
| - property: [ |
8 |
| - { |
9 |
| - _attr: { |
10 |
| - name: name, |
11 |
| - value: value |
12 |
| - } |
13 |
| - } |
14 |
| - ] |
15 |
| - }; |
16 |
| - }, |
17 |
| - |
18 |
| - createFailure=(message)=>{ |
19 |
| - return { |
20 |
| - failure: [ |
21 |
| - { _attr: { message: message.split("\n").shift() } }, |
22 |
| - message |
23 |
| - ] |
24 |
| - }; |
25 |
| - }, |
26 |
| - |
27 |
| - createFailureOrError=(type,exception)=>{ |
28 |
| - let {name}=exception, |
29 |
| - ret ={}; |
30 |
| - ret[type]=[ |
31 |
| - { _attr: { message: name } }, |
32 |
| - formatterHelpers.formatError(exception) |
33 |
| - ]; |
34 |
| - return ret; |
35 |
| - }, |
36 |
| - |
37 |
| - // Next one from original json_formatter |
38 |
| - convertNameToId=(obj)=>{ |
39 |
| - return obj.replace(/ /g, '-').toLowerCase(); |
40 |
| - }; |
| 3 | +const {scenarioAsStep,scenarioAsSuite}=require("./before6"); |
41 | 4 |
|
42 | 5 | class JUnitFormatter extends Formatter {
|
43 | 6 | /** @param {Options} options */
|
44 | 7 | constructor(options) {
|
45 | 8 | super(options);
|
46 | 9 | this._result=[];
|
47 |
| - |
48 |
| - let scenarioAsSuite=({ sourceLocation })=>{ |
49 |
| - const { gherkinDocument, pickle, testCase } = options.eventDataCollector.getTestCaseData(sourceLocation); |
50 |
| - let testSuiteId=`${convertNameToId(gherkinDocument.feature.name)};${convertNameToId(pickle.name)}`; |
51 |
| - let attr={name:testSuiteId,tests:0,failures:0,skipped:0,errors:0,time:testCase.result.duration||0}, |
52 |
| - testSuite=[{_attr:attr}]; |
53 |
| - |
54 |
| - if (options.withPackage) { |
55 |
| - attr.package=convertNameToId(pickle.name); |
56 |
| - } |
57 |
| - |
58 |
| - if (pickle.tags.length) { |
59 |
| - testSuite.push({properties:pickle.tags.map(tag=>createProperty("tag",tag.name))}); |
60 |
| - } |
61 |
| - |
62 |
| - testCase.steps.forEach((step,index)=>{ |
63 |
| - const {gherkinKeyword, pickleStep } = options.eventDataCollector.getTestStepData({testCase:testCase,index:index}); |
64 |
| - if (gherkinKeyword || (step.result && step.result.status==='failed')) { |
65 |
| - let result=step.result || {}; |
66 |
| - let testCaseId=convertNameToId(gherkinKeyword?pickleStep.text:`${pickle.name} ${index?'after':'before'}`); |
67 |
| - let testCase=[ |
68 |
| - { |
69 |
| - _attr:{ |
70 |
| - classname:testCaseId, |
71 |
| - name:gherkinKeyword?pickleStep.text:(pickle.name+(index?' after':' before')), |
72 |
| - time:((result.duration || 0)/1000).toFixed(3) |
73 |
| - } |
74 |
| - } |
75 |
| - ]; |
76 |
| - switch (result.status) { |
77 |
| - case 'passed': |
78 |
| - break; |
79 |
| - case 'failed': |
80 |
| - testCase.push(createFailureOrError(gherkinKeyword?"failure":"error",result.exception)); |
81 |
| - if (gherkinKeyword) { |
82 |
| - attr.failures+=1; |
83 |
| - } |
84 |
| - else { |
85 |
| - attr.errors+=1; |
86 |
| - } |
87 |
| - break; |
88 |
| - case 'pending': |
89 |
| - case 'undefined': |
90 |
| - testCase.push(createFailure(result.status === 'pending' ? 'Pending' |
91 |
| - : `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 });` |
92 |
| - )); |
93 |
| - attr.failures+=1; |
94 |
| - break; |
95 |
| - case 'skipped': |
96 |
| - testCase.push({skipped: []}); |
97 |
| - attr.skipped+=1; |
98 |
| - break; |
99 |
| - case 'ambiguous': |
100 |
| - testCase.push(createFailureOrError("error",result.exception)); |
101 |
| - attr.errors+=1; |
102 |
| - break; |
103 |
| - default: |
104 |
| - break; |
105 |
| -// testCase.push(createFailure(`Unknown status - ${step.result.status}`)); |
106 |
| - } |
107 |
| - attr.tests+=1; |
108 |
| - testSuite.push({testcase:testCase}); |
109 |
| - } |
110 |
| - }); |
111 |
| - this._result.push({testsuite:testSuite}); |
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.length-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 |
| - if (pickle.tags.length && options.propertiesInTestcase) { |
142 |
| - testCaseTag.push({properties:pickle.tags.map(tag=>createProperty("tag",tag.name))}); |
143 |
| - } |
144 |
| - |
145 |
| - testCase.steps.every((step,index)=>{ |
146 |
| - const {gherkinKeyword, pickleStep } = options.eventDataCollector.getTestStepData({testCase:testCase,index:index}); |
147 |
| - if (gherkinKeyword || (step.result && step.result.status==='failed')) { |
148 |
| - let result=step.result || {}; |
149 |
| - switch (result.status) { |
150 |
| - case 'passed': |
151 |
| - break; |
152 |
| - case 'failed': |
153 |
| - testCaseTag.push(createFailureOrError(gherkinKeyword?"failure":"error",result.exception)); |
154 |
| - attr[gherkinKeyword?"failures":"errors"]+=1; |
155 |
| - return false; |
156 |
| - case 'pending': |
157 |
| - case 'undefined': |
158 |
| - testCaseTag.push(createFailure(result.status === 'pending' ? 'Pending' |
159 |
| - : `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 });` |
160 |
| - )); |
161 |
| - attr.failures+=1; |
162 |
| - return false; |
163 |
| - case 'skipped': |
164 |
| - testCaseTag.push({skipped: []}); |
165 |
| - attr.skipped+=1; |
166 |
| - return false; |
167 |
| - case 'ambiguous': |
168 |
| - testCaseTag.push(createFailureOrError("error",result.exception)); |
169 |
| - attr.errors+=1; |
170 |
| - return false; |
171 |
| - default: |
172 |
| - break; |
173 |
| -// testCase.push(createFailure(`Unknown status - ${step.result.status}`)); |
174 |
| - } |
175 |
| - } |
176 |
| - return true; |
177 |
| - }); |
178 |
| - testSuite.push({testcase:testCaseTag}); |
179 |
| - |
180 |
| - }; |
181 | 10 |
|
182 | 11 | options.eventBroadcaster.on('test-run-started', ()=>{
|
183 | 12 | this._result=[];
|
184 | 13 | });
|
185 | 14 |
|
186 |
| - options.eventBroadcaster.on('test-case-finished', options.scenarioAsStep?scenarioAsStep:scenarioAsSuite); |
| 15 | + options.eventBroadcaster.on('test-case-finished', (options.scenarioAsStep?scenarioAsStep:scenarioAsSuite)(options,this._result)); |
187 | 16 |
|
188 | 17 |
|
189 | 18 | options.eventBroadcaster.on('test-run-finished', ()=>{
|
|
0 commit comments