This repository has been archived by the owner on Jun 29, 2020. It is now read-only.
/
jasmine2-runner.js
210 lines (182 loc) · 7.59 KB
/
jasmine2-runner.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
var system = require('system');
var fs = require('fs');
var files = [];
if(system.args[1] != null){
files = eval(system.args[1]);
}
console.log(files);
function humanReadableElapsed(rawMilliseconds){
var minutes = Math.floor(rawMilliseconds / (1000 * 60));
var seconds = ((rawMilliseconds / 1000) % (1000 * 60)).toFixed(3);
if(minutes > 0){
minutes + ":" + (100 + seconds).substring(1) + "s";
}
else {
return seconds + "s";
}
}
var Runner = {
execute: function(callback){
console.log('J2R: Starting...');
var page = require('webpage').create();
page.onConsoleMessage = function(msg) {
if(msg == 'jasmineDone'){
// ?
}
else if(msg.indexOf('display: ') === 0){
console.log(msg.substring(9));
}
else if(msg.indexOf('stats: ') === 0){
callback(JSON.parse(msg.substring(7)));
}
else{
console.log(msg);
}
};
page.onResourceError = function(resourceError){
console.log("ERROR: Failed to load resource '" + resourceError.url + "'. Error " + resourceError.errorCode + ': ' + resourceError.errorString);
};
console.log("J2R: Working Dir is: " + fs.workingDirectory);
var expectedContent = '<html><head></head><body></body></html>';
var expectedLocation = 'file:///' + fs.workingDirectory + '/';
console.log("J2R: Setting page location to: " + expectedLocation);
page.setContent(expectedContent, expectedLocation);
// standard files
console.log('J2R: Inject Jasmine Files');
page.injectJs('town/js/lib/jasmine-2.0.0/jasmine.js');
page.injectJs('town/js/lib/jasmine-2.0.0/jasmine-html.js');
page.injectJs('jasmine2-runner-boot.js');
// inject reporter
// console.log('J2R: Inject TeamCity reporter');
// page.injectJs('teamcity_reporter.js');
// page.evaluate(function(){
// jasmine.addReporter(new jasmineReporters.TeamCityReporter());
// });
console.log('J2R: Inject Console reporter');
page.injectJs('console_reporter.js');
page.evaluate(function(){
jasmine.addReporter(new jasmineReporters.ConsoleReporter());
});
// inject additional required files
console.log('J2R: Inject your include files');
page.injectJs('town/js/lib/require-2.1.11.js');
// execute passed code
console.log('J2R: Execute your code');
page.evaluate(function(specs){
require.config({
baseUrl: "town/js/src",
paths: {
"knockout": "../lib/knockout-3.0.0",
"Squire": "../lib/Squire"
}
});
require(specs, function(){
window.executeTests();
});
}, files);
}
};
var StatsProcessor = {
evaluate: function(rawStats){
var flatStats = [];
var flatSuites = [];
StatsProcessor.flattenSpecs("", rawStats, flatStats, flatSuites);
var sortedFlatStats = flatStats.sort(function(a,b){
// sort descending by execution time
return b.executionTime - a.executionTime;
});
var averages = StatsProcessor.getAverages(flatStats);
if(averages.total > 0){
var fiftyPercentOfTotalIndex = StatsProcessor.getPercentOfTotalIndex(sortedFlatStats, averages, 0.50);
var ninetyPercentOfTotalIndex = StatsProcessor.getPercentOfTotalIndex(sortedFlatStats, averages, 0.90);
return {
averageExecutionTime: averages.average,
standardDeviation: averages.standardDeviation,
totalExecutionTime: averages.total,
totalCount: sortedFlatStats.length,
fiftyPercent: {
numberOfTests: fiftyPercentOfTotalIndex + 1
},
ninetyPercent: {
numberOfTests: ninetyPercentOfTotalIndex + 1
},
specs: sortedFlatStats
};
}
else{
return 0;
}
},
flattenSpecs: function(description, stats, flatStats, flatSuites){
if(stats.specs !== undefined){
stats.specs.forEach(function(spec){
if(spec.status == "passed"){
spec.fullName = description + spec.description;
flatStats.push(spec);
}
});
}
if(stats.suites !== undefined){
stats.suites.forEach(function(suite){
StatsProcessor.flattenSpecs(description + suite.description + " -> ", suite, flatStats, flatSuites);
});
}
},
getAverages: function(flatStats){
var sum = flatStats.reduce(function(memo, stats){
return memo + stats.executionTime;
}, 0);
var avg = sum / flatStats.length;
var squaredDiffs = flatStats.map(function(stats){
return Math.pow((stats.executionTime - avg), 2);
});
var sumSquaredDiffs = squaredDiffs.reduce(function(memo,diff){
return memo + diff;
}, 0);
var avgSquaredDiff = sumSquaredDiffs/flatStats.length;
var stdDev = Math.sqrt(avgSquaredDiff);
return {
average: avg,
total: sum,
standardDeviation: stdDev
};
},
getPercentOfTotalIndex: function(sortedFlatStats, averages, percentage){
var percentOfTotal = averages.total * percentage;
var runningTotal = 0;
var percentIndex = undefined;
for(var i = 0; i < sortedFlatStats.length; i++){
runningTotal += sortedFlatStats[i].executionTime;
percentIndex = i;
if(runningTotal >= percentOfTotal)
break;
}
return percentIndex;
}
};
Runner.execute(function(result){
var stats = StatsProcessor.evaluate(result);
if(stats === 0){
console.log("-----------------------------------------------------------------");
console.log("No passed specs, cannot calculate statistics.")
console.log("-----------------------------------------------------------------");
}
else{
console.log("-----------------------------------------------------------------");
console.log(stats.totalCount + " tests passed in " + humanReadableElapsed(stats.totalExecutionTime));
console.log("Average Time: " + humanReadableElapsed(stats.averageExecutionTime));
console.log("Standard Deviation: " + humanReadableElapsed(stats.standardDeviation));
var ninetyPerBy = Math.round((stats.ninetyPercent.numberOfTests / stats.totalCount) * 100.0);
console.log(ninetyPerBy + "% (" + stats.ninetyPercent.numberOfTests + ") of the tests account for 90% of the overall time.");
var fiftyPerBy = Math.round((stats.fiftyPercent.numberOfTests / stats.totalCount) * 100.0);
console.log(fiftyPerBy + "% (" + stats.fiftyPercent.numberOfTests + ") of the tests account for 50% of the overall time.");
console.log("-----------------------------------------------------------------");
console.log("Slowest Tests:")
stats.specs.slice(0, stats.fiftyPercent.numberOfTests - 1).forEach(function(spec){
var time = (" " + humanReadableElapsed(spec.executionTime)).slice(-10);
console.log(" [" + time + "]: " + spec.fullName);
});
console.log("-----------------------------------------------------------------");
}
phantom.exit();
});