|
| 1 | +/* |
| 2 | + This file is part of the Jasmine JSReporter project from Ivan De Marino. |
| 3 | +
|
| 4 | + Copyright (C) 2011 Ivan De Marino (aka detro, aka detronizator), http://blog.ivandemarino.me, [email protected] |
| 5 | +
|
| 6 | + Redistribution and use in source and binary forms, with or without |
| 7 | + modification, are permitted provided that the following conditions are met: |
| 8 | +
|
| 9 | + * Redistributions of source code must retain the above copyright |
| 10 | + notice, this list of conditions and the following disclaimer. |
| 11 | + * Redistributions in binary form must reproduce the above copyright |
| 12 | + notice, this list of conditions and the following disclaimer in the |
| 13 | + documentation and/or other materials provided with the distribution. |
| 14 | + * Neither the name of the <organization> nor the |
| 15 | + names of its contributors may be used to endorse or promote products |
| 16 | + derived from this software without specific prior written permission. |
| 17 | +
|
| 18 | + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | + ARE DISCLAIMED. IN NO EVENT SHALL IVAN DE MARINO BE LIABLE FOR ANY |
| 22 | + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 | + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 24 | + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 25 | + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 27 | + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | +*/ |
| 29 | + |
| 30 | +(function () { |
| 31 | + // Ensure that Jasmine library is loaded first |
| 32 | + if (typeof jasmine === "undefined") { |
| 33 | + throw new Error("[Jasmine JSReporter] 'Jasmine' library not found"); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Calculate elapsed time, in Seconds. |
| 38 | + * @param startMs Start time in Milliseconds |
| 39 | + * @param finishMs Finish time in Milliseconds |
| 40 | + * @return Elapsed time in Seconds */ |
| 41 | + function elapsedSec (startMs, finishMs) { |
| 42 | + return (finishMs - startMs) / 1000; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Round an amount to the given number of Digits. |
| 47 | + * If no number of digits is given, than '2' is assumed. |
| 48 | + * @param amount Amount to round |
| 49 | + * @param numOfDecDigits Number of Digits to round to. Default value is '2'. |
| 50 | + * @return Rounded amount */ |
| 51 | + function round (amount, numOfDecDigits) { |
| 52 | + numOfDecDigits = numOfDecDigits || 2; |
| 53 | + return Math.round(amount * Math.pow(10, numOfDecDigits)) / Math.pow(10, numOfDecDigits); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Collect information about a Suite, recursively, and return a JSON result. |
| 58 | + * @param suite The Jasmine Suite to get data from |
| 59 | + */ |
| 60 | + function getSuiteData (suite) { |
| 61 | + var suiteData = { |
| 62 | + description : suite.description, |
| 63 | + durationSec : 0, |
| 64 | + specs: [], |
| 65 | + suites: [], |
| 66 | + passed: true |
| 67 | + }, |
| 68 | + specs = suite.specs(), |
| 69 | + suites = suite.suites(), |
| 70 | + i, ilen; |
| 71 | + |
| 72 | + // Loop over all the Suite's Specs |
| 73 | + for (i = 0, ilen = specs.length; i < ilen; ++i) { |
| 74 | + suiteData.specs[i] = { |
| 75 | + description : specs[i].description, |
| 76 | + durationSec : specs[i].durationSec, |
| 77 | + passed : specs[i].results().passedCount === specs[i].results().totalCount, |
| 78 | + skipped : specs[i].results().skipped, |
| 79 | + passedCount : specs[i].results().passedCount, |
| 80 | + failedCount : specs[i].results().failedCount, |
| 81 | + totalCount : specs[i].results().totalCount |
| 82 | + }; |
| 83 | + suiteData.passed = !suiteData.specs[i].passed ? false : suiteData.passed; |
| 84 | + suiteData.durationSec += suiteData.specs[i].durationSec; |
| 85 | + } |
| 86 | + |
| 87 | + // Loop over all the Suite's sub-Suites |
| 88 | + for (i = 0, ilen = suites.length; i < ilen; ++i) { |
| 89 | + suiteData.suites[i] = getSuiteData(suites[i]); //< recursive population |
| 90 | + suiteData.passed = !suiteData.suites[i].passed ? false : suiteData.passed; |
| 91 | + suiteData.durationSec += suiteData.suites[i].durationSec; |
| 92 | + } |
| 93 | + |
| 94 | + // Rounding duration numbers to 3 decimal digits |
| 95 | + suiteData.durationSec = round(suiteData.durationSec, 4); |
| 96 | + |
| 97 | + return suiteData; |
| 98 | + } |
| 99 | + |
| 100 | + var JSReporter = function () { |
| 101 | + }; |
| 102 | + |
| 103 | + JSReporter.prototype = { |
| 104 | + reportRunnerStarting: function (runner) { |
| 105 | + // Nothing to do |
| 106 | + }, |
| 107 | + |
| 108 | + reportSpecStarting: function (spec) { |
| 109 | + // Start timing this spec |
| 110 | + spec.startedAt = new Date(); |
| 111 | + }, |
| 112 | + |
| 113 | + reportSpecResults: function (spec) { |
| 114 | + // Finish timing this spec and calculate duration/delta (in sec) |
| 115 | + spec.finishedAt = new Date(); |
| 116 | + spec.durationSec = elapsedSec(spec.startedAt.getTime(), spec.finishedAt.getTime()); |
| 117 | + }, |
| 118 | + |
| 119 | + reportSuiteResults: function (suite) { |
| 120 | + // Nothing to do |
| 121 | + }, |
| 122 | + |
| 123 | + reportRunnerResults: function (runner) { |
| 124 | + var suites = runner.suites(), |
| 125 | + i, ilen; |
| 126 | + |
| 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 | + |
| 134 | + // Loop over all the Suites |
| 135 | + for (i = 0, ilen = suites.length; i < ilen; ++i) { |
| 136 | + if (suites[i].parentSuite === null) { |
| 137 | + jasmine.runnerResults.suites[i] = getSuiteData(suites[i]); |
| 138 | + // If 1 suite fails, the whole runner fails |
| 139 | + jasmine.runnerResults.passed = !jasmine.runnerResults.suites[i].passed ? false : jasmine.runnerResults.passed; |
| 140 | + // Add up all the durations |
| 141 | + jasmine.runnerResults.durationSec += jasmine.runnerResults.suites[i].durationSec; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + // Decorate the 'jasmine' object with getters |
| 146 | + jasmine.getJSReport = function () { |
| 147 | + if (jasmine.runnerResults) { |
| 148 | + return jasmine.runnerResults; |
| 149 | + } |
| 150 | + return null; |
| 151 | + }; |
| 152 | + jasmine.getJSReportAsString = function () { |
| 153 | + return JSON.stringify(jasmine.getJSReport()); |
| 154 | + }; |
| 155 | + } |
| 156 | + }; |
| 157 | + |
| 158 | + // export public |
| 159 | + jasmine.JSReporter = JSReporter; |
| 160 | +})(); |
| 161 | + |
0 commit comments