1 | const cucumber = require('cucumber');
|
2 |
|
3 | class CucumberStepFormatter extends cucumber.Formatter {
|
4 | constructor(options) {
|
5 | super(options);
|
6 | options.eventBroadcaster
|
7 | .on('test-step-attachment', this.attached.bind(this))
|
8 | .on('test-case-started', this.logTestCaseName.bind(this))
|
9 | .on('test-step-finished', this.logTestStep.bind(this))
|
10 | .on('test-case-finished', this.logSeparator.bind(this))
|
11 | .on('test-run-finished', this.logTestRunResult.bind(this));
|
12 | }
|
13 |
|
14 | attached({ data }) {
|
15 | if (data.includes('Hook Step:')) {
|
16 | this.hookStep = data;
|
17 | }
|
18 | }
|
19 |
|
20 | logTestCaseName({ sourceLocation }) {
|
21 | const { gherkinDocument, pickle } = this.eventDataCollector.getTestCaseData(sourceLocation);
|
22 | const text = `${gherkinDocument.feature.name}::: ${pickle.name}\n`;
|
23 | const colouredText = this.colorFns.location(text);
|
24 | this.log(colouredText);
|
25 | }
|
26 |
|
27 | logTestStep({ testCase, index, result }) {
|
28 | const { gherkinKeyword, pickleStep } =
|
29 | this.eventDataCollector.getTestStepData({ testCase, index });
|
30 |
|
31 | let text;
|
32 |
|
33 | if (pickleStep) {
|
34 | text = `${gherkinKeyword}${pickleStep.text} ---> ${result.status.toUpperCase()}\n`;
|
35 | } else {
|
36 | const statusUpper = result.status.toUpperCase();
|
37 | text = statusUpper === 'FAILED' ? `${this.hookStep} - FAILED\n\n` : '';
|
38 | }
|
39 |
|
40 | if (text) {
|
41 | const colouredText = this.colorFns[result.status](text);
|
42 | this.log(colouredText);
|
43 | }
|
44 | }
|
45 |
|
46 | logSeparator() {
|
47 | this.log('\n');
|
48 | }
|
49 |
|
50 | logTestRunResult({ result }) {
|
51 | if (result.success) {
|
52 | this.log(this.colorFns.passed('---PASS---'));
|
53 | } else {
|
54 | this.log(this.colorFns.failed('---FAIL---'));
|
55 | }
|
56 | }
|
57 | }
|
58 |
|
59 | module.exports = CucumberStepFormatter;
|