From 3d5372e1c544ddf79fa6f7f7da60d9cdfb92d05f Mon Sep 17 00:00:00 2001 From: Anton Shchekota Date: Wed, 22 Jul 2020 19:39:51 +0300 Subject: [PATCH] fix(report): waiting promise resolve in onExist method fix #418 onComplete are async method which should bw complete in onExit method --- lib/reporter.js | 9 +++++++-- test/reporter.spec.js | 18 ++++++++++++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/reporter.js b/lib/reporter.js index 135d8ce..71e9010 100644 --- a/lib/reporter.js +++ b/lib/reporter.js @@ -218,11 +218,13 @@ var CoverageReporter = function (rootConfig, helper, logger, emitter) { coverageMap.merge(result.coverage) } + const promiseCollection = [] + this.onRunComplete = async function (browsers, results) { const checkedCoverage = {} for (const reporterConfig of reporters) { - await Promise.all(browsers.map(async (browser) => { + const promise = Promise.all(browsers.map(async (browser) => { const coverageMap = coverageMaps[browser.id] if (!coverageMap) { return @@ -266,10 +268,13 @@ var CoverageReporter = function (rootConfig, helper, logger, emitter) { report.execute(context) }) })) + promiseCollection.push(promise) + await promise } } - this.onExit = function (done) { + this.onExit = async function (done) { + await Promise.all(promiseCollection) if (typeof config._onExit === 'function') { config._onExit(done) } else { diff --git a/test/reporter.spec.js b/test/reporter.spec.js index a3563f1..542f1a1 100644 --- a/test/reporter.spec.js +++ b/test/reporter.spec.js @@ -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 }) @@ -298,6 +298,20 @@ describe('reporter', () => { expect(mkdirIfNotExistsStub).not.to.have.been.called }) + it('should not create directory if reporting text* to console', 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)