UNPKG

2.62 kBJavaScriptView Raw
1'use strict';
2var prettyMs = require('pretty-ms');
3var figures = require('figures');
4var Squeak = require('squeak');
5var chalk = require('chalk');
6var plur = require('plur');
7var log = new Squeak({separator: ' '});
8var x = module.exports;
9
10function beautifyStack(stack) {
11 var re = /(?:^(?! {4}at\b).{6})|(?:\((?:[\\\/](?:(?!node_modules[\\\/]ava[\\\/])[^:\\\/])+)+:\d+:\d+\))/;
12 var found = false;
13
14 return stack.split('\n').filter(function (line) {
15 var relevant = re.test(line);
16 found = found || relevant;
17 return !found || relevant;
18 }).join('\n');
19}
20
21log.type('success', {
22 color: 'green',
23 prefix: figures.tick
24});
25
26log.type('error', {
27 color: 'red',
28 prefix: figures.cross
29});
30
31x.write = log.write.bind(log);
32x.writelpad = log.writelpad.bind(log);
33x.success = log.success.bind(log);
34x.error = log.error.bind(log);
35
36x.test = function (props) {
37 if (props.err) {
38 log.error(props.title, chalk.red(props.err.message));
39 return;
40 }
41
42 if (props.skip) {
43 log.write(' ' + chalk.cyan('- ' + props.title));
44 return;
45 }
46
47 // display duration only over a threshold
48 var threshold = 100;
49 var dur = props.duration > threshold ? chalk.gray.dim(' (' + prettyMs(props.duration) + ')') : '';
50 log.success(props.title + dur);
51};
52
53x.errors = function (results) {
54 var i = 0;
55
56 results.forEach(function (result) {
57 if (!(result.error && result.error.message)) {
58 return;
59 }
60
61 i++;
62
63 log.writelpad(chalk.red(i + '.', result.title));
64 log.writelpad(chalk.red(beautifyStack(result.error.stack)));
65 log.write();
66 });
67};
68
69x.report = function (passed, failed, unhandled, uncaught) {
70 if (failed > 0) {
71 log.writelpad(chalk.red(failed, plur('test', failed), 'failed'));
72 } else {
73 log.writelpad(chalk.green(passed, plur('test', passed), 'passed'));
74 }
75
76 if (unhandled > 0) {
77 log.writelpad(chalk.red(unhandled, 'unhandled', plur('rejection', unhandled)));
78 }
79
80 if (uncaught > 0) {
81 log.writelpad(chalk.red(uncaught, 'uncaught', plur('exception', uncaught)));
82 }
83};
84
85x.unhandledRejections = function (file, rejections) {
86 if (!(rejections && rejections.length)) {
87 return;
88 }
89
90 rejections.forEach(function (rejection) {
91 log.write(chalk.red('Unhandled Rejection: ', file));
92
93 if (rejection.stack) {
94 log.writelpad(chalk.red(beautifyStack(rejection.stack)));
95 } else {
96 log.writelpad(chalk.red(JSON.stringify(rejection)));
97 }
98
99 log.write();
100 });
101};
102
103x.uncaughtException = function (file, error) {
104 log.write(chalk.red('Uncaught Exception: ', file));
105
106 if (error.stack) {
107 log.writelpad(chalk.red(beautifyStack(error.stack)));
108 } else {
109 log.writelpad(chalk.red(JSON.stringify(error)));
110 }
111
112 log.write();
113};