all files / lib/internal/process/ write-coverage.js

72.41% Statements 21/29
66.67% Branches 4/6
66.67% Functions 2/3
72.41% Lines 21/29
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47  1553× 1553× 1553× 1553× 1553×   1553× 1553× 1553×     1553×   1553× 1553× 1553× 1553×   1552×           1553× 1553×               1553× 1553×   1553×         1553×     1553×  
'use strict';
const process = require('process');
const path = require('path');
const fs = require('fs');
const mkdirSync = fs.mkdirSync;
const writeFileSync = fs.writeFileSync;
 
var isWritingCoverage = false;
function writeCoverage() {
  Iif (isWritingCoverage || !global.__coverage__) {
    return;
  }
  isWritingCoverage = true;
 
  const dirname = path.join(path.dirname(process.execPath), '.coverage');
  const filename = `coverage-${process.pid}-${Date.now()}.json`;
  try {
    mkdirSync(dirname);
  } catch (err) {
    Iif (err.code !== 'EEXIST') {
      console.error(err);
      return;
    }
  }
 
  const target = path.join(dirname, filename);
  const coverageInfo = JSON.stringify(global.__coverage__);
  try {
    writeFileSync(target, coverageInfo);
  } catch (err) {
    console.error(err);
  }
}
 
function setup() {
  var reallyReallyExit = process.reallyExit;
 
  process.reallyExit = function(code) {
    writeCoverage();
    reallyReallyExit(code);
  };
 
  process.on('exit', writeCoverage);
}
 
exports.setup = setup;