Skip to content

Commit

Permalink
feat(reporter): log coverage threshold as a warning fixed #432
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-redFox committed Aug 6, 2021
1 parent 433ca08 commit a6c95d8
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
3 changes: 3 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,12 @@ For example, `statements: 90` implies minimum statement coverage is 90%. `statem

`global` applies to all files together and `each` on a per-file basis. A list of files or patterns can be excluded from enforcement via the `excludes` property. On a per-file or pattern basis, per-file thresholds can be overridden via the `overrides` property.

`emitWarning` allows to log exceeding coverage threshold into console as warning and not fail tests.

```javascript
coverageReporter: {
check: {
emitWarning: false,
global: {
statements: 50,
branches: 50,
Expand Down
11 changes: 7 additions & 4 deletions lib/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ var CoverageReporter = function (rootConfig, helper, logger, emitter) {
})

var coverageFailed = false
const { emitWarning = false } = thresholds

function check (name, thresholds, actuals) {
var keys = [
Expand All @@ -146,11 +147,13 @@ var CoverageReporter = function (rootConfig, helper, logger, emitter) {
log.error(browser.name + ': Uncovered count for ' + key + ' (' + actualUncovered +
') exceeds ' + name + ' threshold (' + -1 * threshold + ')')
}
} else {
if (actual < threshold) {
} else if (actual < threshold) {
const message = `${browser.name}: Coverage for ${key} (${actual}%) does not meet ${name} threshold (${threshold}%)`
if (emitWarning) {
log.warn(message)
} else {
coverageFailed = true
log.error(browser.name + ': Coverage for ' + key + ' (' + actual +
'%) does not meet ' + name + ' threshold (' + threshold + '%)')
log.error(message)
}
}
})
Expand Down
48 changes: 48 additions & 0 deletions test/reporter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,5 +538,53 @@ describe('reporter', () => {

expect(results.exitCode).to.equal(0)
})

it('Should log warnings on low coverage and not fail the build', async () => {
const customConfig = helper.merge({}, rootConfig, {
coverageReporter: {
check: {
emitWarning: true,
each: {
statements: 50
}
}
}
})

mockCoverageMap.files.returns(['./foo/bar.js', './foo/baz.js'])
mockCoverageSummary.toJSON.returns({
lines: { total: 5, covered: 1, skipped: 0, pct: 20 },
statements: { total: 5, covered: 1, skipped: 0, pct: 20 },
functions: { total: 5, covered: 1, skipped: 0, pct: 20 },
branches: { total: 5, covered: 1, skipped: 0, pct: 20 }
})

const log = {
debug () {},
info () {},
warn: sinon.stub(),
error: sinon.stub()
}

const customLogger = {
create: (name) => {
return log
}
}

const results = { exitCode: 0 }

reporter = new m.CoverageReporter(customConfig, mockHelper, customLogger)
reporter.onRunStart()
browsers.forEach(b => reporter.onBrowserStart(b))
reporter.onRunComplete(browsers, results)

const done = sinon.stub()
await reporter.onExit(done)

expect(log.error).to.not.have.been.called
expect(log.warn).to.have.been.called
expect(done.calledOnce).to.be.true
})
})
})

0 comments on commit a6c95d8

Please sign in to comment.