Node Save Mocha Test Log to Disk

You must save the test log to disk

I tried the code below

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

Suppose that some test case could not enter the report file. Please suggest the best way to record test reports.

+6
source share
2 answers

You should redirect stderr to this log file:

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

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

 mocha -R spec test/**/*_test.js 2>&1 | tee report 
+7
source

The question does not indicate what the journal will be used for. If you need stack traces in a report, you cannot use json-stream , because this reporter does not include traces. However, if you want, this is something that can be trivially analyzed, and only you care about knowing the status of the tests, and not about why they failed, then the json-stream reporter works great:

 $ 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 easily grep for failed tests:

 $ grep '^\["fail"' report 

The output can also be transferred to the tool, which narrows it to the desired one and formats it to go in an email, chat message or something else. I used this method quite often when running a test suite with thousands of tests in which what I cared about most quickly got a list of unsuccessful ones.

+4
source

All Articles