UNPKG

1.66 kBJavaScriptView Raw
1'use strict';
2
3function Logger(reporter) {
4 if (!(this instanceof Logger)) {
5 throw new TypeError('Class constructor Logger cannot be invoked without \'new\'');
6 }
7
8 Object.keys(Logger.prototype).forEach(function (key) {
9 this[key] = this[key].bind(this);
10 }, this);
11
12 this.reporter = reporter;
13}
14
15module.exports = Logger;
16
17Logger.prototype.start = function () {
18 if (!this.reporter.start) {
19 return;
20 }
21
22 this.write(this.reporter.start());
23};
24
25Logger.prototype.reset = function () {
26 if (!this.reporter.reset) {
27 return;
28 }
29
30 this.write(this.reporter.reset());
31};
32
33Logger.prototype.test = function (test) {
34 this.write(this.reporter.test(test));
35};
36
37Logger.prototype.unhandledError = function (err) {
38 if (!this.reporter.unhandledError) {
39 return;
40 }
41
42 this.write(this.reporter.unhandledError(err));
43};
44
45Logger.prototype.finish = function () {
46 if (!this.reporter.finish) {
47 return;
48 }
49
50 this.write(this.reporter.finish());
51};
52
53Logger.prototype.write = function (str) {
54 if (typeof str === 'undefined') {
55 return;
56 }
57
58 this.reporter.write(str);
59};
60
61Logger.prototype.stdout = function (data) {
62 if (!this.reporter.stdout) {
63 return;
64 }
65
66 this.reporter.stdout(data);
67};
68
69Logger.prototype.stderr = function (data) {
70 if (!this.reporter.stderr) {
71 return;
72 }
73
74 this.reporter.stderr(data);
75};
76
77Logger.prototype.exit = function (code) {
78 // TODO: figure out why this needs to be here to
79 // correctly flush the output when multiple test files
80 process.stdout.write('');
81 process.stderr.write('');
82
83 // timeout required to correctly flush IO on Node.js 0.10 on Windows
84 setTimeout(function () {
85 process.exit(code);
86 }, process.env.AVA_APPVEYOR ? 500 : 0);
87};