-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
JasmineAdapter-1.1.2.js
192 lines (151 loc) · 5.4 KB
/
JasmineAdapter-1.1.2.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
/**
* @fileoverview Jasmine JsTestDriver Adapter.
* @author [email protected] (Misko Hevery)
* @author [email protected] (Olmo Maldonado)
*/
(function() {
var Env = function(onTestDone, onComplete) {
jasmine.Env.call(this);
this.specFilter = function(spec) {
if (!this.exclusive) return true;
var blocks = spec.queue.blocks, l = blocks.length;
for (var i = 0; i < l; i++) if (blocks[i].func.exclusive >= this.exclusive) return true;
return false;
};
this.reporter = new Reporter(onTestDone, onComplete);
};
jasmine.util.inherit(Env, jasmine.Env);
// Here we store:
// 0: everyone runs
// 1: run everything under ddescribe
// 2: run only iits (ignore ddescribe)
Env.prototype.exclusive = 0;
Env.prototype.execute = function() {
collectMode = false;
playback();
jasmine.Env.prototype.execute.call(this);
};
var Reporter = function(onTestDone, onComplete) {
this.onTestDone = onTestDone;
this.onComplete = onComplete;
this.reset();
};
jasmine.util.inherit(Reporter, jasmine.Reporter);
Reporter.formatStack = function(stack) {
var line, lines = (stack || '').split(/\r?\n/), l = lines.length, frames = [];
for (var i = 0; i < l; i++) {
line = lines[i];
if (line.match(/\/jasmine[\.-]/)) continue;
frames.push(line.replace(/https?:\/\/\w+(:\d+)?\/test\//, '').replace(/^\s*/, ' '));
}
return frames.join('\n');
};
Reporter.prototype.reset = function() {
this.specLog = jstestdriver.console.log_ = [];
};
Reporter.prototype.log = function(str) {
this.specLog.push(str);
};
Reporter.prototype.reportSpecStarting = function() {
this.reset();
this.start = +new Date();
};
Reporter.prototype.reportSpecResults = function(spec) {
var elapsed = +new Date() - this.start, results = spec.results();
if (results.skipped) return;
var item, state = 'passed', items = results.getItems(), l = items.length, messages = [];
for (var i = 0; i < l; i++) {
item = items[i];
if (item.passed()) continue;
state = (item.message.indexOf('AssertionError:') != -1) ? 'error' : 'failed';
messages.push( {
message: item + '',
name: item.trace.name,
stack: Reporter.formatStack(item.trace.stack)
});
}
this.onTestDone(new jstestdriver.TestResult(
spec.suite.getFullName(),
spec.description,
state,
jstestdriver.angular.toJson(messages),
this.specLog.join('\n'),
elapsed
));
};
Reporter.prototype.reportRunnerResults = function() {
this.onComplete();
};
var collectMode = true, intercepted = {};
describe = intercept('describe');
beforeEach = intercept('beforeEach');
afterEach = intercept('afterEach');
var JASMINE_TYPE = 'jasmine test case';
TestCase('Jasmine Adapter Tests', null, JASMINE_TYPE);
jstestdriver.pluginRegistrar.register( {
name: 'jasmine',
getTestRunsConfigurationFor: function(testCaseInfos, expressions, testRunsConfiguration) {
for (var i = 0; i < testCaseInfos.length; i++) {
if (testCaseInfos[i].getType() == JASMINE_TYPE) {
testRunsConfiguration.push(new jstestdriver.TestRunConfiguration(testCaseInfos[i], []));
}
}
return false; // allow other TestCases to be collected.
},
runTestConfiguration: function(config, onTestDone, onComplete) {
if (config.getTestCaseInfo().getType() != JASMINE_TYPE) return false;
(jasmine.currentEnv_ = new Env(onTestDone, onComplete)).execute();
return true;
},
onTestsFinish: function() {
jasmine.currentEnv_ = null;
collectMode = true;
}
});
function intercept(method) {
var bucket = intercepted[method] = [], method = window[method];
return function(desc, fn) {
if (collectMode) bucket.push(function() {
method(desc, fn);
});
else method(desc, fn);
};
}
function playback() {
for (var method in intercepted) {
var bucket = intercepted[method];
for (var i = 0, l = bucket.length; i < l; i++) bucket[i]();
}
}
})();
var ddescribe = function(name, fn) {
var env = jasmine.getEnv();
if (!env.exclusive) env.exclusive = 1; // run ddescribe only
describe(name, function() {
var oldIt = it;
it = function(name, fn) {
fn.exclusive = 1; // run anything under ddescribe
env.it(name, fn);
};
try {
fn.call(this);
}
finally {
it = oldIt;
};
});
};
var iit = function(name, fn) {
var env = jasmine.getEnv();
env.exclusive = fn.exclusive = 2; // run only iits
env.it(name, fn);
};
// Patch Jasmine for proper stack traces
jasmine.Spec.prototype.fail = function (e) {
var result = new jasmine.ExpectationResult( {
passed: false,
message: e ? jasmine.util.formatException(e) : 'Exception'
});
if(e) result.trace = e;
this.results_.addResult(result);
};