Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node Mocha test log save to disk

Need to save the test logging to the disk

I have tried the below code

  mocha -R spec  test/**/*_test.js   >  report

suppose any test case failed it not log into the 'report' file. Please suggest best way to log the test report.

like image 582
Elankeeran Avatar asked Jan 08 '14 15:01

Elankeeran


2 Answers

You should redirect stderr to that logfile too:

mocha -R spec  test/**/*_test.js > report 2>&1

EDIT: if you want the contents to be sent both to a file and the console:

mocha -R spec  test/**/*_test.js 2>&1 | tee report
like image 124
robertklep Avatar answered Oct 14 '22 08:10

robertklep


The question does not specify what the log is going to be used for. If you need stack traces in the report, then you can't use json-stream because this reporter does not include the traces. However, if what you want is something that can be parsed trivially and only thing you care about is knowing the status of the tests and not why they failed, then the json-stream reporter works nicely:

$ mocha -R json-stream > report

You get a list of regularly formatted lines:

["start",{"total":1}]
["fail",{"title":"q","fullTitle":"blah q"}]
["end",{"suites":1,"tests":1,"passes":0,"pending":0,"failures":1,"start":"2014-01-08T18:10:27.764Z","end":"2014-01-08T18:10:27.768Z","duration":4}]

Then you can grep for failed tests easily:

$ grep '^\["fail"' report

The output could also be passed to a tool that narrows it down to what you want and formats it nicely to go in an email, an IM message or something else. I've used this method quite a lot when running a test suite with thousands of tests where what I cared about most was quickly getting a list of what had failed.

like image 20
Louis Avatar answered Oct 14 '22 08:10

Louis