Hapus cakupan kode dari aplikasi Node.JS yang sudah berjalan

Dan lagi, saya berbicara tentang pengujian dan cakupan.



Mungkin, Anda sudah tersedak kopi dari pertanyaan "Mengapa menghapus cakupan dari aplikasi yang berjalan" - tetapi kebutuhan seperti itu muncul secara berkala.


Contohnya:


  • Cari tahu cakupan tes integrasi tanpa instarmentalisasi kode, penghentian aplikasi, dan unggah laporan menggunakan beberapa alat pihak ketiga;
  • Cari tahu, tanpa memetik kode yang lama, yang digunakan modul aplikasi tertentu;
  • Tentukan kode "mati", yang sebenarnya tidak digunakan dalam aplikasi;
  • , .

? !



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. Saya sudah selesai sedikit - sekarang proses terpisah digunakan untuk mengumpulkan penutup kosong, yang diisolasi dari yang utama - sehingga kemungkinan efek samping dapat diabaikan.


Jika tiba-tiba seseorang memiliki pikiran dan kenalan yang memakan seekor anjing di tempat kerja dengan pelapis (dan tidak seperti saya), saya akan senang menerima tawaran dan menarik permintaan. Smiley.


All Articles