Node Mocha test log save to disk
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
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.