1 | const Base = require('mocha/lib/reporters/base');
|
2 | const mochaPkg = require('mocha/package.json');
|
3 | const uuid = require('uuid');
|
4 | const marge = require('mochawesome-report-generator');
|
5 | const margePkg = require('mochawesome-report-generator/package.json');
|
6 | const conf = require('./config');
|
7 | const utils = require('./utils');
|
8 | const pkg = require('../package.json');
|
9 |
|
10 |
|
11 | const {
|
12 | log,
|
13 | mapSuites
|
14 | } = utils;
|
15 |
|
16 |
|
17 | const totalTestsRegistered = { total: 0 };
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | function done(output, options, config, failures, exit) {
|
33 | return marge.create(output, options)
|
34 | .then(([ htmlFile, jsonFile ]) => {
|
35 | if (!htmlFile && !jsonFile) {
|
36 | log('No files were generated', 'warn', config);
|
37 | } else {
|
38 | jsonFile && log(`Report JSON saved to ${jsonFile}`, null, config);
|
39 | htmlFile && log(`Report HTML saved to ${htmlFile}`, null, config);
|
40 | }
|
41 | })
|
42 | .catch(err => {
|
43 | log(err, 'error', config);
|
44 | })
|
45 | .then(() => {
|
46 | exit && exit(failures > 0 ? 1 : 0);
|
47 | });
|
48 | }
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | function consoleReporter(reporter) {
|
62 | if (reporter) {
|
63 | try {
|
64 |
|
65 | return require(`mocha/lib/reporters/${reporter}`);
|
66 | } catch (e) {
|
67 | log(`Unknown console reporter '${reporter}', defaulting to spec`);
|
68 | }
|
69 | }
|
70 |
|
71 | return require('mocha/lib/reporters/spec');
|
72 | }
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | function Mochawesome(runner, options) {
|
81 |
|
82 | this.config = conf(options);
|
83 |
|
84 |
|
85 | if (!runner.stats) {
|
86 | const createStatsCollector = require('mocha/lib/stats-collector');
|
87 | createStatsCollector(runner);
|
88 | }
|
89 |
|
90 |
|
91 | const reporterOptions = {
|
92 | ...options.reporterOptions,
|
93 | reportFilename: this.config.reportFilename,
|
94 | saveHtml: this.config.saveHtml,
|
95 | saveJson: this.config.saveJson
|
96 | };
|
97 |
|
98 |
|
99 |
|
100 | this.done = (failures, exit) => done(
|
101 | this.output,
|
102 | reporterOptions,
|
103 | this.config,
|
104 | failures,
|
105 | exit
|
106 | );
|
107 |
|
108 |
|
109 | totalTestsRegistered.total = 0;
|
110 |
|
111 |
|
112 | Base.call(this, runner);
|
113 |
|
114 | const reporterName = reporterOptions.consoleReporter;
|
115 | if (reporterName !== 'none') {
|
116 | const ConsoleReporter = consoleReporter(reporterName);
|
117 | new ConsoleReporter(runner);
|
118 | }
|
119 |
|
120 | let endCalled = false;
|
121 |
|
122 |
|
123 | [ 'suite', 'test', 'hook', 'pending' ].forEach(type => {
|
124 | runner.on(type, item => {
|
125 | item.uuid = uuid.v4();
|
126 | });
|
127 | });
|
128 |
|
129 |
|
130 | runner.on('end', () => {
|
131 | try {
|
132 |
|
133 | if (!endCalled) {
|
134 |
|
135 |
|
136 | endCalled = true;
|
137 |
|
138 | const rootSuite = mapSuites(this.runner.suite, totalTestsRegistered, this.config);
|
139 |
|
140 | const obj = {
|
141 | stats: this.stats,
|
142 | results: [ rootSuite ],
|
143 | meta: {
|
144 | mocha: {
|
145 | version: mochaPkg.version
|
146 | },
|
147 | mochawesome: {
|
148 | options: this.config,
|
149 | version: pkg.version
|
150 | },
|
151 | marge: {
|
152 | options: options.reporterOptions,
|
153 | version: margePkg.version
|
154 | }
|
155 | }
|
156 | };
|
157 |
|
158 | obj.stats.testsRegistered = totalTestsRegistered.total;
|
159 |
|
160 | const { passes, failures, pending, tests, testsRegistered } = obj.stats;
|
161 | const passPercentage = (passes / (testsRegistered - pending)) * 100;
|
162 | const pendingPercentage = (pending / testsRegistered) * 100;
|
163 |
|
164 | obj.stats.passPercent = passPercentage;
|
165 | obj.stats.pendingPercent = pendingPercentage;
|
166 | obj.stats.other = (passes + failures + pending) - tests;
|
167 | obj.stats.hasOther = obj.stats.other > 0;
|
168 | obj.stats.skipped = testsRegistered - tests;
|
169 | obj.stats.hasSkipped = obj.stats.skipped > 0;
|
170 | obj.stats.failures -= obj.stats.other;
|
171 |
|
172 |
|
173 | this.output = obj;
|
174 | }
|
175 | } catch (e) {
|
176 |
|
177 |
|
178 |
|
179 | log(`Problem with mochawesome: ${e.stack}`, 'error');
|
180 | }
|
181 | });
|
182 | }
|
183 |
|
184 | module.exports = Mochawesome;
|