Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(report): waiting promise resolve in onExist method fix #418 #419

Merged
merged 1 commit into from
Jul 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 63 additions & 44 deletions lib/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,58 +218,77 @@ var CoverageReporter = function (rootConfig, helper, logger, emitter) {
coverageMap.merge(result.coverage)
}

this.onRunComplete = async function (browsers, results) {
const checkedCoverage = {}

for (const reporterConfig of reporters) {
await Promise.all(browsers.map(async (browser) => {
const coverageMap = coverageMaps[browser.id]
if (!coverageMap) {
return
}
let checkedCoverage = {}
let promiseComplete = null

this.executeReport = async function (reporterConfig, browser) {
const results = { exitCode: 0 }
const coverageMap = coverageMaps[browser.id]
if (!coverageMap) {
return
}

const mainDir = reporterConfig.dir || config.dir
const subDir = reporterConfig.subdir || config.subdir
const outputPath = generateOutputPath(basePath, browser.name, mainDir, subDir)
const remappedCoverageMap = await sourceMapStore.transformCoverage(coverageMap)

const options = helper.merge(config, reporterConfig, {
dir: outputPath,
subdir: '',
browser: browser,
emitter: emitter,
coverageMap: remappedCoverageMap
})

// If config.check is defined, check coverage levels for each browser
if (hasOwnProperty.call(config, 'check') && !checkedCoverage[browser.id]) {
checkedCoverage[browser.id] = true
var coverageFailed = checkCoverage(browser, remappedCoverageMap)
if (coverageFailed && results) {
results.exitCode = 1
}
}
const mainDir = reporterConfig.dir || config.dir
const subDir = reporterConfig.subdir || config.subdir
const outputPath = generateOutputPath(basePath, browser.name, mainDir, subDir)
const remappedCoverageMap = await sourceMapStore.transformCoverage(coverageMap)

const options = helper.merge(config, reporterConfig, {
dir: outputPath,
subdir: '',
browser: browser,
emitter: emitter,
coverageMap: remappedCoverageMap
})

const context = istanbulLibReport.createContext(options)
const report = reports.create(reporterConfig.type || 'html', options)
// If config.check is defined, check coverage levels for each browser
if (hasOwnProperty.call(config, 'check') && !checkedCoverage[browser.id]) {
checkedCoverage[browser.id] = true
var coverageFailed = checkCoverage(browser, remappedCoverageMap)
if (coverageFailed && results) {
results.exitCode = 1
}
}

// // If reporting to console or in-memory skip directory creation
const toDisk = !reporterConfig.type || !reporterConfig.type.match(/^(text|text-summary|in-memory)$/)
const context = istanbulLibReport.createContext(options)
const report = reports.create(reporterConfig.type || 'html', options)

if (!toDisk && reporterConfig.file === undefined) {
report.execute(context)
return
}
// // If reporting to console or in-memory skip directory creation
const toDisk = !reporterConfig.type || !reporterConfig.type.match(/^(text|text-summary|in-memory)$/)

helper.mkdirIfNotExists(outputPath, function () {
log.debug('Writing coverage to %s', outputPath)
report.execute(context)
})
}))
if (!toDisk && reporterConfig.file === undefined) {
report.execute(context)
return
}

helper.mkdirIfNotExists(outputPath, function () {
log.debug('Writing coverage to %s', outputPath)
report.execute(context)
})
return results
}

this.onExit = function (done) {
this.onRunComplete = function (browsers) {
checkedCoverage = {}
let results = { exitCode: 0 }

const promiseCollection = reporters.map(reporterConfig =>
Promise.all(browsers.map(async (browser) => {
const res = await this.executeReport(reporterConfig, browser)
if (res && res.exitCode === 1) {
results = res
}
})))
promiseComplete = Promise.all(promiseCollection).then(() => results)
return promiseComplete
anthony-redFox marked this conversation as resolved.
Show resolved Hide resolved
}

this.onExit = async function (done) {
const results = await promiseComplete
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no error handling by karma in case promiseComplete rejects. Karma doesn't expect the onExit handlers to return a promise. I would suggest using plain old then to handle success/error. See my code in #420

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

await works with success only, if will be error the code onExit will be failed

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onExit is called sync by the karma-runner and expects the callback to be invoked. It doesn't expect a promise to be returned. That's why I suspect that a rejection would result into an unhandled Promise rejection.

if (results && results.exitCode === 1) {
done(results.exitCode)
return
}
if (typeof config._onExit === 'function') {
config._onExit(done)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure where this function gets defined or where it's used, but it won't be called in case exitCode is 1.
See #420 for my approach, which still calls it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure that will be useful to call callback onExit? custom code can call with 1 or 0 too but you are ignore it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Build will be failed. Is matter to execute onExit method?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know where _onExit is defined or used, but with this change it's not called anymore in case the exitCode is 1. That's what I wanted to note here.

} else {
Expand Down
27 changes: 22 additions & 5 deletions test/reporter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ describe('reporter', () => {
mkdirIfNotExistsStub.resetHistory()
})

it('has no pending file writings', () => {
it('has no pending file writings', async () => {
const done = sinon.spy()
reporter.onExit(done)
await reporter.onExit(done)
expect(done).to.have.been.called
})

Expand Down Expand Up @@ -298,6 +298,20 @@ describe('reporter', () => {
expect(mkdirIfNotExistsStub).not.to.have.been.called
})

it('should calls done callback when onComplete event will be complete', async () => {
reporter = new m.CoverageReporter(rootConfig, mockHelper, mockLogger)
reporter.onRunStart()
browsers.forEach(b => reporter.onBrowserStart(b))
reporter.onRunComplete(browsers)
const done = sinon.stub()

const promiseExit = reporter.onExit(done)

expect(done.notCalled).to.be.true
await promiseExit
expect(done.calledOnce).to.be.true
})

it('should create directory if reporting text* to file', async () => {
const run = () => {
reporter = new m.CoverageReporter(rootConfig, mockHelper, mockLogger)
Expand Down Expand Up @@ -435,7 +449,7 @@ describe('reporter', () => {
expect(options.args[1].watermarks.lines).to.deep.equal(watermarks.lines)
})

it('should log errors on low coverage and fail the build', async () => {
it.only('should log errors on low coverage and fail the build', async () => {
const customConfig = helper.merge({}, rootConfig, {
coverageReporter: {
check: {
Expand Down Expand Up @@ -472,10 +486,13 @@ describe('reporter', () => {
reporter = new m.CoverageReporter(customConfig, mockHelper, customLogger)
reporter.onRunStart()
browsers.forEach(b => reporter.onBrowserStart(b))
await reporter.onRunComplete(browsers, results)
reporter.onRunComplete(browsers, results)

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

expect(spy1).to.have.been.called
expect(results.exitCode).to.not.equal(0)
expect(done.calledOnceWith(1)).to.be.true
})

it('should not log errors on sufficient coverage and not fail the build', async () => {
Expand Down