Skip to content

Commit

Permalink
handle nested describes
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Gruber committed Jul 14, 2014
1 parent 3e450e5 commit e25a5eb
Showing 1 changed file with 72 additions and 70 deletions.
142 changes: 72 additions & 70 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
});
}

Expand All @@ -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];
}
});
}

/**
Expand Down Expand Up @@ -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();
}
}

0 comments on commit e25a5eb

Please sign in to comment.