UNPKG

3.23 kBJavaScriptView Raw
1const { Formatter, SummaryFormatter } = require('cucumber');
2const { EOL } = require('os');
3
4class PrettyFormatter extends Formatter {
5 constructor(options) {
6 super(options);
7 /**
8 * @property colorFns - a series of helper functions for outputting colors
9 * @property colorsEnabled
10 * @property cwd - the current working directory
11 * @property eventBroadcaster - an event emitter that emits the event protocol
12 * @property eventDataCollector - an instance of EventDataCollector which handles the complexity of grouping the data for related events
13 * @property log - function which will write the passed string to the the designated stream
14 * @property snippetBuilder - an object with a build method that should be called with {keywordType, pickleStep}
15 * @property stream - the underlying stream the formatter is writing to
16 * @property supportCodeLibrary
17 *
18 * @property {boolean} [pretty.passed=false] - Log passed status
19 * @property {boolean} [pretty.summary=true] - Log summary
20 *
21 * @see https://docs.cucumber.io/event-protocol/
22 * @see https://github.com/cucumber/cucumber-js/blob/master/docs/custom_formatters.md
23 */
24 this.options = options;
25
26 this.noptions = Object.create(options);
27 this.noptions.eventBroadcaster = { on: () => {} };
28
29 options.eventBroadcaster.on('test-case-started', event => {
30 const data = this.eventDataCollector.getTestCaseData(event.sourceLocation);
31 this.log(` Scenario: ${data.pickle.name}${EOL}`);
32 });
33
34 options.eventBroadcaster.on('test-step-started', event => {
35 const data = this.eventDataCollector.getTestStepData(event);
36 if (data.testStep.sourceLocation) {
37 this.log(` ${data.gherkinKeyword}${data.pickleStep.text}${EOL}`);
38 }
39 });
40
41 options.eventBroadcaster.on('test-step-finished', event => {
42 const status = event.result.status;
43 if (status === 'passed' && !this.option('passed', false)) {
44 return;
45 }
46
47 const data = this.eventDataCollector.getTestStepData(event);
48 if (data.testStep.sourceLocation) {
49 this.log(this.color(status, ` ${status}${EOL}`));
50 }
51 });
52
53 options.eventBroadcaster.on('test-case-finished', event => {
54 this.log(EOL);
55 });
56
57 options.eventBroadcaster.on('test-run-finished', event => {
58 if (this.option('summary', true)) {
59 new SummaryFormatter(this.noptions).logSummary(event);
60 }
61 });
62 }
63
64 /**
65 * Get format option
66 * @param {string} key - Pretty key
67 * @param {*} value - Default value
68 * @return {*} Pretty value or default value if key is undefined
69 * @example
70 * // --format-options '{ "pretty": { "foo": "baz" } }'
71 * this.get('foo', 'bar'); // === 'baz'
72 */
73 option(key, value) {
74 const pretty = this.options.pretty;
75 return pretty && pretty.hasOwnProperty(key) ? pretty[key] : value;
76 }
77
78 /**
79 * Colour text respecting colorsEnabled option
80 * @param {string} key - colorFns key
81 * @param {string} value - Text to colour
82 * @return {string} Coloured text
83 */
84 color(key, value) {
85 return this.options.colorsEnabled ? this.colorFns[key](value) : value;
86 }
87}
88
89module.exports = PrettyFormatter;