-
Notifications
You must be signed in to change notification settings - Fork 0
/
slow-reporter.js
97 lines (73 loc) · 2.5 KB
/
slow-reporter.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
/* jshint node: true */
var tty = require('tty');
var table = require('text-table');
var wrap = require('wordwrap');
var stringLength = require('string-length');
var chalk = require('chalk');
module.exports = SlowReporter;
var SLOW_DEFAULT = 75;
var isatty = tty.isatty(1) && tty.isatty(2);
var window = { width: 75 };
if (isatty) {
window.width = process.stdout.getWindowSize ?
process.stdout.getWindowSize(1)[0] :
tty.getWindowSize()[1];
}
if (process.env.FORCE_TTY_WIDTH && +process.env.FORCE_TTY_WIDTH) {
window.width = parseInt(process.env.FORCE_TTY_WIDTH);
}
function SlowReporter(runner, options) {
var slowTests = [];
var verySlowColor = chalk.red;
var slowColor = chalk.yellow;
var textColor = chalk.gray;
function onTestComplete(duration, title, slow) {
if (duration > slow) {
slowTests.push({
title: title,
duration: duration,
durText: duration > (slow * 2) ?
verySlowColor(duration + 'ms') :
slowColor(duration + 'ms')
});
}
}
function onEnd() {
console.log(chalk.bold('Slow test count: %s\n'), slowTests.length);
if (slowTests.length === 0) {
return;
}
var sortedSlow = slowTests.sort(function(a, b) {
return b.duration - a.duration;
});
var slowTable = [];
sortedSlow.forEach(function(test) {
var wrappedTitle = wrap(10, window.width)(test.title);
var wrappedTokens = wrappedTitle.split('\n');
var first = wrappedTokens.shift();
slowTable.push([
test.durText,
textColor(first.trim())
]);
if (wrappedTokens.length) {
wrappedTokens.forEach(function(token) {
slowTable.push([
'',
textColor(token.trim())
]);
});
}
slowTable.push(['', '']);
});
console.log(table(slowTable, { stringLength: stringLength }));
setTimeout(function() {
process.exit();
}, 0);
}
runner.on('test end', function(test) {
onTestComplete(test.duration, test.fullTitle(), test.slow() || SLOW_DEFAULT);
});
runner.on('end', function () {
onEnd();
});
}