-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
96 lines (90 loc) · 3 KB
/
index.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
/* eslint-env node */
const chalk = require('chalk');
let write;
function TimeReporter (opts) {
opts = opts || {};
this.out = opts.out || process.stdout;
this.runDuration = 0;
this.previousDot = true;
this.opts = opts;
write = this.out.write.bind(this.out);
this.failures = [];
this.longestTest = { runDuration: 0 };
this.longTests = [];
}
TimeReporter.prototype = {
writeFailure: function (data) {
write(chalk.red(`\nTest Failure - ${data.name.trim()}\n`));
if (data.error) {
if (data.error.message) {
write(`\t ${chalk.red(data.error.message)} \n`);
}
if (data.error.hasOwnProperty('actual') && data.error.hasOwnProperty('expected')) {
write(`\t Actual : ${chalk.yellow(data.error.actual)}\n`);
write(`\t Expected: ${chalk.yellow(data.error.expected)}\n`);
}
if (data.error.stack) {
write(`\t${chalk.red(data.error.stack)} \n`);
}
} else {
write(`\t ${chalk.red(JSON.stringify(data))} \n`);
}
this.failures.push(data);
},
report: function (prefix, data) {
data.runDuration = data.runDuration || 0; // so that math doesn't break for null/undefined
this.runDuration += data.runDuration;
let dot = false;
if (data.skipped) {
write(chalk.blue(`\nTest Skipped - ${data.name.trim()}`));
}
if (data.failed) {
this.writeFailure(data);
}
if (data.passed) {
if (data.runDuration > this.longestTest.runDuration) {
this.longestTest = data;
}
if (this.opts.sort && !this.done && data.runDuration > 500) {
this.longTests.push(data);
if (!this.previousDot) {
write('\n');
}
write('🐢 ');
this.previousDot = true;
return;
}
const result = `\n${data.runDuration}ms - ${data.name.trim()}`;
if (data.runDuration > 2000) {
write(chalk.red(result));
} else if (data.runDuration > 1000) {
write(chalk.magenta(result));
} else if (data.runDuration > 500) {
write(chalk.yellow(result));
} else {
dot = true;
if (!this.previousDot) {
write('\n');
}
write(this.opts.sort ? '🐇 ' : '.');
}
}
this.previousDot = dot;
},
finish: function () {
this.done = true;
if (this.opts.sort && this.longTests.length) {
const sorted = this.longTests.sort((a, b) => a.runDuration < b.runDuration ? -1 : 1);
sorted.forEach(data => this.report(null, data));
} else if (this.longestTest.name) {
write(`\n Longest test - ${this.longestTest.runDuration}ms - ${this.longestTest.name.trim()}\n`);
}
write(`\n Tests completed in ${this.runDuration / 1000} seconds \n`);
if (this.failures.length) {
write('\n Failing tests: \n');
this.failures.forEach(this.writeFailure.bind(this));
}
write(`\nLEGEND: ${chalk.blue('Skipped')} ${chalk.red('Tests > 2 seconds')} ${chalk.magenta('Tests > 1 second')} ${chalk.yellow('Tests > 0.5 seconds')}\n`);
}
};
module.exports = TimeReporter;