diff --git a/lib/index.js b/lib/index.js index 879662d..659a8de 100755 --- a/lib/index.js +++ b/lib/index.js @@ -62,61 +62,8 @@ function Mochawesome (runner) { var allSuites = {}, allTests = [], allFailures = [], - allPasses = []; - - runner.on('suite', function (suite) { - // ignore the root suite - if (!suite.root) { - // top level suite - if (suite.parent.root) { - allSuites[suite.title] = {}; - } - - // nested suite 1 level deep only - if (!suite.parent.root && suite.parent.parent.root) { - parentSuite = allSuites[suite.parent.title]; - if (!parentSuite.childSuites) { - parentSuite.childSuites = {}; - } - parentSuite.childSuites[suite.title] = {}; - } - } - }); - - runner.on('suite end', function (suite) { - // ignore the root suite - if (!suite.root) { - - var allTests = suite.tests.map(cleanTest); - var passingTests = allTests.filter(filterTest.bind(self, true)); - var failingTests = allTests.filter(filterTest.bind(self, false)); - - // TODO: account for child suites in suite totals - var suiteObj = { - title: suite.title, - fullTitle: suite.fullTitle(), - file: suite.file.replace(process.cwd(), ''), - fullFile: suite.file, - tests: allTests, - passes: passingTests, - failures: failingTests, - totalTests: suite.tests.length, - totalPasses: passingTests.length, - totalFailures: failingTests.length - }; - - // top level suite - if (suite.parent.root) { - allSuites[suite.title] = _.merge(allSuites[suite.title], suiteObj); - } - - // nested suite 1 level deep only - if (!suite.parent.root && suite.parent.parent.root) { - parentSuite = allSuites[suite.parent.title]; - parentSuite.childSuites[suite.title] = suiteObj; - } - } - }); + allPasses = [], + endCalled = false; runner.on('test end', function (test) { allTests.push(test); @@ -131,20 +78,27 @@ function Mochawesome (runner) { }); runner.on('end', function () { + if (!endCalled) { + endCalled = true; // end gets called more than once for some reason so this ensures we only do this once + + allSuites = self.runner.suite; + removeAllPropsFromObjExcept(allSuites, ['suites', 'root']); + cleanSuiteObj(allSuites); + + // unless we render client-side, this has to be updated client-side + // self.stats.endDateStr = moment(self.stats.end).fromNow(); + + var obj = { + stats: self.stats, + suites: allSuites.suites, + tests: allTests.map(cleanTest), + passes: allPasses.map(cleanTest), + failures: allFailures.map(cleanTest) + }; - // unless we render client-side, this has to be updated client-side - // self.stats.endDateStr = moment(self.stats.end).fromNow(); - - var obj = { - stats: self.stats, - suites: allSuites, - tests: allTests.map(cleanTest), - passes: allPasses.map(cleanTest), - failures: allFailures.map(cleanTest) - }; - - saveToFile('json', obj); - saveToFile('html', obj); + saveToFile('json', obj); + saveToFile('html', obj); + } }); } @@ -170,8 +124,12 @@ function cleanTest (test) { } } -function filterTest (passes, test) { - return test.state === (passes ? 'passed' : 'failed'); +function removeAllPropsFromObjExcept(obj, propsToKeep) { + _.forOwn(obj, function(val, prop) { + if (propsToKeep.indexOf(prop) === -1) { + delete obj[prop]; + } + }); } /** @@ -235,5 +193,49 @@ function saveToFile (filetype, inData) { console.log(err); util.print("\nError: Unable to save " + outFile + "\n"); } +} +function formatSuiteObject (suite) { + var cleanTests = _.map(suite.tests, cleanTest); + var passingTests = _.where(cleanTests, {state: 'passed'}); + var failingTests = _.where(cleanTests, {state: 'failed'}); + + suite.tests = cleanTests; + // suite.fullTitle = suite.fullTitle(); + file: suite.file.replace(process.cwd(), ''); + fullFile: suite.file; + passes: passingTests; + failures: failingTests; + totalTests: suite.tests.length; + totalPasses: passingTests.length; + totalFailures: failingTests.length; + + removeAllPropsFromObjExcept(suite, [ + 'title', + 'tests', + 'suites', + 'fullTitle', + 'file', + 'fullFile', + 'passes', + 'failures', + 'totalTests', + 'totalPasses', + 'totalFailures' + ]); +} + +function cleanSuiteObj (suite) { + var queue = [], + next = suite; + while (next) { + console.log("cleaning suite:", next.title); + if (next.suites.length) { + _.each(next.suites, function(suite, i) { + formatSuiteObject(suite); + queue.push(suite); + }); + } + next = queue.shift(); + } } \ No newline at end of file