Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using HTML reporting with Mocha test framework

I've been generating some tests using NodeJS and Mocha, and I'd like to find a way to place the results into a browser. I know that Mocha has support for this using 'html' reporter and mocha init <dir> however neither seem to be working for me (the reporter actually throws errors without even running a test).

Could someone give me a good example of running a test via Mocha and generating a HTML report?An example I want to mimic is on the visionmedia site. Also, for examples sake we'll say I'm using a test file called example.js.

Thanks in advance for any assistance, it's surprising there are so few example pieces around.

like image 847
whitfin Avatar asked Nov 14 '13 09:11

whitfin


People also ask

Which of the following is considered as the default test reporter of Mocha?

Spec is the default reporter in Mocha. The Spec Mocha reporter generates the report as a hierarchical view. The Spec reports are nested based on test cases.

Is Mocha a testing framework?

Mocha is a feature-rich JavaScript test framework running on Node. js and in the browser, making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases. Hosted on GitHub.

How do you create a report in Cypress?

Steps to create Junit Reports in Cypress To solve this problem and wish to have a unique report for individual spec files, we have to add [hash] in the mochaFile parameter in cypress. json. After running this command, the User can see the results folder inside the integration folder where the user can see the reports.


1 Answers

You try to use the html reporter, which throws when you try to use it in Node:

$ mocha --reporter html > report.html

/usr/local/lib/node_modules/mocha/lib/reporters/html.js:194
    , div = document.createElement('div')
            ^
ReferenceError: document is not defined

Per the Mocha documentation (and relevant issue in Github), the htmlreporter only works in the browser, ie. to test client-side code in the browser.

If you want to output HTML for a Node.js test script, use the doc reporter, which will generate HTML.

like image 116
Paul Mougel Avatar answered Sep 19 '22 08:09

Paul Mougel