Skip to content

Commit 4aa9c41

Browse files
replaced legacy json formatter with custom one based on cucumber mess… (#30)
* replaced legacy json formatter with custom one based on cucumber messages added support of named hooks
1 parent 931be62 commit 4aa9c41

File tree

11 files changed

+354
-3514
lines changed

11 files changed

+354
-3514
lines changed

CHANGELOG.MD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 0.13.0
2+
- replaced legacy json formatter with custom one based on cucumber messages
3+
- added support of named hooks
4+
15
# 0.0.11
26
- fixed $' replace issue
37

formatter/formatter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { JsonFormatter } = require('@cucumber/cucumber');
1+
const JsonFormatter = require('./json_formatter');
22
const fs = require('fs');
33
const path = require('path');
44
class HTMLFormatter extends JsonFormatter {

formatter/json_formatter.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
const { Formatter } = require('@cucumber/cucumber');
2+
class JsonFormatter extends Formatter {
3+
4+
constructor(options) {
5+
super(options);
6+
options.eventBroadcaster.on('envelope', this.processEnvelope.bind(this));
7+
this.rpConfig = options.parsedArgvOptions.rpConfig;
8+
this.json = [];
9+
this.hooks = {};
10+
}
11+
12+
processEnvelope(envelope) {
13+
if (envelope.testCaseFinished) {
14+
this.finishTest(envelope);
15+
} else if (envelope.testRunFinished) {
16+
this.finishLaunch();
17+
} else if (envelope.hook) {
18+
this.hooks[envelope.hook.id] = envelope.hook;
19+
}
20+
}
21+
22+
finishTest(envelope) {
23+
const testCase = this.eventDataCollector.getTestCaseAttempt(envelope.testCaseFinished.testCaseStartedId);
24+
let feature = this.json.find(feature => feature.name === testCase.gherkinDocument.feature.name);
25+
if (!feature) {
26+
feature = {
27+
description: testCase.gherkinDocument.feature.description,
28+
id: 'feature' + testCase.pickle.id,
29+
line: testCase.gherkinDocument.feature.location.line,
30+
keyword: testCase.gherkinDocument.feature.keyword,
31+
name: testCase.gherkinDocument.feature.name,
32+
uri: testCase.gherkinDocument.uri,
33+
elements: [],
34+
tags: this.formatTags(testCase.gherkinDocument.feature.tags)
35+
};
36+
this.json.push(feature);
37+
}
38+
const steps = testCase.testCase.testSteps;
39+
for (const step of steps) {
40+
const pickle = testCase.pickle.steps.find(pickle => step.pickleStepId === pickle.id);
41+
step.name = pickle ? pickle.text : this.hookText(steps, step);
42+
const result= testCase.stepResults[step.id];
43+
step.result = {
44+
status: result.status.toLowerCase(),
45+
duration: result.duration.seconds * 1_000_000 + result.duration.nanos
46+
};
47+
step.embeddings = testCase.stepAttachments[step.id]?.map(attachment => ({
48+
...attachment,
49+
data: attachment.body,
50+
mime_type: attachment.mediaType
51+
}));
52+
}
53+
const scenario = {
54+
steps,
55+
name: testCase.pickle.name,
56+
id: testCase.pickle.id,
57+
keyword: 'Scenario',
58+
tags: this.formatTags(testCase.pickle.tags),
59+
type: 'scenario'
60+
}
61+
feature.elements.push(scenario);
62+
}
63+
64+
finishLaunch() {
65+
this.log(JSON.stringify(this.json, null, 2))
66+
}
67+
68+
formatTags(tags) {
69+
return tags.map(tag => ({ name: tag.name, line: tag.location?.line }))
70+
}
71+
72+
hookText(steps, step) {
73+
const hook = this.hooks[step.hookId];
74+
if (hook?.name) return hook.name;
75+
const stepsBefore = steps.slice(0, steps.findIndex((element) => element === step));
76+
return stepsBefore.every(element => element.pickleStepId === undefined) ? 'Before' : 'After'
77+
}
78+
}
79+
80+
module.exports = JsonFormatter;

0 commit comments

Comments
 (0)