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 | const reporterOptions = Object.assign(
|
86 | {},
|
87 | (options.reporterOptions || {}),
|
88 | {
|
89 | reportFilename: this.config.reportFilename,
|
90 | saveHtml: this.config.saveHtml,
|
91 | saveJson: this.config.saveJson
|
92 | }
|
93 | );
|
94 |
|
95 |
|
96 |
|
97 | this.done = (failures, exit) => done(
|
98 | this.output,
|
99 | reporterOptions,
|
100 | this.config,
|
101 | failures,
|
102 | exit
|
103 | );
|
104 |
|
105 |
|
106 | totalTestsRegistered.total = 0;
|
107 |
|
108 |
|
109 | Base.call(this, runner);
|
110 |
|
111 | const reporterName = reporterOptions.consoleReporter;
|
112 | if (reporterName !== 'none') {
|
113 | const ConsoleReporter = consoleReporter(reporterName);
|
114 | new ConsoleReporter(runner);
|
115 | }
|
116 |
|
117 | let endCalled = false;
|
118 |
|
119 |
|
120 | [ 'suite', 'test', 'hook', 'pending' ].forEach(type => {
|
121 | runner.on(type, item => {
|
122 | item.uuid = uuid.v4();
|
123 | });
|
124 | });
|
125 |
|
126 |
|
127 | runner.on('end', () => {
|
128 | try {
|
129 |
|
130 | if (!endCalled) {
|
131 |
|
132 |
|
133 | endCalled = true;
|
134 |
|
135 | const rootSuite = mapSuites(this.runner.suite, totalTestsRegistered, this.config);
|
136 |
|
137 | const obj = {
|
138 | stats: this.stats,
|
139 | results: [ rootSuite ],
|
140 | meta: {
|
141 | mocha: {
|
142 | version: mochaPkg.version
|
143 | },
|
144 | mochawesome: {
|
145 | options: this.config,
|
146 | version: pkg.version
|
147 | },
|
148 | marge: {
|
149 | options: options.reporterOptions,
|
150 | version: margePkg.version
|
151 | }
|
152 | }
|
153 | };
|
154 |
|
155 | obj.stats.testsRegistered = totalTestsRegistered.total;
|
156 |
|
157 | const { passes, failures, pending, tests, testsRegistered } = obj.stats;
|
158 | const passPercentage = (passes / (testsRegistered - pending)) * 100;
|
159 | const pendingPercentage = (pending / testsRegistered) * 100;
|
160 |
|
161 | obj.stats.passPercent = passPercentage;
|
162 | obj.stats.pendingPercent = pendingPercentage;
|
163 | obj.stats.other = (passes + failures + pending) - tests;
|
164 | obj.stats.hasOther = obj.stats.other > 0;
|
165 | obj.stats.skipped = testsRegistered - tests;
|
166 | obj.stats.hasSkipped = obj.stats.skipped > 0;
|
167 | obj.stats.failures -= obj.stats.other;
|
168 |
|
169 |
|
170 | this.output = obj;
|
171 | }
|
172 | } catch (e) {
|
173 |
|
174 |
|
175 |
|
176 | log(`Problem with mochawesome: ${e.stack}`, 'error');
|
177 | }
|
178 | });
|
179 | }
|
180 |
|
181 | module.exports = Mochawesome;
|