VilledeMontreal / mocha-error-reporters

mocha-error-reporters includes the full error object as JSON in the test report when an error has some meaningful extra information.
MIT License
2 stars 0 forks source link

`extendReporterWithFullErrors` is incompatible with `mocha-junit-reporter` #1

Open bericp1 opened 3 years ago

bericp1 commented 3 years ago

Hey all! Firstly, thanks for the helpful util!

One note though is that in its current form, extendReporterWithFullErrors is incompatible with the popular mocha-junit-reporter. Since extendReporterWithFullErrors calls superReporter.call(this, runner, options); before calling withFullErrors, the JUnit reporter attaches its fail handler first which serializes the error to a string for the later-generated XML before addFullErrorIfMeaningful is ever called to modify error.stack.

I fixed this by adding the fail handler before calling the JUnit constructor:

junitWithFullErrors.js:

const { inherits } = require('util');
const addFullErrorIfMeaningful = require('mocha-error-reporters/lib/addFullErrorIfMeaningful');
const JUnitReporter = require('mocha-junit-reporter');

function JUnitReporterWithFullErrors(runner, options) {
  // `mocha-error-reporters` has a `extendReporterWithFullErrors` util but we manually pull in
  // `addFullErrorIfMeaningful` and extend the JUnit reporter in order to ensure that our
  // `.on('fail', ...)` is called before JUnit's `.on('fail', ...` so that the full error data is
  // appended to `error.stack` before JUnit serializes it to XML.
  runner.on('fail', (test, error) => addFullErrorIfMeaningful(error));
  JUnitReporter.call(this, runner, options);
}
inherits(JUnitReporterWithFullErrors, JUnitReporter);

module.exports = JUnitReporterWithFullErrors;
steph-lebl commented 3 years ago

Hi Brandon, thanks for the positive feedback and for the junit reporter workaround!

Le mar. 21 sept. 2021 à 14:58, Brandon Phillips @.***> a écrit :

Hey all! Firstly, thanks for the helpful util!

One note though is that in its current form, extendReporterWithFullErrors is incompatible with the popular mocha-junit-reporter https://www.npmjs.com/package/mocha-junit-reporter. Since extendReporterWithFullErrors calls superReporter.call(this, runner, options); before calling withFullErrors, the JUnit reporter attaches its fail handler first which serializes the error to a string for the later-generated XML before addFullErrorIfMeaningful is ever called to modify error.stack.

I fixed this by adding the fail handler before calling the JUnit constructor:

junitWithFullErrors.js:

const { inherits } = require('util');const addFullErrorIfMeaningful = require('mocha-error-reporters/lib/addFullErrorIfMeaningful');const JUnitReporter = require('mocha-junit-reporter'); function JUnitReporterWithFullErrors(runner, options) { // mocha-error-reporters has a extendReporterWithFullErrors util but we manually pull in // addFullErrorIfMeaningful and extend the JUnit reporter in order to ensure that our // .on('fail', ...) is called before JUnit's .on('fail', ... so that the full error data is // appended to error.stack before JUnit serializes it to XML. runner.on('fail', (test, error) => addFullErrorIfMeaningful(error)); JUnitReporter.call(this, runner, options);}inherits(JUnitReporterWithFullErrors, JUnitReporter); module.exports = JUnitReporterWithFullErrors;

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/VilledeMontreal/mocha-error-reporters/issues/1, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB6M4XZRM6NN234POM2XIELUDDIWFANCNFSM5EPLNUOA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.