Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run mocha tests with reporter?

When I run npm test it outputs:

 mocha ./tests/ --recursive --reporter mocha-junit-reporter

And all tests run well. But when I try to invoke mocha ./tests/flickr/mytest --reporter junit-reporter I got:

 Unknown "reporter": junit-reporter

How to pass it correctly?

like image 441
Cherry Avatar asked Mar 29 '19 10:03

Cherry


People also ask

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

Mocha provides a variety of interfaces for defining test suites, hooks, and individual tests, including TSS, Exports, QUnit, and Require. The default interface is behavior-driven development (BDD), which aims to help developers build software that is predictable, resilient to changes, and not error-prone.

How do I run a programmatically mocha?

Here is an example of using Mocha programmatically: var Mocha = require('mocha'), fs = require('fs'), path = require('path'); // Instantiate a Mocha instance. var mocha = new Mocha(); var testDir = 'some/dir/test' // Add each . js file to the mocha instance fs.

Does mocha run tests in parallel?

Mocha does not run individual tests in parallel. That means if you hand Mocha a single, lonely test file, it will spawn a single worker process, and that worker process will run the file. If you only have one test file, you'll be penalized for using parallel mode. Don't do that.


2 Answers

From mocha-junit-reporter's readme:

# install the package
npm install mocha-junit-reporter --save-dev

# run the test with reporter
mocha test --reporter mocha-junit-reporter --reporter-options mochaFile=./path_to_your/file.xml
like image 86
sarkiroka Avatar answered Oct 09 '22 23:10

sarkiroka


I spotted two issues in your command:

mocha ./tests/flickr/mytest --reporter junit-reporter

First issue is mocha in above is a mocha command from global node module. However, when executing npm test, it actually targets local mocha command inside our node_modules folder.

Second issue is the name of reporter should be mocha-junit-reporter not junit-reporter

Solution

Workaround is to target local mocha

./node_modules/.bin/mocha ./tests/flickr/mytest --reporter mocha-junit-reporter

This is preferrable solution.

Alternative solution is to install mocha-junit-reporter for global node modules as below:

npm install -g mocha-junit-reporter
mocha ./tests/flickr/mytest --reporter mocha-junit-reporter

This is not too preferrable because you can target different version of mocha and mocha-junit-reporter in global compare to the one in local node modules.

Cheers

like image 36
deerawan Avatar answered Oct 09 '22 23:10

deerawan