forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrintReporter.js
128 lines (107 loc) · 3.38 KB
/
PrintReporter.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
var jasmine = require("./jasmine");
var diff = require('./diff');
var red = '\u001b[1;41m';
var reset = '\u001b[0m';
function highlightDifferences(a, b) {
var changes = diff.diffChars(a, b);
var ret = {a: '', b: ''};
var change;
for (var i = 0, il = changes.length; i < il; i++) {
change = changes[i];
if (change.added) {
ret.b += red + change.value + reset;
} else if (change.removed) {
ret.a += red + change.value + reset;
} else {
ret.a += change.value;
ret.b += change.value;
}
}
return ret;
}
function PrintReporter(name, root_directory) {
this.skipCount = 0;
this.failCount = 0;
this.expectCount = 0;
this.specCount = 0;
this.name = name;
this.rootDirectory = root_directory;
}
exports.PrintReporter =
jasmine.PrintReporter = PrintReporter;
PrintReporter.prototype = new jasmine.Reporter();
// Set verbose=true on the test instance to enable additional logging
PrintReporter.prototype.verbose = false;
PrintReporter.prototype._didSpecName = false;
PrintReporter.prototype.reportSpecStarting = function(spec) {
this._didSpecName = false;
this._spec = spec;
};
PrintReporter.prototype.reportRunnerResults = function(runner) {
// Don't print out the spec name at the end
this._didSpecName = true;
this.log([
this.specCount + " spec",
this.expectCount + " expect",
this.skipCount + " skip",
this.failCount + " fail"
].join(" "));
require("./phantom").exit(this.failCount);
};
PrintReporter.prototype.reportSuiteResults = function(suite) {
var results = suite.results();
if (this.verbose) {
this.log('Suite "' + suite.description + '": ' +
results.passedCount + '/' + results.totalCount + ' passed.');
}
// If suite is nested, only count the root suite
if (!suite.parentSuite) {
this.failCount += results.totalCount - results.passedCount;
this.expectCount += results.totalCount;
}
};
PrintReporter.prototype.reportSpecResults = function(spec) {
if (this.verbose) {
this.log('it... ' +
spec.description + ': ' + (spec.results().passed() ? 'pass' : 'fail'));
}
var self = this;
var results = spec.results();
this.specCount += 1;
if (results.skipped)
this.skipCount += 1;
results.getItems().forEach(function(result) {
if (result.type == 'log') {
self.log(result.toString());
} else if (result.type == 'expect' && result.passed && !result.passed()) {
var ppActual = jasmine.pp(result.actual);
var ppExpected = jasmine.pp(result.expected);
var colorDiff = highlightDifferences(ppActual, ppExpected);
var message = result.message;
message = message.replace(ppActual, colorDiff.a);
message = message.replace(ppExpected, colorDiff.b);
self.log(message);
if (result.trace.stack) {
self.log();
result.trace.stack.split('\n').forEach(function(stackFrame) {
if (stackFrame.indexOf('/jasmine/lib/jasmine-core') > -1) {
return;
}
if (this.rootDirectory) {
stackFrame = stackFrame.replace(this.rootDirectory + '/', '');
}
self.log(' ' + stackFrame);
}.bind(this));
}
}
});
};
PrintReporter.prototype.log = function(str) {
if (!this._didSpecName) {
console.log('');
console.log(
this._spec.suite.description + ': it ' + this._spec.description);
this._didSpecName = true;
}
console.log(str || '');
};