-
Notifications
You must be signed in to change notification settings - Fork 160
/
utils.js
288 lines (261 loc) · 7.95 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
const _ = require('lodash');
const chalk = require('chalk');
const uuid = require('uuid');
const mochaUtils = require('mocha/lib/utils');
const stringify = require('json-stringify-safe');
const diff = require('diff');
const stripAnsi = require('strip-ansi');
/**
* Return a classname based on percentage
*
* @param {String} msg - message to log
* @param {String} level - log level [log, info, warn, error]
* @param {Object} config - configuration object
*/
function log(msg, level, config) {
// Don't log messages in quiet mode
if (config && config.quiet) return;
const logMethod = console[level] || console.log;
let out = msg;
if (typeof msg === 'object') {
out = stringify(msg, null, 2);
}
logMethod(`[${chalk.gray('mochawesome')}] ${out}\n`);
}
/**
* Return a classname based on percentage
*
* @param {Integer} pct - percentage
*
* @return {String} classname
*/
function getPercentClass(pct) {
if (pct <= 50) {
return 'danger';
} else if (pct > 50 && pct < 80) {
return 'warning';
} else {
return 'success';
}
}
/**
* Strip the function definition from `str`,
* and re-indent for pre whitespace.
*
* @param {String} str - code in
*
* @return {String} cleaned code string
*/
function cleanCode(str) {
str = str
.replace(/\r\n|[\r\n\u2028\u2029]/g, '\n') // unify linebreaks
.replace(/^\uFEFF/, '') // replace zero-width no-break space
.replace(/^(?:.|\s)*?(?:{|=>) *\n?(?:\(|{)?/, '') // replace function declaration
.replace(/\)\s*\)\s*$/, ')') // replace closing paren
.replace(/\s*};?\s*$/, ''); // replace closing bracket
// Preserve indentation by finding leading tabs/spaces
// and removing that amount of space from each line
const spaces = str.match(/^\n?( *)/)[1].length;
const tabs = str.match(/^\n?(\t*)/)[1].length;
/* istanbul ignore next */
const indentRegex = new RegExp(`^\n?${tabs ? '\t' : ' '}{${tabs || spaces}}`, 'gm');
str = str.replace(indentRegex, '').trim();
return str;
}
/**
* Create a unified diff between two strings
*
* @param {Error} err Error object
* @param {string} err.actual Actual result returned
* @param {string} err.expected Result expected
*
* @return {string} diff
*/
function createUnifiedDiff({ actual, expected }) {
return diff.createPatch('string', actual, expected)
.split('\n')
.splice(4)
.map(line => {
if (line.match(/@@/)) {
return null;
}
if (line.match(/\\ No newline/)) {
return null;
}
return line.replace(/^(-|\+)/, '$1 ');
})
.filter(line => typeof line !== 'undefined' && line !== null)
.join('\n');
}
/**
* Create an inline diff between two strings
*
* @param {Error} err Error object
* @param {string} err.actual Actual result returned
* @param {string} err.expected Result expected
*
* @return {array} diff string objects
*/
function createInlineDiff({ actual, expected }) {
return diff.diffWordsWithSpace(actual, expected);
}
/**
* Return a normalized error object
*
* @param {Error} err Error object
*
* @return {Object} normalized error
*/
function normalizeErr(err, config) {
const { name, message, actual, expected, stack, showDiff } = err;
let errMessage;
let errDiff;
/**
* Check that a / b have the same type.
*/
function sameType(a, b) {
const objToString = Object.prototype.toString;
return objToString.call(a) === objToString.call(b);
}
// Format actual/expected for creating diff
if (showDiff !== false && sameType(actual, expected) && expected !== undefined) {
/* istanbul ignore if */
if (!(_.isString(actual) && _.isString(expected))) {
err.actual = mochaUtils.stringify(actual);
err.expected = mochaUtils.stringify(expected);
}
errDiff = config.useInlineDiffs ? createInlineDiff(err) : createUnifiedDiff(err);
}
// Assertion libraries do not output consitent error objects so in order to
// get a consistent message object we need to create it ourselves
if (name && message) {
errMessage = `${name}: ${stripAnsi(message)}`;
} else if (stack) {
errMessage = stack.replace(/\n.*/g, '');
}
return {
message: errMessage,
estack: stack && stripAnsi(stack),
diff: errDiff
};
}
/**
* Return a plain-object representation of `test`
* free of cyclic properties etc.
*
* @param {Object} test
*
* @return {Object} cleaned test
*/
function cleanTest(test, config) {
let code = test.body;
/* istanbul ignore next */
if (code === undefined) {
/* istanbul ignore next: test.fn exists prior to mocha 2.4.0 */
code = test.fn ? test.fn.toString() : '';
}
const cleaned = {
title: stripAnsi(test.title),
fullTitle: _.isFunction(test.fullTitle) ? stripAnsi(test.fullTitle()) : /* istanbul ignore next */ stripAnsi(test.title),
timedOut: test.timedOut,
duration: test.duration || 0,
state: test.state,
speed: test.speed,
pass: test.state === 'passed',
fail: test.state === 'failed',
pending: test.pending,
context: stringify(test.context, null, 2),
code: code && cleanCode(code),
err: (test.err && normalizeErr(test.err, config)) || {},
isRoot: test.parent && test.parent.root,
uuid: test.uuid || /* istanbul ignore next: default */uuid.v4(),
parentUUID: test.parent && test.parent.uuid,
isHook: test.type === 'hook'
};
cleaned.skipped = (!cleaned.pass && !cleaned.fail && !cleaned.pending && !cleaned.isHook);
return cleaned;
}
/**
* Return a plain-object representation of `suite` with additional properties for rendering.
*
* @param {Object} suite
* @param {Object} totalTestsRegistered
* @param {Integer} totalTestsRegistered.total
*
* @return {Object|boolean} cleaned suite or false if suite is empty
*/
function cleanSuite(suite, totalTestsRegistered, config) {
let duration = 0;
const passingTests = [];
const failingTests = [];
const pendingTests = [];
const skippedTests = [];
const beforeHooks = _.map(
[].concat(suite._beforeAll, suite._beforeEach),
test => cleanTest(test, config)
);
const afterHooks = _.map(
[].concat(suite._afterAll, suite._afterEach),
test => cleanTest(test, config)
);
const tests = _.map(
suite.tests,
test => {
const cleanedTest = cleanTest(test, config);
duration += test.duration;
if (cleanedTest.state === 'passed') passingTests.push(cleanedTest.uuid);
if (cleanedTest.state === 'failed') failingTests.push(cleanedTest.uuid);
if (cleanedTest.pending) pendingTests.push(cleanedTest.uuid);
if (cleanedTest.skipped) skippedTests.push(cleanedTest.uuid);
return cleanedTest;
}
);
totalTestsRegistered.total += tests.length;
const cleaned = {
uuid: uuid.v4(),
title: stripAnsi(suite.title),
fullFile: suite.file || '',
file: suite.file ? suite.file.replace(process.cwd(), '') : '',
beforeHooks,
afterHooks,
tests,
suites: suite.suites,
passes: passingTests,
failures: failingTests,
pending: pendingTests,
skipped: skippedTests,
duration,
root: suite.root,
rootEmpty: suite.root && tests.length === 0,
_timeout: suite._timeout
};
const isEmptySuite = _.isEmpty(cleaned.suites)
&& _.isEmpty(cleaned.tests)
&& _.isEmpty(cleaned.beforeHooks)
&& _.isEmpty(cleaned.afterHooks);
return !isEmptySuite && cleaned;
}
/**
* Map over a suite, returning a cleaned suite object
* and recursively cleaning any nested suites.
*
* @param {Object} suite Suite to map over
* @param {Object} totalTestsReg Cumulative count of total tests registered
* @param {Integer} totalTestsReg.total
* @param {Object} config Reporter configuration
*/
function mapSuites(suite, totalTestsReg, config) {
const suites = _.compact(_.map(suite.suites, subSuite => (
mapSuites(subSuite, totalTestsReg, config)
)));
const toBeCleaned = Object.assign({}, suite, { suites });
return cleanSuite(toBeCleaned, totalTestsReg, config);
}
module.exports = {
log,
getPercentClass,
cleanCode,
cleanTest,
cleanSuite,
mapSuites
};