forcedotcom / LightningTestingService

Apache License 2.0
122 stars 35 forks source link

mocha hooks failures not reported #65

Closed maniax89 closed 6 years ago

maniax89 commented 6 years ago

According to https://github.com/mochajs/mocha/blob/master/lib/runner.js#L42-L54 there are a few events that can happen.

Currently, the mocha suite test reporter (https://github.com/forcedotcom/LightningTestingService/blob/master/lightning-component-tests/test/default/staticresources/lts_testutil.resource#L193-L212) uses the following event:

mochaRunner.on('test end', function (test) {

unfortunately, this does not include hook failures in the report.

My proposal would be to switch out the logic to something more like:

$T._convertMochaEventToTestResult = function(test) {
  console.log(test.state, test.title, '-' + test.duration + 'ms');
  return {
    FullName: test.parent.title + ' : ' + test.title,
    Outcome: test.state,
    RunTime: test.duration,
    Message: test.err ? test.err.message : null
  };
}

$T._sfdxReportForMocha = function (mochaRunner) {
        var run_results_full = { "tests": [] };
        mochaRunner.on('fail', function(test) {
            run_results_full.tests.push($T._convertMochaEventToTestResult(test));
        }).on('pass', function(test) {
            run_results_full.tests.push($T._convertMochaEventToTestResult(test));
        }).on('pending', function(test) {
            test.err = {
              message: ' # SKIP disabled by xit or similar'
            };
            run_results_full.tests.push($T._convertMochaEventToTestResult(test));
        }).on('end', function (suite) {
            setHiddenDivContent("run_results_full", JSON.stringify(run_results_full));
        });
    }

this captures both test failures and beforeEach failures

I can try to get a PR up sometime if others think this is useful

esalman-sfdc commented 6 years ago

Hey @maniax89 with the current approach, is the problem that beforeEach failures are not showing up? Asking because I would have thought that 'test end' would encompass that.

maniax89 commented 6 years ago

@esalman-sfdc ^ exactly, the test never runs because the beforeEach fails, thus 'test end' is never invoked

maniax89 commented 6 years ago

this seems to highlight the issue at least for afterEach hooks -> https://github.com/mochajs/mocha/issues/1860#issuecomment-139072862

esalman-sfdc commented 6 years ago

Good find @maniax89 and thanks for the pull request. We'll try reviewing/merging soon.

esalman-sfdc commented 6 years ago

PR accepted. Next package release will have the changes.