forked from mochajs/mocha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmochaRunnerExtend.js
144 lines (127 loc) · 3.86 KB
/
mochaRunnerExtend.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* Extend Mocha.Runner to enable log feature.
* Because in current version of Mocha, it does not support multiple reporters, so I overrides Runner for convenience.
* @author yongqingdong
* @date 2014-11-18
*/
/* global Mocha */
/* jshint -W116, -W110 */
;(function(global){
"use strict";
/* code: ready */
Mocha.Runner.prototype.CODE_READY = 0;
/* code: running */
Mocha.Runner.prototype.CODE_RUNNING = 1;
/* code: ended */
Mocha.Runner.prototype.CODE_ENDED = 2;
var logList = global.logList = [];
// Log function.
function recordLog(msg){
logList.push(msg);
console.log(msg);
}
/**
* Extends Mocha.Runner to add log information.
* @constructor
*/
function RunnerWrap(){
RunnerOld.apply(this, arguments);
var self = this;
global.mocha.runner = this;
/**
* Current status of test running.
*/
this.code = this.CODE_READY;
this.on('start', function(){
this.code = self.CODE_RUNNING;
recordLog("##Test started");
});
this.on('end', function(){
this.code = self.CODE_ENDED;
recordLog("##Test ended");
});
this.on("suite", function(suite){
if (suite.root) return;
if(suite.parent.root){
recordLog("Suite: " + suite.title);
}else{
recordLog("\t Suite: " + suite.title);
}
});
this.on("suite end", function(suite){
if (suite.root) return;
});
this.on('test end', function(test) {
if ('passed' == test.state) {
recordLog("\t PASS: " + test.title + test.duration);
} else if (test.pending) {
recordLog("\t PEND: " + test.title + test.duration);
} else {
recordLog("\t FAIL: " + test.title + test.duration);
}
});
// Copy from reporter/json
var tests = [];
var pending = [];
var failures = [];
var passes = [];
this.testResults = {
code: self.code,
stats: self.stats,
tests: tests.map(clean),
passes: passes.map(clean),
failures: failures.map(clean),
pending: pending.map(clean)
};
function update(){
self.testResults.code = self.code;
self.testResults.stats = self.stats;
self.testResults.tests = tests.map(clean);
self.testResults.passes = passes.map(clean);
self.testResults.failures = failures.map(clean);
self.testResults.pending = pending.map(clean);
}
this.on('test end', function(test){
tests.push(test);
update();
});
this.on('pass', function(test){
passes.push(test);
update();
});
this.on('fail', function(test){
failures.push(test);
update();
});
this.on('pending', function(test){
pending.push(test);
update();
});
this.on('end', function(){
update();
});
}
// Copy from reporter/json.
function clean(test) {
return {
title: test.title,
fullTitle: test.fullTitle(),
duration: test.duration,
err: errorJSON(test.err || {})
};
}
function errorJSON(err) {
var res = {};
Object.getOwnPropertyNames(err).forEach(function(key) {
res[key] = err[key];
}, err);
return res;
}
// Integrate Mocha.Runner to add some log information.
var RunnerOld = Mocha.Runner;
RunnerWrap.prototype = RunnerOld.prototype;
Mocha.Runner = RunnerWrap;
for(var prop in RunnerOld){
Mocha.Runner.prop = RunnerOld[prop];
}
})(this);