Coder Social home page Coder Social logo

villedemontreal / mocha-error-reporters Goto Github PK

View Code? Open in Web Editor NEW
2.0 6.0 0.0 69 KB

mocha-error-reporters includes the full error object as JSON in the test report when an error has some meaningful extra information.

License: MIT License

JavaScript 100.00%
mocha reporter error unexpected unhandled full complete

mocha-error-reporters's Introduction

(French version)

mocha-error-reporters

When a test fails due to an unexpected error, mocha-error-reporters appends the full error in JSON format to the test report.

Test report example

1 passing (5ms)
  1 failing

  1) Test Example
       Test with error:
     Error: Pow!
      at Context.it (test/testData/testExample.js:9:17)
  
  Full Error:
  {
    "additionnalAttribute": "The key to understand this bug",
    "name": "Error",
    "message": "Pow!",
    "stack": "Error: Pow!\n    at Context.it (test/testData/testExample.js:9:17)"
  }

Without mocha-error-reporters, the Full Error section would ne be present and it would be impossible to read additionnalAttribute which may be the key to understand this bug. All additionnal attributes (ex: inner error) will be fully serialized; thanks to the npm package serialize-error.

To Install

In your project, run this npm command:

npm install mocha-error-reporters --save-dev

Usage

Call mocha with:

mocha --require mocha-error-reporters --reporter=specWithFullErrors

mocha-error-reporters provides 8 reporters:

Custom reporters

Mocha allows you to define custom reporters.

The code snippet below shows how unexpected errors can be serialized in your custom reporters.

const { extendReporterWithFullErrors } = require('mocha-error-reporters');
const yourCustomReporter;
const yourCustomReporterWithFullErrors = extendReporterWithFullErrors(yourCustomReporter);

License

The source code of this project is distributed under the MIT License.

Contributing

See CONTRIBUTING.md.

Code of Conduct

Participation in this poject is governed by the Code of Conduct.

mocha-error-reporters's People

Contributors

psionikangel avatar steph-lebl avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

mocha-error-reporters's Issues

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

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;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.