|
1 | 1 | /**
|
2 | 2 | * @fileoverview Jasmine JsTestDriver Adapter.
|
3 | 3 | * @author [email protected] (Misko Hevery)
|
4 |
| - * @author [email protected] (Olmo Maldonado) |
5 | 4 | */
|
6 |
| -(function(){ |
7 |
| - |
8 |
| - |
9 |
| -var Env = function(onTestDone, onComplete){ |
10 |
| - jasmine.Env.call(this); |
11 |
| - |
12 |
| - this.specFilter = function(spec){ |
13 |
| - if (!this.exclusive) return true; |
14 |
| - var blocks = spec.queue.blocks, l = blocks.length; |
15 |
| - for (var i = 0; i < l; i++) if (blocks[i].func.exclusive >= this.exclusive) return true; |
16 |
| - return false; |
17 |
| - }; |
18 |
| - |
19 |
| - this.reporter = new Reporter(onTestDone, onComplete); |
20 |
| -}; |
21 |
| -jasmine.util.inherit(Env, jasmine.Env); |
22 |
| - |
23 |
| -// Here we store: |
24 |
| -// 0: everyone runs |
25 |
| -// 1: run everything under ddescribe |
26 |
| -// 2: run only iits (ignore ddescribe) |
27 |
| -Env.prototype.exclusive = 0; |
28 |
| - |
29 |
| - |
30 |
| -Env.prototype.execute = function(){ |
31 |
| - collectMode = false; |
32 |
| - playback(); |
33 |
| - jasmine.Env.prototype.execute.call(this); |
34 |
| -}; |
35 |
| - |
36 |
| - |
37 |
| -var Reporter = function(onTestDone, onComplete){ |
38 |
| - this.onTestDone = onTestDone; |
39 |
| - this.onComplete = onComplete; |
40 |
| - this.reset(); |
41 |
| -}; |
42 |
| -jasmine.util.inherit(Reporter, jasmine.Reporter); |
43 |
| - |
44 |
| - |
45 |
| -Reporter.formatStack = function(stack) { |
46 |
| - var line, lines = (stack || '').split(/\r?\n/), l = lines.length, frames = []; |
47 |
| - for (var i = 0; i < l; i++){ |
48 |
| - line = lines[i]; |
49 |
| - if (line.match(/\/jasmine[\.-]/)) continue; |
50 |
| - frames.push(line.replace(/https?:\/\/\w+(:\d+)?\/test\//, '').replace(/^\s*/, ' ')); |
51 |
| - } |
52 |
| - return frames.join('\n'); |
53 |
| -}; |
54 |
| - |
55 |
| - |
56 |
| -Reporter.prototype.reset = function(){ |
57 |
| - this.specLog = jstestdriver.console.log_ = []; |
58 |
| -}; |
59 |
| - |
60 |
| - |
61 |
| -Reporter.prototype.log = function(str){ |
62 |
| - this.specLog.push(str); |
63 |
| -}; |
64 |
| - |
65 |
| - |
66 |
| -Reporter.prototype.reportSpecStarting = function(){ |
67 |
| - this.reset(); |
68 |
| - this.start = +new Date(); |
69 |
| -}; |
70 |
| - |
71 |
| - |
72 |
| -Reporter.prototype.reportSpecResults = function(spec){ |
73 |
| - var elapsed = +new Date() - this.start, results = spec.results(); |
74 |
| - |
75 |
| - if (results.skipped) return; |
76 |
| - |
77 |
| - var item, state = 'passed', items = results.getItems(), l = items.length, messages = []; |
78 |
| - for (var i = 0; i < l; i++){ |
79 |
| - item = items[i]; |
80 |
| - if (item.passed()) continue; |
81 |
| - state = (item.message.indexOf('AssertionError:') != -1) ? 'error' : 'failed'; |
82 |
| - messages.push({ |
83 |
| - message: item + '', |
84 |
| - name: item.trace.name, |
85 |
| - stack: Reporter.formatStack(item.trace.stack) |
86 |
| - }); |
87 |
| - } |
88 |
| - |
89 |
| - this.onTestDone(new jstestdriver.TestResult( |
90 |
| - spec.suite.getFullName(), |
91 |
| - spec.description, |
92 |
| - state, |
93 |
| - jstestdriver.angular.toJson(messages), |
94 |
| - this.specLog.join('\n'), |
95 |
| - elapsed |
96 |
| - )); |
97 |
| -}; |
98 |
| - |
99 |
| - |
100 |
| -Reporter.prototype.reportRunnerResults = function(){ |
101 |
| - this.onComplete(); |
102 |
| -}; |
103 |
| - |
104 |
| - |
105 |
| -var collectMode = true, intercepted = {}; |
106 |
| - |
107 |
| -describe = intercept('describe'); |
108 |
| -beforeEach = intercept('beforeEach'); |
109 |
| -afterEach = intercept('afterEach'); |
110 |
| - |
111 |
| -var JASMINE_TYPE = 'jasmine test case'; |
112 |
| -TestCase('Jasmine Adapter Tests', null, JASMINE_TYPE); |
113 |
| - |
114 |
| -jstestdriver.pluginRegistrar.register({ |
115 |
| - |
116 |
| - name: 'jasmine', |
117 |
| - |
118 |
| - getTestRunsConfigurationFor: function(testCaseInfos, expressions, testRunsConfiguration) { |
119 |
| - for (var i = 0; i < testCaseInfos.length; i++) { |
120 |
| - if (testCaseInfos[i].getType() == JASMINE_TYPE) { |
121 |
| - testRunsConfiguration.push(new jstestdriver.TestRunConfiguration(testCaseInfos[i], [])); |
122 |
| - } |
123 |
| - } |
124 |
| - return false; // allow other TestCases to be collected. |
125 |
| - }, |
126 |
| - |
127 |
| - runTestConfiguration: function(config, onTestDone, onComplete){ |
128 |
| - if (config.getTestCaseInfo().getType() != JASMINE_TYPE) return false; |
129 |
| - (jasmine.currentEnv_ = new Env(onTestDone, onComplete)).execute(); |
130 |
| - return true; |
131 |
| - }, |
132 |
| - |
133 |
| - onTestsFinish: function(){ |
134 |
| - jasmine.currentEnv_ = null; |
135 |
| - collectMode = true; |
136 |
| - } |
137 |
| - |
138 |
| -}); |
139 |
| - |
140 |
| -function intercept(method){ |
141 |
| - var bucket = intercepted[method] = [], method = window[method]; |
142 |
| - return function(desc, fn){ |
143 |
| - if (collectMode) bucket.push(function(){ method(desc, fn); }); |
144 |
| - else method(desc, fn); |
145 |
| - }; |
146 |
| -} |
147 |
| - |
148 |
| -function playback(){ |
149 |
| - for (var method in intercepted){ |
150 |
| - var bucket = intercepted[method]; |
151 |
| - for (var i = 0, l = bucket.length; i < l; i++) bucket[i](); |
152 |
| - } |
153 |
| -} |
154 |
| - |
155 |
| -})(); |
156 |
| - |
157 |
| -var ddescribe = function(name, fn){ |
158 |
| - var env = jasmine.getEnv(); |
159 |
| - if (!env.exclusive) env.exclusive = 1; // run ddescribe only |
160 |
| - describe(name, function(){ |
161 |
| - var oldIt = it; |
162 |
| - it = function(name, fn){ |
163 |
| - fn.exclusive = 1; // run anything under ddescribe |
164 |
| - env.it(name, fn); |
165 |
| - }; |
166 |
| - |
167 |
| - try { |
168 |
| - fn.call(this); |
169 |
| - } finally { |
170 |
| - it = oldIt; |
171 |
| - }; |
172 |
| - }); |
173 |
| -}; |
174 |
| - |
175 |
| -var iit = function(name, fn){ |
176 |
| - var env = jasmine.getEnv(); |
177 |
| - env.exclusive = fn.exclusive = 2; // run only iits |
178 |
| - env.it(name, fn); |
179 |
| -}; |
| 5 | +(function(window) { |
| 6 | + var rootDescribes = new Describes(window); |
| 7 | + var describePath = []; |
| 8 | + rootDescribes.collectMode(); |
| 9 | + |
| 10 | + var JASMINE_TYPE = 'jasmine test case'; |
| 11 | + TestCase('Jasmine Adapter Tests', null, JASMINE_TYPE); |
| 12 | + |
| 13 | + var jasminePlugin = { |
| 14 | + name:'jasmine', |
| 15 | + |
| 16 | + getTestRunsConfigurationFor: function(testCaseInfos, expressions, testRunsConfiguration) { |
| 17 | + for (var i = 0; i < testCaseInfos.length; i++) { |
| 18 | + if (testCaseInfos[i].getType() == JASMINE_TYPE) { |
| 19 | + testRunsConfiguration.push(new jstestdriver.TestRunConfiguration(testCaseInfos[i], [])); |
| 20 | + } |
| 21 | + } |
| 22 | + return false; |
| 23 | + }, |
| 24 | + |
| 25 | + runTestConfiguration: function(testRunConfiguration, onTestDone, onTestRunConfigurationComplete){ |
| 26 | + if (testRunConfiguration.getTestCaseInfo().getType() != JASMINE_TYPE) return false; |
| 27 | + |
| 28 | + var jasmineEnv = jasmine.currentEnv_ = new jasmine.Env(); |
| 29 | + rootDescribes.playback(); |
| 30 | + var specLog = jstestdriver.console.log_ = []; |
| 31 | + var start; |
| 32 | + jasmineEnv.specFilter = function(spec) { |
| 33 | + return rootDescribes.isExclusive(spec); |
| 34 | + }; |
| 35 | + jasmineEnv.reporter = { |
| 36 | + log: function(str){ |
| 37 | + specLog.push(str); |
| 38 | + }, |
| 39 | + |
| 40 | + reportRunnerStarting: function(runner) { }, |
| 41 | + |
| 42 | + reportSpecStarting: function(spec) { |
| 43 | + specLog = jstestdriver.console.log_ = []; |
| 44 | + start = new Date().getTime(); |
| 45 | + }, |
| 46 | + |
| 47 | + reportSpecResults: function(spec) { |
| 48 | + var suite = spec.suite; |
| 49 | + var results = spec.results(); |
| 50 | + if (results.skipped) return; |
| 51 | + var end = new Date().getTime(); |
| 52 | + var messages = []; |
| 53 | + var resultItems = results.getItems(); |
| 54 | + var state = 'passed'; |
| 55 | + for ( var i = 0; i < resultItems.length; i++) { |
| 56 | + if (!resultItems[i].passed()) { |
| 57 | + state = resultItems[i].message.match(/AssertionError:/) ? 'error' : 'failed'; |
| 58 | + messages.push({ |
| 59 | + message: resultItems[i].toString(), |
| 60 | + name: resultItems[i].trace.name, |
| 61 | + stack: formatStack(resultItems[i].trace.stack) |
| 62 | + }); |
| 63 | + } |
| 64 | + } |
| 65 | + onTestDone( |
| 66 | + new jstestdriver.TestResult( |
| 67 | + suite.getFullName(), |
| 68 | + spec.description, |
| 69 | + state, |
| 70 | + jstestdriver.angular.toJson(messages), |
| 71 | + specLog.join('\n'), |
| 72 | + end - start)); |
| 73 | + }, |
| 74 | + |
| 75 | + reportSuiteResults: function(suite) {}, |
| 76 | + |
| 77 | + reportRunnerResults: function(runner) { |
| 78 | + onTestRunConfigurationComplete(); |
| 79 | + } |
| 80 | + }; |
| 81 | + jasmineEnv.execute(); |
| 82 | + return true; |
| 83 | + }, |
| 84 | + |
| 85 | + onTestsFinish: function(){ |
| 86 | + jasmine.currentEnv_ = null; |
| 87 | + rootDescribes.collectMode(); |
| 88 | + } |
| 89 | + }; |
| 90 | + jstestdriver.pluginRegistrar.register(jasminePlugin); |
| 91 | + |
| 92 | + function formatStack(stack) { |
| 93 | + var lines = (stack||'').split(/\r?\n/); |
| 94 | + var frames = []; |
| 95 | + for (i = 0; i < lines.length; i++) { |
| 96 | + if (!lines[i].match(/\/jasmine[\.-]/)) { |
| 97 | + frames.push(lines[i].replace(/https?:\/\/\w+(:\d+)?\/test\//, '').replace(/^\s*/, ' ')); |
| 98 | + } |
| 99 | + } |
| 100 | + return frames.join('\n'); |
| 101 | + } |
| 102 | + |
| 103 | + function noop(){} |
| 104 | + function Describes(window){ |
| 105 | + var describes = {}; |
| 106 | + var beforeEachs = {}; |
| 107 | + var afterEachs = {}; |
| 108 | + // Here we store: |
| 109 | + // 0: everyone runs |
| 110 | + // 1: run everything under ddescribe |
| 111 | + // 2: run only iits (ignore ddescribe) |
| 112 | + var exclusive = 0; |
| 113 | + var collectMode = true; |
| 114 | + intercept('describe', describes); |
| 115 | + intercept('xdescribe', describes); |
| 116 | + intercept('beforeEach', beforeEachs); |
| 117 | + intercept('afterEach', afterEachs); |
| 118 | + |
| 119 | + function intercept(functionName, collection){ |
| 120 | + window[functionName] = function(desc, fn){ |
| 121 | + if (collectMode) { |
| 122 | + collection[desc] = function(){ |
| 123 | + jasmine.getEnv()[functionName](desc, fn); |
| 124 | + }; |
| 125 | + } else { |
| 126 | + jasmine.getEnv()[functionName](desc, fn); |
| 127 | + } |
| 128 | + }; |
| 129 | + } |
| 130 | + window.ddescribe = function(name, fn){ |
| 131 | + if (exclusive < 1) { |
| 132 | + exclusive = 1; // run ddescribe only |
| 133 | + } |
| 134 | + window.describe(name, function(){ |
| 135 | + var oldIt = window.it; |
| 136 | + window.it = function(name, fn){ |
| 137 | + fn.exclusive = 1; // run anything under ddescribe |
| 138 | + jasmine.getEnv().it(name, fn); |
| 139 | + }; |
| 140 | + try { |
| 141 | + fn.call(this); |
| 142 | + } finally { |
| 143 | + window.it = oldIt; |
| 144 | + }; |
| 145 | + }); |
| 146 | + }; |
| 147 | + window.iit = function(name, fn){ |
| 148 | + exclusive = fn.exclusive = 2; // run only iits |
| 149 | + jasmine.getEnv().it(name, fn); |
| 150 | + }; |
| 151 | + |
| 152 | + |
| 153 | + this.collectMode = function() { |
| 154 | + collectMode = true; |
| 155 | + exclusive = 0; // run everything |
| 156 | + }; |
| 157 | + this.playback = function(){ |
| 158 | + collectMode = false; |
| 159 | + playback(beforeEachs); |
| 160 | + playback(afterEachs); |
| 161 | + playback(describes); |
| 162 | + |
| 163 | + function playback(set) { |
| 164 | + for ( var name in set) { |
| 165 | + set[name](); |
| 166 | + } |
| 167 | + } |
| 168 | + }; |
| 169 | + |
| 170 | + this.isExclusive = function(spec) { |
| 171 | + if (exclusive) { |
| 172 | + var blocks = spec.queue.blocks; |
| 173 | + for ( var i = 0; i < blocks.length; i++) { |
| 174 | + if (blocks[i].func.exclusive >= exclusive) { |
| 175 | + return true; |
| 176 | + } |
| 177 | + } |
| 178 | + return false; |
| 179 | + } |
| 180 | + return true; |
| 181 | + }; |
| 182 | + } |
| 183 | + |
| 184 | +})(window); |
180 | 185 |
|
181 | 186 | // Patch Jasmine for proper stack traces
|
182 | 187 | jasmine.Spec.prototype.fail = function (e) {
|
183 |
| - var result = new jasmine.ExpectationResult({ |
184 |
| - passed: false, |
185 |
| - message: e ? jasmine.util.formatException(e) : 'Exception' |
186 |
| - }); |
187 |
| - if(e) result.trace = e; |
188 |
| - this.results_.addResult(result); |
| 188 | + var expectationResult = new jasmine.ExpectationResult({ |
| 189 | + passed: false, |
| 190 | + message: e ? jasmine.util.formatException(e) : 'Exception' |
| 191 | + }); |
| 192 | + // PATCH |
| 193 | + if (e) { |
| 194 | + expectationResult.trace = e; |
| 195 | + } |
| 196 | + this.results_.addResult(expectationResult); |
189 | 197 | };
|
| 198 | + |
0 commit comments