istanbuljs / nyc

the Istanbul command line interface
https://istanbul.js.org/
ISC License
5.54k stars 353 forks source link

NYC checkCoverage doesn't have a way to throw error when insufficient coverage met #1539

Open sh977218 opened 6 months ago

sh977218 commented 6 months ago

Expected Behavior

In typescript, I import NYC from 'nyc', and generate report, check code coverage programmatically. new NYC().checkCoverage(coverageThresholds) should have option to throw error at end, instead of just purely console.error this is my example code:

import NYC from 'nyc';   //"nyc": "^15.1.0",

  try {
    const nycOutput = join(__dirname, '.', '.nyc_output');
    const nycInstance = new NYC({
      cwd: join(__dirname, '.'),
      reportDir: `coverage-e2e`,
      reporter: ['lcov', 'json', 'text-summary'],
    });
    await nycInstance.checkCoverage(coverageThresholds);
    await nycInstance.report();
    await fs.rm(nycOutput, { recursive: true, force: true });
  } catch (e) {
    // NYC doesn't throw error when coverage is not met. bug
    console.error('Insufficient playwright code coverage!');
  }

Observed Behavior

checkCoverage() only set process.exitCode = 1 when insufficient coverage met, but in my use case, process is not passed into NYC class.

Troubleshooting steps

Environment Information

# paste the output here