Remove code coverage from an already running Node.JS application

And again, I'm talking about testing and coverage.



Probably, you have already choked on coffee from the question "Why remove coverage from a running application" - but such a need periodically arises.


For instance:


  • Find out the coverage of integration tests without code instarmentalization, application termination and report uploading using some third-party tools;
  • Find out, without a long picking of the code, by which particular application modules the request passed;
  • Define a "dead" code, which in fact is not used in the application;
  • , .

? !



jest mocha (, ), , V8 - . β€” , , . , , !



, .


, , express.


1)


const runtimeCoverage = require('runtime-coverage');

2) API endpoint, :


app.get('/startCoverage', async (req, res) => {
  await runtimeCoverage.startCoverage();
  res.send('coverage started');
});

3) endpoint,


app.get('/getCoverage', async (req, res) => {

  const options = {
    all: req.query.all,
    return: true,
    reporters: [req.query.reporter || 'text'],
  };
  const coverage = await runtimeCoverage.getCoverage(options);
  const data = Object.values(coverage)[0];
  res.end(data);
});

… !


endpoint, API, β€” ! , , , .



, - http://localhost:3000/startCoverage http://localhost:3000/getCoverage


----------|---------|----------|---------|---------|---------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s   
----------|---------|----------|---------|---------|---------------------
All files |   60.34 |       50 |     100 |   60.34 |                     
 index.js |   60.34 |       50 |     100 |   60.34 | 9,27,28,32-41,46-55 
----------|---------|----------|---------|---------|---------------------

node_modules, , node modules :


.
-------------------------------------------------------------|---------|----------|---------|---------|-------------------------
File                                                         | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s       
-------------------------------------------------------------|---------|----------|---------|---------|-------------------------
All files                                                    |   62.89 |       30 |   26.14 |   62.89 |                         
 runtime-coverage-sample                                     |   62.07 |       50 |     100 |   62.07 |                         
  index.js                                                   |   62.07 |       50 |     100 |   62.07 | 9,20-28,32-41,54,55     
 runtime-coverage-sample/node_modules/content-type           |   59.46 |      100 |   28.57 |   59.46 |                         
  index.js                                                   |   59.46 |      100 |   28.57 |   59.46 | ...-122,126-163,174-190 
 runtime-coverage-sample/node_modules/debug/src              |   42.57 |        0 |   14.29 |   42.57 |                         
  debug.js                                                   |   42.57 |        0 |   14.29 |   42.57 | ...-166,176-189,199-202 
 runtime-coverage-sample/node_modules/etag                   |   95.42 |      100 |      75 |   95.42 |                         
  index.js                                                   |   95.42 |      100 |      75 |   95.42 | 126-131                 
 runtime-coverage-sample/node_modules/express/lib            |   64.54 |    83.33 |   15.58 |   64.54 |                         
  application.js                                             |   62.11 |       50 |      20 |   62.11 | ...,618,628-631,638-644 
  express.js                                                 |   81.03 |      100 |   33.33 |   81.03 | 37-57,112               
  request.js                                                 |      76 |      100 |       0 |      76 | ...,496,507,508,519-525 
  response.js                                                |   58.93 |      100 |   17.39 |   58.93 | ...,1016-1104,1118-1142 
  utils.js                                                   |   64.71 |      100 |      25 |   64.71 | ...-239,274-282,304-306 
 runtime-coverage-sample/node_modules/express/lib/middleware |    61.8 |    66.67 |      50 |    61.8 |                         
  init.js                                                    |   69.05 |       50 |      50 |   69.05 | 29-41                   
  query.js                                                   |   55.32 |      100 |      50 |   55.32 | 26-46                   
 runtime-coverage-sample/node_modules/express/lib/router     |   65.82 |    66.67 |   43.33 |   65.82 |                         
  index.js                                                   |   61.93 |      100 |   42.11 |   61.93 | ...-635,640-648,651-662 
  layer.js                                                   |   74.59 |      100 |      40 |   74.59 | 33-50,63-74,166-181     
  route.js                                                   |   70.37 |       50 |      50 |   70.37 | ...8-90,171-189,193-215 
 runtime-coverage-sample/node_modules/finalhandler           |   60.12 |      100 |    9.09 |   60.12 |                         
  index.js                                                   |   60.12 |      100 |    9.09 |   60.12 | ...-259,272-311,321-331 
 runtime-coverage-sample/node_modules/fresh                  |   94.16 |      100 |   66.67 |   94.16 |                         
  index.js                                                   |   94.16 |      100 |   66.67 |   94.16 | 94-101                  
 runtime-coverage-sample/node_modules/mime                   |   62.96 |       25 |   33.33 |   62.96 |                         
  mime.js                                                    |   62.96 |       25 |   33.33 |   62.96 | 4-10,22-37,49-63,79,80  
 runtime-coverage-sample/node_modules/parseurl               |   87.34 |    33.33 |      75 |   87.34 |                         
  index.js                                                   |   87.34 |    33.33 |      75 |   87.34 | 65-84                   
 runtime-coverage-sample/node_modules/qs/lib                 |   33.05 |    17.65 |   18.75 |   33.05 |                         
  parse.js                                                   |   41.32 |      100 |   33.33 |   41.32 | ...2-97,101-132,136-186 
  utils.js                                                   |   24.35 |    15.15 |      10 |   24.35 | ...,181-201,209-213,217 
-------------------------------------------------------------|---------|----------|---------|---------|-------------------------

, β€” , ( ), - ( , endpoint ).


,


. , .


, collect-v8-coverage ( ), v8-to-istanbul, istanbul-lib-coverage, istanbul-lib-report, istanbul-reports β€” . , V8 . β€” .


, β€” , , . , :


1) , ;
2) ;
3) require , , ;
4) ;
4.1) require, ;
4.2) , , , ;
4.3) require;
5) require;
6) ;
7) , .


, , , β€” - β€” - . :


  1. .
  2. setInterval
  3. unhandledRejction, .

, , . . forceReload forceLineMode β€” , .


UPD. I’ve finished it a bit - now a separate process is used to collect the empty cover, which is isolated from the main one - so the probability of side effects is negligible.


If suddenly someone has thoughts and acquaintances who ate a dog at work with a coating (and not like me), I will be glad to receive offers and pull requests. Smiley.


All Articles