jasmine-jsreporter.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. This file is part of the Jasmine JSReporter project from Ivan De Marino.
  3. Copyright (C) 2011-2014 Ivan De Marino <http://ivandemarino.me>
  4. Copyright (C) 2014 Alex Treppass <http://alextreppass.co.uk>
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are met:
  7. * Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. * Neither the name of the <organization> nor the
  13. names of its contributors may be used to endorse or promote products
  14. derived from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. ARE DISCLAIMED. IN NO EVENT SHALL IVAN DE MARINO BE LIABLE FOR ANY
  19. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. (function (jasmine) {
  27. if (!jasmine) {
  28. throw new Error("[Jasmine JSReporter] 'Jasmine' library not found");
  29. }
  30. // ------------------------------------------------------------------------
  31. // Jasmine JSReporter for Jasmine 1.x
  32. // ------------------------------------------------------------------------
  33. /**
  34. * Calculate elapsed time, in Seconds.
  35. * @param startMs Start time in Milliseconds
  36. * @param finishMs Finish time in Milliseconds
  37. * @return Elapsed time in Seconds */
  38. function elapsedSec(startMs, finishMs) {
  39. return (finishMs - startMs) / 1000;
  40. }
  41. /**
  42. * Round an amount to the given number of Digits.
  43. * If no number of digits is given, than '2' is assumed.
  44. * @param amount Amount to round
  45. * @param numOfDecDigits Number of Digits to round to. Default value is '2'.
  46. * @return Rounded amount */
  47. function round(amount, numOfDecDigits) {
  48. numOfDecDigits = numOfDecDigits || 2;
  49. return Math.round(amount * Math.pow(10, numOfDecDigits)) / Math.pow(10, numOfDecDigits);
  50. }
  51. /**
  52. * Create a new array which contains only the failed items.
  53. * @param items Items which will be filtered
  54. * @returns {Array} of failed items */
  55. function failures(items) {
  56. var fs = [], i, v;
  57. for (i = 0; i < items.length; i += 1) {
  58. v = items[i];
  59. if (!v.passed_) {
  60. fs.push(v);
  61. }
  62. }
  63. return fs;
  64. }
  65. /**
  66. * Collect information about a Suite, recursively, and return a JSON result.
  67. * @param suite The Jasmine Suite to get data from
  68. */
  69. function getSuiteData(suite) {
  70. var suiteData = {
  71. description : suite.description,
  72. durationSec : 0,
  73. specs: [],
  74. suites: [],
  75. passed: true
  76. },
  77. specs = suite.specs(),
  78. suites = suite.suites(),
  79. i, ilen;
  80. // Loop over all the Suite's Specs
  81. for (i = 0, ilen = specs.length; i < ilen; ++i) {
  82. suiteData.specs[i] = {
  83. description : specs[i].description,
  84. durationSec : specs[i].durationSec,
  85. passed : specs[i].results().passedCount === specs[i].results().totalCount,
  86. skipped : specs[i].results().skipped,
  87. passedCount : specs[i].results().passedCount,
  88. failedCount : specs[i].results().failedCount,
  89. totalCount : specs[i].results().totalCount,
  90. failures: failures(specs[i].results().getItems())
  91. };
  92. suiteData.passed = !suiteData.specs[i].passed ? false : suiteData.passed;
  93. suiteData.durationSec += suiteData.specs[i].durationSec;
  94. }
  95. // Loop over all the Suite's sub-Suites
  96. for (i = 0, ilen = suites.length; i < ilen; ++i) {
  97. suiteData.suites[i] = getSuiteData(suites[i]); //< recursive population
  98. suiteData.passed = !suiteData.suites[i].passed ? false : suiteData.passed;
  99. suiteData.durationSec += suiteData.suites[i].durationSec;
  100. }
  101. // Rounding duration numbers to 3 decimal digits
  102. suiteData.durationSec = round(suiteData.durationSec, 4);
  103. return suiteData;
  104. }
  105. var JSReporter = function () {
  106. };
  107. JSReporter.prototype = {
  108. reportRunnerStarting: function (runner) {
  109. // Nothing to do
  110. },
  111. reportSpecStarting: function (spec) {
  112. // Start timing this spec
  113. spec.startedAt = new Date();
  114. },
  115. reportSpecResults: function (spec) {
  116. // Finish timing this spec and calculate duration/delta (in sec)
  117. spec.finishedAt = new Date();
  118. // If the spec was skipped, reportSpecStarting is never called and spec.startedAt is undefined
  119. spec.durationSec = spec.startedAt ? elapsedSec(spec.startedAt.getTime(), spec.finishedAt.getTime()) : 0;
  120. },
  121. reportSuiteResults: function (suite) {
  122. // Nothing to do
  123. },
  124. reportRunnerResults: function (runner) {
  125. var suites = runner.suites(),
  126. i, j, ilen;
  127. // Attach results to the "jasmine" object to make those results easy to scrap/find
  128. jasmine.runnerResults = {
  129. suites: [],
  130. durationSec : 0,
  131. passed : true
  132. };
  133. // Loop over all the Suites
  134. for (i = 0, ilen = suites.length, j = 0; i < ilen; ++i) {
  135. if (suites[i].parentSuite === null) {
  136. jasmine.runnerResults.suites[j] = getSuiteData(suites[i]);
  137. // If 1 suite fails, the whole runner fails
  138. jasmine.runnerResults.passed = !jasmine.runnerResults.suites[j].passed ? false : jasmine.runnerResults.passed;
  139. // Add up all the durations
  140. jasmine.runnerResults.durationSec += jasmine.runnerResults.suites[j].durationSec;
  141. j++;
  142. }
  143. }
  144. // Decorate the 'jasmine' object with getters
  145. jasmine.getJSReport = function () {
  146. if (jasmine.runnerResults) {
  147. return jasmine.runnerResults;
  148. }
  149. return null;
  150. };
  151. jasmine.getJSReportAsString = function () {
  152. return JSON.stringify(jasmine.getJSReport());
  153. };
  154. }
  155. };
  156. // export public
  157. jasmine.JSReporter = JSReporter;
  158. // ------------------------------------------------------------------------
  159. // Jasmine JSReporter for Jasmine 2.0
  160. // ------------------------------------------------------------------------
  161. /*
  162. Simple timer implementation
  163. */
  164. var Timer = function () {};
  165. Timer.prototype.start = function () {
  166. this.startTime = new Date().getTime();
  167. return this;
  168. };
  169. Timer.prototype.elapsed = function () {
  170. if (this.startTime == null) {
  171. return -1;
  172. }
  173. return new Date().getTime() - this.startTime;
  174. };
  175. /*
  176. Utility methods
  177. */
  178. var _extend = function (obj1, obj2) {
  179. for (var prop in obj2) {
  180. obj1[prop] = obj2[prop];
  181. }
  182. return obj1;
  183. };
  184. var _clone = function (obj) {
  185. if (obj !== Object(obj)) {
  186. return obj;
  187. }
  188. return _extend({}, obj);
  189. };
  190. jasmine.JSReporter2 = function () {
  191. this.specs = {};
  192. this.suites = {};
  193. this.rootSuites = [];
  194. this.suiteStack = [];
  195. // export methods under jasmine namespace
  196. jasmine.getJSReport = this.getJSReport;
  197. jasmine.getJSReportAsString = this.getJSReportAsString;
  198. };
  199. var JSR = jasmine.JSReporter2.prototype;
  200. // Reporter API methods
  201. // --------------------
  202. JSR.suiteStarted = function (suite) {
  203. suite = this._cacheSuite(suite);
  204. // build up suite tree as we go
  205. suite.specs = [];
  206. suite.suites = [];
  207. suite.passed = true;
  208. suite.parentId = this.suiteStack.slice(this.suiteStack.length - 1)[0];
  209. if (suite.parentId) {
  210. this.suites[suite.parentId].suites.push(suite);
  211. } else {
  212. this.rootSuites.push(suite.id);
  213. }
  214. this.suiteStack.push(suite.id);
  215. suite.timer = new Timer().start();
  216. };
  217. JSR.suiteDone = function (suite) {
  218. suite = this._cacheSuite(suite);
  219. suite.duration = suite.timer.elapsed();
  220. suite.durationSec = suite.duration / 1000;
  221. this.suiteStack.pop();
  222. // maintain parent suite state
  223. var parent = this.suites[suite.parentId];
  224. if (parent) {
  225. parent.passed = parent.passed && suite.passed;
  226. }
  227. // keep report representation clean
  228. delete suite.timer;
  229. delete suite.id;
  230. delete suite.parentId;
  231. delete suite.fullName;
  232. };
  233. JSR.specStarted = function (spec) {
  234. spec = this._cacheSpec(spec);
  235. spec.timer = new Timer().start();
  236. // build up suites->spec tree as we go
  237. spec.suiteId = this.suiteStack.slice(this.suiteStack.length - 1)[0];
  238. this.suites[spec.suiteId].specs.push(spec);
  239. };
  240. JSR.specDone = function (spec) {
  241. spec = this._cacheSpec(spec);
  242. spec.duration = spec.timer.elapsed();
  243. spec.durationSec = spec.duration / 1000;
  244. spec.skipped = spec.status === 'pending';
  245. spec.passed = spec.skipped || spec.status === 'passed';
  246. spec.totalCount = spec.passedExpectations.length + spec.failedExpectations.length;
  247. spec.passedCount = spec.passedExpectations.length;
  248. spec.failedCount = spec.failedExpectations.length;
  249. spec.failures = [];
  250. for (var i = 0, j = spec.failedExpectations.length; i < j; i++) {
  251. var fail = spec.failedExpectations[i];
  252. spec.failures.push({
  253. type: 'expect',
  254. expected: fail.expected,
  255. passed: false,
  256. message: fail.message,
  257. matcherName: fail.matcherName,
  258. trace: {
  259. stack: fail.stack
  260. }
  261. });
  262. }
  263. // maintain parent suite state
  264. var parent = this.suites[spec.suiteId];
  265. if (spec.failed) {
  266. parent.failingSpecs.push(spec);
  267. }
  268. parent.passed = parent.passed && spec.passed;
  269. // keep report representation clean
  270. delete spec.timer;
  271. delete spec.totalExpectations;
  272. delete spec.passedExpectations;
  273. delete spec.suiteId;
  274. delete spec.fullName;
  275. delete spec.id;
  276. delete spec.status;
  277. delete spec.failedExpectations;
  278. };
  279. JSR.jasmineDone = function () {
  280. this._buildReport();
  281. };
  282. JSR.getJSReport = function () {
  283. if (jasmine.jsReport) {
  284. return jasmine.jsReport;
  285. }
  286. };
  287. JSR.getJSReportAsString = function () {
  288. if (jasmine.jsReport) {
  289. return JSON.stringify(jasmine.jsReport);
  290. }
  291. };
  292. // Private methods
  293. // ---------------
  294. JSR._haveSpec = function (spec) {
  295. return this.specs[spec.id] != null;
  296. };
  297. JSR._cacheSpec = function (spec) {
  298. var existing = this.specs[spec.id];
  299. if (existing == null) {
  300. existing = this.specs[spec.id] = _clone(spec);
  301. } else {
  302. _extend(existing, spec);
  303. }
  304. return existing;
  305. };
  306. JSR._haveSuite = function (suite) {
  307. return this.suites[suite.id] != null;
  308. };
  309. JSR._cacheSuite = function (suite) {
  310. var existing = this.suites[suite.id];
  311. if (existing == null) {
  312. existing = this.suites[suite.id] = _clone(suite);
  313. } else {
  314. _extend(existing, suite);
  315. }
  316. return existing;
  317. };
  318. JSR._buildReport = function () {
  319. var overallDuration = 0;
  320. var overallPassed = true;
  321. var overallSuites = [];
  322. for (var i = 0, j = this.rootSuites.length; i < j; i++) {
  323. var suite = this.suites[this.rootSuites[i]];
  324. overallDuration += suite.duration;
  325. overallPassed = overallPassed && suite.passed;
  326. overallSuites.push(suite);
  327. }
  328. jasmine.jsReport = {
  329. passed: overallPassed,
  330. durationSec: overallDuration / 1000,
  331. suites: overallSuites
  332. };
  333. };
  334. })(jasmine);