Wednesday, 17 May 2017

Node application testing and LCOV report generation with Mocha

My group recently started working on a previously existing Node application written entirely in ES6 (its been through a couple of different developers prior to us working on it). The application is minimally unit tested using Mocha and Nyan as the reporter for Mocha. The package.json file already contained the following command to run tests:

"test": "node ./node_modules/mocha/bin/mocha  --compilers js:babel-core/register ./test/**/*.spec.js"

As part of our deployment cycle, we use a CI/CD pipeline with Travis and Codacy. As a result, in order to enable the "code coverage" portion of Codacy I needed to make some changes to the application repository and package.json file. The basic idea was to have Mocha generate an .lcov file which then is piped to Codacy in the Travis build script.

After doing some research i put together the following to generate the lcov file successfully:

    "build-test": "node ./node_modules/mocha/bin/mocha -R mocha-lcov-reporter > coverage/coverage.lcov  --compilers js:babel-core/register ./test/**/*.spec.js --reporter nyan"

The problem is, I'm getting a Codacy parsing error for the .lcov file. I looked at lcov files for other projects and there seems to be extra data in the one I'm generating. For reference here is the generated lcov file:

0
0
0

[4A[5C-
[5C-
[5C-
[5C-
[4A[6C_,------,
[6C_|   /\_/\ 
[6C^|__( - .-) 
[6C  ""  "" 
[4ACONSTRUCTOR
 1
 0
 0

[4A[5C-_
[5C-_
[5C-_
[5C-_
[4A[7C_,------,
[7C_|  /\_/\ 
[7C~|_( ^ .^) 
[7C ""  "" 
[4ACONSTRUCTOR
 2
 0
 0

[4A[5C-_-
[5C-_-
[5C-_-
[5C-_-
[4A[8C_,------,
[8C_|   /\_/\ 
[8C^|__( ^ .^) 
[8C  ""  "" 
[4ACONSTRUCTOR
 3
 0
 0

[4A[5C-_-_
[5C-_-_
[5C-_-_
[5C-_-_
[4A[9C_,------,
[9C_|  /\_/\ 
[9C~|_( ^ .^) 
[9C ""  "" 
[4ACONSTRUCTOR
{"name":"from http://localhost:9200 searcher","hostname":"NCI-01966295-ML","pid":14596,"level":30,"msg":"Active","time":"2017-05-16T23:02:37.746Z","v":0}
{"name":"from http://localhost:9200 searcher","hostname":"NCI-01966295-ML","pid":14596,"level":30,"msg":"Temporarily Closed to Accrual","time":"2017-05-16T23:02:37.747Z","v":0}
 4
 0
 0

[4A[5C-_-_-
[5C-_-_-
[5C-_-_-
[5C-_-_-
[4A[10C_,------,
[10C_|   /\_/\ 
[10C^|__( ^ .^) 
[10C  ""  "" 
[4ACONSTRUCTOR
 5
 0
 0

[4A[5C-_-_-_
[5C-_-_-_
[5C-_-_-_
[5C-_-_-_
[4A[11C_,------,
[11C_|  /\_/\ 
[11C~|_( ^ .^) 
[11C ""  "" 
[4ACONSTRUCTOR
 6
 0
 0

[4A[5C-_-_-_-
[5C-_-_-_-
[5C-_-_-_-
[5C-_-_-_-
[4A[12C_,------,
[12C_|   /\_/\ 
[12C^|__( ^ .^) 
[12C  ""  "" 
[4ACONSTRUCTOR
 7
 0
 0

[4A[5C-_-_-_-_
[5C-_-_-_-_
[5C-_-_-_-_
[5C-_-_-_-_
[4A[13C_,------,
[13C_|  /\_/\ 
[13C~|_( ^ .^) 
[13C ""  "" 
[4ACONSTRUCTOR
 8
 0
 0

[4A[5C-_-_-_-_-
[5C-_-_-_-_-
[5C-_-_-_-_-
[5C-_-_-_-_-
[4A[14C_,------,
[14C_|   /\_/\ 
[14C^|__( ^ .^) 
[14C  ""  "" 
[4ACONSTRUCTOR
 9
 0
 0

[4A[5C-_-_-_-_-_
[5C-_-_-_-_-_
[5C-_-_-_-_-_
[5C-_-_-_-_-_
[4A[15C_,------,
[15C_|  /\_/\ 
[15C~|_( ^ .^) 
[15C ""  "" 
[4A




  9 passing (20ms)

I observed a few things regarding the npm scripts (test and build-test):

  • test works correctly, informing me that 9 tests are passing
  • If I run build-test without "--reporter nyan" it says "no coverage data found, make sure your code is properly instrumented"
  • Everything in the build-test line of code seems to be necessary.

I'm at my wits end to be honest. I have no idea why the .lcov file is having parsing issues. All I really need is to use mocha with mocha-lcov-reporter to generate a valid .lcov file. It doesn't seem like it should be all that difficult.

Thank you for any help provided. I appreciate it.



via user2360062

No comments:

Post a Comment