Skip to content

Commit be74f34

Browse files
committed
Refactor code before cucumber version 6 support
1 parent d2bf4d5 commit be74f34

File tree

3 files changed

+189
-174
lines changed

3 files changed

+189
-174
lines changed

before6.js

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/** Module for cucumber-junit-formatter before cucumber 6.0.0 */
2+
3+
"use strict;";
4+
5+
const {createProperty,createFailure,createFailureOrError,convertNameToId}=require("./util");
6+
7+
module.exports={
8+
scenarioAsSuite:(options,result)=>{return ({sourceLocation})=>{
9+
const { gherkinDocument, pickle, testCase } = options.eventDataCollector.getTestCaseData(sourceLocation);
10+
let testSuiteId=`${convertNameToId(gherkinDocument.feature.name)};${convertNameToId(pickle.name)}`;
11+
let attr={name:testSuiteId,tests:0,failures:0,skipped:0,errors:0,time:testCase.result.duration||0},
12+
testSuite=[{_attr:attr}];
13+
14+
if (options.withPackage) {
15+
attr.package=convertNameToId(pickle.name);
16+
}
17+
18+
if (pickle.tags.length) {
19+
testSuite.push({properties:pickle.tags.map(tag=>createProperty("tag",tag.name))});
20+
}
21+
22+
testCase.steps.forEach((step,index)=>{
23+
const {gherkinKeyword, pickleStep } = options.eventDataCollector.getTestStepData({testCase:testCase,index:index});
24+
if (gherkinKeyword || (step.result && step.result.status==='failed')) {
25+
let result=step.result || {};
26+
let testCaseId=convertNameToId(gherkinKeyword?pickleStep.text:`${pickle.name} ${index?'after':'before'}`);
27+
let testCase=[
28+
{
29+
_attr:{
30+
classname:testCaseId,
31+
name:gherkinKeyword?pickleStep.text:(pickle.name+(index?' after':' before')),
32+
time:((result.duration || 0)/1000).toFixed(3)
33+
}
34+
}
35+
];
36+
switch (result.status) {
37+
case 'passed':
38+
break;
39+
case 'failed':
40+
testCase.push(createFailureOrError(gherkinKeyword?"failure":"error",result.exception));
41+
if (gherkinKeyword) {
42+
attr.failures+=1;
43+
}
44+
else {
45+
attr.errors+=1;
46+
}
47+
break;
48+
case 'pending':
49+
case 'undefined':
50+
testCase.push(createFailure(result.status === 'pending' ? 'Pending'
51+
: `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 });`
52+
));
53+
attr.failures+=1;
54+
break;
55+
case 'skipped':
56+
testCase.push({skipped: []});
57+
attr.skipped+=1;
58+
break;
59+
case 'ambiguous':
60+
testCase.push(createFailureOrError("error",result.exception));
61+
attr.errors+=1;
62+
break;
63+
default:
64+
break;
65+
// testCase.push(createFailure(`Unknown status - ${step.result.status}`));
66+
}
67+
attr.tests+=1;
68+
testSuite.push({testcase:testCase});
69+
}
70+
});
71+
result.push({testsuite:testSuite});
72+
73+
};
74+
},
75+
76+
scenarioAsStep:(options,result)=>{return ({sourceLocation})=>{
77+
const { gherkinDocument, pickle, testCase } = options.eventDataCollector.getTestCaseData(sourceLocation);
78+
let testSuiteId=`${convertNameToId(gherkinDocument.feature.name)}`,
79+
testCaseId=`${convertNameToId(pickle.name)}`;
80+
let attr,testSuite;
81+
if (result.length && result[result.length-1].testsuite[0]._attr.name===testSuiteId) {
82+
testSuite=result[result.length-1].testsuite;
83+
attr=testSuite[0]._attr;
84+
}
85+
else {
86+
attr={name:testSuiteId,tests:0,failures:0,skipped:0,errors:0,time:0};
87+
testSuite=[{_attr:attr}];
88+
result.push({testsuite:testSuite});
89+
}
90+
attr.time+=testCase.result.duration;
91+
attr.tests+=1;
92+
let testCaseTag=[
93+
{
94+
_attr:{
95+
classname:testCaseId,
96+
name:pickle.name,
97+
time:((testCase.result.duration ||0)/1000).toFixed(3)
98+
}
99+
}
100+
];
101+
102+
if (pickle.tags.length && options.propertiesInTestcase) {
103+
testCaseTag.push({properties:pickle.tags.map(tag=>createProperty("tag",tag.name))});
104+
}
105+
106+
testCase.steps.every((step,index)=>{
107+
const {gherkinKeyword, pickleStep } = options.eventDataCollector.getTestStepData({testCase:testCase,index:index});
108+
if (gherkinKeyword || (step.result && step.result.status==='failed')) {
109+
let result=step.result || {};
110+
switch (result.status) {
111+
case 'passed':
112+
break;
113+
case 'failed':
114+
testCaseTag.push(createFailureOrError(gherkinKeyword?"failure":"error",result.exception));
115+
attr[gherkinKeyword?"failures":"errors"]+=1;
116+
return false;
117+
case 'pending':
118+
case 'undefined':
119+
testCaseTag.push(createFailure(result.status === 'pending' ? 'Pending'
120+
: `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 });`
121+
));
122+
attr.failures+=1;
123+
return false;
124+
case 'skipped':
125+
testCaseTag.push({skipped: []});
126+
attr.skipped+=1;
127+
return false;
128+
case 'ambiguous':
129+
testCaseTag.push(createFailureOrError("error",result.exception));
130+
attr.errors+=1;
131+
return false;
132+
default:
133+
break;
134+
// testCase.push(createFailure(`Unknown status - ${step.result.status}`));
135+
}
136+
}
137+
return true;
138+
});
139+
testSuite.push({testcase:testCaseTag});
140+
};
141+
}
142+
143+
};

index.js

Lines changed: 3 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -1,189 +1,18 @@
1-
const { Formatter, formatterHelpers } = require('cucumber');
1+
const {Formatter} = require('cucumber');
22
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");
414

425
class JUnitFormatter extends Formatter {
436
/** @param {Options} options */
447
constructor(options) {
458
super(options);
469
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-
};
18110

18211
options.eventBroadcaster.on('test-run-started', ()=>{
18312
this._result=[];
18413
});
18514

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));
18716

18817

18918
options.eventBroadcaster.on('test-run-finished', ()=>{

util.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/** Utility module */
2+
3+
"use strict";
4+
5+
const {formatterHelpers} = require('cucumber');
6+
7+
module.exports={
8+
// Next two methods taken from https://github.com/stjohnjohnson/cucumber-junit/blob/master/lib/cucumber_junit.js
9+
createProperty:(name, value)=>{
10+
return {
11+
property: [
12+
{
13+
_attr: {
14+
name: name,
15+
value: value
16+
}
17+
}
18+
]
19+
};
20+
},
21+
22+
createFailure:(message)=>{
23+
return {
24+
failure: [
25+
{ _attr: { message: message.split("\n").shift() } },
26+
message
27+
]
28+
};
29+
},
30+
31+
createFailureOrError:(type,exception)=>{
32+
let {name}=exception,
33+
ret ={};
34+
ret[type]=[
35+
{ _attr: { message: name } },
36+
formatterHelpers.formatError(exception)
37+
];
38+
return ret;
39+
},
40+
41+
// Next one from original json_formatter
42+
convertNameToId:obj=>obj.replace(/ /g, '-').toLowerCase()
43+
};

0 commit comments

Comments
 (0)