Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #69 from forcedotcom/maniax89-fix_mocha_fail_repor…
Browse files Browse the repository at this point in the history
…ting

accept PR68 and switch to using hierarchal suite names
  • Loading branch information
esalman-sfdc committed Mar 21, 2018
2 parents afd3377 + 74166d6 commit 9a182f9
Showing 1 changed file with 47 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,20 +138,23 @@
*/
$T._sfdxReportForJasmine = function (jasmine) {
var run_results_full = { "tests": [] };
var suiteDescription = '';
var suiteDescription = [];
var specStartTime = 0;

var sfdxReporter = {
suiteStarted: function(suite) {
suiteDescription = suite.description;
suiteDescription.push(suite.description);
},
suiteDone: function(suite) {
suiteDescription.pop();
},
specStarted: function(spec) {
specStartTime = Date.now();
},
specDone: function(spec) {
var fullReport = {};
var failedStr = '';
fullReport.FullName = suiteDescription + ' : ' + spec.description;
fullReport.FullName = suiteDescription.join(' : ') + ' : ' + spec.description;
fullReport.Outcome = 'Pass';
fullReport.RunTime = Date.now() - specStartTime;

Expand All @@ -168,7 +171,6 @@
console.log('failed!', fullReport.FullName, '- ' + fullReport.RunTime + 'ms');
for (var i = 0, failure; i < spec.failedExpectations.length; i++) {
var failureMessage = spec.failedExpectations[i].message;
failureMessage.replace(/^\s+/, "" ).replace(/\s+$/, "" );
failedStr += '\n ' + failureMessage;
}

Expand All @@ -190,30 +192,52 @@
*/
$T._sfdxReportForMocha = function (mochaRunner) {
var run_results_full = { "tests": [] };
mochaRunner.on('test end', function (test) {

var fullReport = {};
fullReport.FullName = test.parent.title + ' : ' + test.title;
fullReport.Outcome = 'Pass';
fullReport.RunTime = test.duration;

if ('passed' === test.state) {
console.log('passed!', test.title, '- ' + fullReport.RunTime + 'ms')
} else if (test.pending) {
console.log('pending!', test.title, '- ' + fullReport.RunTime + 'ms')
fullReport.Outcome = 'Skip';
fullReport.Message = ' # SKIP disabled by xit or similar';
} else {
console.log('fail!', test.title, '- ' + fullReport.RunTime + 'ms')
fullReport.Outcome = 'Fail';
fullReport.Message = test.err.message;
}
run_results_full.tests.push(fullReport);
mochaRunner.on('pass', function (test) {
run_results_full.tests.push(convertMochaTestEventToSfdxTestResult(test));
}).on('fail', function (test) {
run_results_full.tests.push(convertMochaTestEventToSfdxTestResult(test));
}).on('pending', function (test) {
var pendingTest = Object.assign({}, test, {
err: {
message: ' # SKIP disabled by xit or similar'
},
duration: 0,
state: 'pending'
});
run_results_full.tests.push(convertMochaTestEventToSfdxTestResult(pendingTest));
}).on('end', function (suite) {
setHiddenDivContent("run_results_full", JSON.stringify(run_results_full));
});
}

/**
* Turns a mocha test event into a sfdx test result
*/
var convertMochaTestEventToSfdxTestResult = function (test) {
console.log(test.state + '!', test.title, '-' + test.duration + 'ms');
return {
FullName: generateMochaTestTitleForSfdx(test),
Outcome: mochaStateToOutcome[test.state],
RunTime: test.duration,
Message: test.err ? test.err.message : undefined
};
};

var generateMochaTestTitleForSfdx = function (test) {
var sfdxTitle = test.title;
while (!test.parent.root) {
sfdxTitle = test.parent.title + ' : ' + sfdxTitle;
test = test.parent;
}
return sfdxTitle.replace(/["]/g, "'");
}

var mochaStateToOutcome = {
'passed': 'Pass',
'failed': 'Failed',
'pending': 'Skip'
};

var setHiddenDivContent = function (id, content) {
var aDiv = document.getElementById(id);

Expand Down

0 comments on commit 9a182f9

Please sign in to comment.