Skip to content

Commit

Permalink
refactor: onRunComplete should be async method and fix unit tests fix #…
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-redFox committed Mar 18, 2020
1 parent a6d5a94 commit f799fe4
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 116 deletions.
63 changes: 30 additions & 33 deletions lib/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,17 +165,20 @@ var CoverageReporter = function (rootConfig, helper, logger, emitter) {
return coverageFailed
}

// Generate the output directory from the `coverageReporter.dir` and
// Generate the output path from the `coverageReporter.dir` and
// `coverageReporter.subdir` options.
function generateOutputDir (browserName, dir, subdir) {
dir = dir || 'coverage'
subdir = subdir || browserName

if (typeof subdir === 'function') {
function generateOutputPath (basePath, browserName, dir = 'coverage', subdir) {
if (subdir && typeof subdir === 'function') {
subdir = subdir(browserName)
}
if (browserName) {
browserName = browserName.replace(':', '')
}

return path.join(dir, subdir)
let outPutPath = path.join(dir, subdir || browserName)
outPutPath = path.resolve(basePath, outPutPath)

return helper.normalizeWinPath(outPutPath)
}

this.onRunStart = function (browsers) {
Expand Down Expand Up @@ -214,28 +217,24 @@ var CoverageReporter = function (rootConfig, helper, logger, emitter) {
coverageMap.merge(result.coverage)
}

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

reporters.forEach(function (reporterConfig) {
browsers.forEach(async function (browser) {
var coverageMap = coverageMaps[browser.id]

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

var mainDir = reporterConfig.dir || config.dir
var subDir = reporterConfig.subdir || config.subdir
var browserName = browser.name.replace(':', '')
var simpleOutputDir = generateOutputDir(browserName, mainDir, subDir)
var resolvedOutputDir = path.resolve(basePath, simpleOutputDir)
const mainDir = reporterConfig.dir || config.dir
const subDir = reporterConfig.subdir || config.subdir
const simpleOutputPath = generateOutputPath(basePath, browser.name, mainDir, subDir)

var outputDir = helper.normalizeWinPath(resolvedOutputDir)
var remappedCoverageMap = await sourceMapStore.transformCoverage(coverageMap)
const remappedCoverageMap = await sourceMapStore.transformCoverage(coverageMap)

var options = helper.merge(config, reporterConfig, {
dir: outputDir,
const options = helper.merge(config, reporterConfig, {
dir: simpleOutputPath,
subdir: '',
browser: browser,
emitter: emitter,
Expand All @@ -245,31 +244,29 @@ var CoverageReporter = function (rootConfig, helper, logger, emitter) {
// If config.check is defined, check coverage levels for each browser
if (config.hasOwnProperty('check') && !checkedCoverage[browser.id]) {
checkedCoverage[browser.id] = true
var coverageFailed = checkCoverage(browser, remappedCoverageMap)
if (coverageFailed) {
if (results) {
results.exitCode = 1
}
const coverageFailed = checkCoverage(browser, remappedCoverageMap)
if (coverageFailed && results) {
results.exitCode = 1
}
}

var context = istanbulLibReport.createContext(options)
var report = reports.create(reporterConfig.type || 'html', options)
const context = istanbulLibReport.createContext(options)
const report = reports.create(reporterConfig.type || 'html', options)

// // If reporting to console or in-memory skip directory creation
var toDisk = !reporterConfig.type || !reporterConfig.type.match(/^(text|text-summary|in-memory)$/)
const toDisk = !reporterConfig.type || !reporterConfig.type.match(/^(text|text-summary|in-memory)$/)

if (!toDisk && reporterConfig.file === undefined) {
report.execute(context)
return
}

helper.mkdirIfNotExists(outputDir, function () {
log.debug('Writing to %s', outputDir)
helper.mkdirIfNotExists(simpleOutputPath, function () {
log.debug('Writing to %s', simpleOutputPath)
report.execute(context)
})
})
})
}))
}
}

this.onExit = function (done) {
Expand Down
172 changes: 89 additions & 83 deletions test/reporter.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe 'reporter', ->
mockSourceMapStore = {
transformCoverage: sinon.stub()
}
mockSourceMapStore.transformCoverage.returns { map: mockCoverageMap }
mockSourceMapStore.transformCoverage.resolves mockCoverageMap
sourceMapStoreGetStub = sinon.stub(globalSourceMapStore, 'get')
sourceMapStoreGetStub.returns mockSourceMapStore

Expand Down Expand Up @@ -145,18 +145,19 @@ describe 'reporter', ->
expect(mockCoverageMap.merge).not.to.have.been.called

it 'should make reports', ->
reporter.onRunComplete browsers
expect(mkdirIfNotExistsStub).to.have.been.calledTwice
dir = rootConfig.coverageReporter.dir
expect(mkdirIfNotExistsStub.getCall(0).args[0]).to.deep.equal resolve('/base', dir, fakeChrome.name)
expect(mkdirIfNotExistsStub.getCall(1).args[0]).to.deep.equal resolve('/base', dir, fakeOpera.name)
mkdirIfNotExistsStub.getCall(0).args[1]()
expect(reportCreateStub).to.have.been.called
expect(mockPackageSummary.execute).to.have.been.called
createArgs = reportCreateStub.getCall(0).args
expect(createArgs[0]).to.be.equal 'html'
expect(createArgs[1].browser).to.be.equal fakeChrome
expect(createArgs[1].emitter).to.be.equal emitter
return reporter.onRunComplete(browsers).then( ->
expect(mkdirIfNotExistsStub).to.have.been.calledTwice
dir = rootConfig.coverageReporter.dir
expect(mkdirIfNotExistsStub.getCall(0).args[0]).to.deep.equal resolve('/base', dir, fakeChrome.name)
expect(mkdirIfNotExistsStub.getCall(1).args[0]).to.deep.equal resolve('/base', dir, fakeOpera.name)
mkdirIfNotExistsStub.getCall(0).args[1]()
expect(reportCreateStub).to.have.been.called
expect(mockPackageSummary.execute).to.have.been.called
createArgs = reportCreateStub.getCall(0).args
expect(createArgs[0]).to.be.equal 'html'
expect(createArgs[1].browser).to.be.equal fakeChrome
expect(createArgs[1].emitter).to.be.equal emitter
)

it 'should support a string for the subdir option', ->
customConfig = helper.merge {}, rootConfig,
Expand All @@ -167,15 +168,16 @@ describe 'reporter', ->
reporter.onRunStart()
browsers.forEach (b) -> reporter.onBrowserStart b

reporter.onRunComplete browsers
expect(mkdirIfNotExistsStub).to.have.been.calledTwice
dir = customConfig.coverageReporter.dir
subdir = customConfig.coverageReporter.subdir
expect(mkdirIfNotExistsStub.getCall(0).args[0]).to.deep.equal resolve('/base', dir, subdir)
expect(mkdirIfNotExistsStub.getCall(1).args[0]).to.deep.equal resolve('/base', dir, subdir)
mkdirIfNotExistsStub.getCall(0).args[1]()
expect(reportCreateStub).to.have.been.called
expect(mockPackageSummary.execute).to.have.been.called
return reporter.onRunComplete(browsers).then( ->
expect(mkdirIfNotExistsStub).to.have.been.calledTwice
dir = customConfig.coverageReporter.dir
subdir = customConfig.coverageReporter.subdir
expect(mkdirIfNotExistsStub.getCall(0).args[0]).to.deep.equal resolve('/base', dir, subdir)
expect(mkdirIfNotExistsStub.getCall(1).args[0]).to.deep.equal resolve('/base', dir, subdir)
mkdirIfNotExistsStub.getCall(0).args[1]()
expect(reportCreateStub).to.have.been.called
expect(mockPackageSummary.execute).to.have.been.called
)

it 'should support a function for the subdir option', ->
customConfig = helper.merge {}, rootConfig,
Expand All @@ -186,14 +188,15 @@ describe 'reporter', ->
reporter.onRunStart()
browsers.forEach (b) -> reporter.onBrowserStart b

reporter.onRunComplete browsers
expect(mkdirIfNotExistsStub).to.have.been.calledTwice
dir = customConfig.coverageReporter.dir
expect(mkdirIfNotExistsStub.getCall(0).args[0]).to.deep.equal resolve('/base', dir, 'chrome')
expect(mkdirIfNotExistsStub.getCall(1).args[0]).to.deep.equal resolve('/base', dir, 'opera')
mkdirIfNotExistsStub.getCall(0).args[1]()
expect(reportCreateStub).to.have.been.called
expect(mockPackageSummary.execute).to.have.been.called
return reporter.onRunComplete(browsers).then( ->
expect(mkdirIfNotExistsStub).to.have.been.calledTwice
dir = customConfig.coverageReporter.dir
expect(mkdirIfNotExistsStub.getCall(0).args[0]).to.deep.equal resolve('/base', dir, 'chrome')
expect(mkdirIfNotExistsStub.getCall(1).args[0]).to.deep.equal resolve('/base', dir, 'opera')
mkdirIfNotExistsStub.getCall(0).args[1]()
expect(reportCreateStub).to.have.been.called
expect(mockPackageSummary.execute).to.have.been.called
)

it 'should support a specific dir and subdir per reporter', ->
customConfig = helper.merge {}, rootConfig,
Expand All @@ -215,15 +218,16 @@ describe 'reporter', ->
reporter.onRunStart()
browsers.forEach (b) -> reporter.onBrowserStart b

reporter.onRunComplete browsers
expect(mkdirIfNotExistsStub.callCount).to.equal 4
expect(mkdirIfNotExistsStub.getCall(0).args[0]).to.deep.equal resolve('/base', 'reporter1', 'chrome')
expect(mkdirIfNotExistsStub.getCall(1).args[0]).to.deep.equal resolve('/base', 'reporter1', 'opera')
expect(mkdirIfNotExistsStub.getCall(2).args[0]).to.deep.equal resolve('/base', 'reporter2', 'CHROME')
expect(mkdirIfNotExistsStub.getCall(3).args[0]).to.deep.equal resolve('/base', 'reporter2', 'OPERA')
mkdirIfNotExistsStub.getCall(0).args[1]()
expect(reportCreateStub).to.have.been.called
expect(mockPackageSummary.execute).to.have.been.called
return reporter.onRunComplete(browsers).then( ->
expect(mkdirIfNotExistsStub.callCount).to.equal 4
expect(mkdirIfNotExistsStub.getCall(0).args[0]).to.deep.equal resolve('/base', 'reporter1', 'chrome')
expect(mkdirIfNotExistsStub.getCall(1).args[0]).to.deep.equal resolve('/base', 'reporter1', 'opera')
expect(mkdirIfNotExistsStub.getCall(2).args[0]).to.deep.equal resolve('/base', 'reporter2', 'CHROME')
expect(mkdirIfNotExistsStub.getCall(3).args[0]).to.deep.equal resolve('/base', 'reporter2', 'OPERA')
mkdirIfNotExistsStub.getCall(0).args[1]()
expect(reportCreateStub).to.have.been.called
expect(mockPackageSummary.execute).to.have.been.called
)

it 'should fallback to the default dir/subdir if not provided', ->
customConfig = helper.merge {}, rootConfig,
Expand All @@ -243,45 +247,49 @@ describe 'reporter', ->
reporter.onRunStart()
browsers.forEach (b) -> reporter.onBrowserStart b

reporter.onRunComplete browsers
expect(mkdirIfNotExistsStub.callCount).to.equal 4
expect(mkdirIfNotExistsStub.getCall(0).args[0]).to.deep.equal resolve('/base', 'reporter1', 'defaultsubdir')
expect(mkdirIfNotExistsStub.getCall(1).args[0]).to.deep.equal resolve('/base', 'reporter1', 'defaultsubdir')
expect(mkdirIfNotExistsStub.getCall(2).args[0]).to.deep.equal resolve('/base', 'defaultdir', 'CHROME')
expect(mkdirIfNotExistsStub.getCall(3).args[0]).to.deep.equal resolve('/base', 'defaultdir', 'OPERA')
mkdirIfNotExistsStub.getCall(0).args[1]()
expect(reportCreateStub).to.have.been.called
expect(mockPackageSummary.execute).to.have.been.called
return reporter.onRunComplete(browsers).then( ->
expect(mkdirIfNotExistsStub.callCount).to.equal 4
expect(mkdirIfNotExistsStub.getCall(0).args[0]).to.deep.equal resolve('/base', 'reporter1', 'defaultsubdir')
expect(mkdirIfNotExistsStub.getCall(1).args[0]).to.deep.equal resolve('/base', 'reporter1', 'defaultsubdir')
expect(mkdirIfNotExistsStub.getCall(2).args[0]).to.deep.equal resolve('/base', 'defaultdir', 'CHROME')
expect(mkdirIfNotExistsStub.getCall(3).args[0]).to.deep.equal resolve('/base', 'defaultdir', 'OPERA')
mkdirIfNotExistsStub.getCall(0).args[1]()
expect(reportCreateStub).to.have.been.called
expect(mockPackageSummary.execute).to.have.been.called
)

it 'should not create directory if reporting text* to console', ->
run = ->
reporter = new m.CoverageReporter rootConfig, mockHelper, mockLogger
reporter.onRunStart()
browsers.forEach (b) -> reporter.onBrowserStart b
reporter.onRunComplete browsers
return reporter.onRunComplete browsers

rootConfig.coverageReporter.reporters = [
{ type: 'text' }
{ type: 'text-summary' }
]
run()
expect(mkdirIfNotExistsStub).not.to.have.been.called
return run().then( ->
expect(mkdirIfNotExistsStub).not.to.have.been.called
)

it 'should create directory if reporting text* to file', ->
run = ->
reporter = new m.CoverageReporter rootConfig, mockHelper, mockLogger
reporter.onRunStart()
browsers.forEach (b) -> reporter.onBrowserStart b
reporter.onRunComplete browsers
return reporter.onRunComplete browsers

rootConfig.coverageReporter.reporters = [{ type: 'text', file: 'file' }]
run()
expect(mkdirIfNotExistsStub).to.have.been.calledTwice
return run().then( ->
expect(mkdirIfNotExistsStub).to.have.been.calledTwice

mkdirIfNotExistsStub.resetHistory()
rootConfig.coverageReporter.reporters = [{ type: 'text-summary', file: 'file' }]
run()
expect(mkdirIfNotExistsStub).to.have.been.calledTwice
mkdirIfNotExistsStub.resetHistory()
rootConfig.coverageReporter.reporters = [{ type: 'text-summary', file: 'file' }]
return run()
).then( ->
expect(mkdirIfNotExistsStub).to.have.been.calledTwice
)

it 'should support including all sources', ->
customConfig = helper.merge {}, rootConfig,
Expand Down Expand Up @@ -348,12 +356,12 @@ describe 'reporter', ->
reporter = new m.CoverageReporter customConfig, mockHelper, mockLogger
reporter.onRunStart()
browsers.forEach (b) -> reporter.onBrowserStart b
reporter.onRunComplete browsers

expect(createContextStub).to.have.been.called
expect(reportCreateStub).to.have.been.called
options = reportCreateStub.getCall(0)
expect(options.args[1].watermarks).to.deep.equal(watermarks)
return reporter.onRunComplete(browsers).then( ->
expect(createContextStub).to.have.been.called
expect(reportCreateStub).to.have.been.called
options = reportCreateStub.getCall(0)
expect(options.args[1].watermarks).to.deep.equal(watermarks)
)

it 'should merge with istanbul default watermarks', ->
watermarks =
Expand All @@ -375,15 +383,15 @@ describe 'reporter', ->
reporter = new m.CoverageReporter customConfig, mockHelper, mockLogger
reporter.onRunStart()
browsers.forEach (b) -> reporter.onBrowserStart b
reporter.onRunComplete browsers

expect(createContextStub).to.have.been.called
expect(reportCreateStub).to.have.been.called
options = reportCreateStub.getCall(0)
expect(options.args[1].watermarks.statements).to.deep.equal(watermarks.statements)
expect(options.args[1].watermarks.branches).to.deep.equal(mockDefaultWatermarks.branches)
expect(options.args[1].watermarks.functions).to.deep.equal(mockDefaultWatermarks.functions)
expect(options.args[1].watermarks.lines).to.deep.equal(watermarks.lines)
return reporter.onRunComplete(browsers).then( ->
expect(createContextStub).to.have.been.called
expect(reportCreateStub).to.have.been.called
options = reportCreateStub.getCall(0)
expect(options.args[1].watermarks.statements).to.deep.equal(watermarks.statements)
expect(options.args[1].watermarks.branches).to.deep.equal(mockDefaultWatermarks.branches)
expect(options.args[1].watermarks.functions).to.deep.equal(mockDefaultWatermarks.functions)
expect(options.args[1].watermarks.lines).to.deep.equal(watermarks.lines)
)

it 'should log errors on low coverage and fail the build', ->
customConfig = helper.merge {}, rootConfig,
Expand Down Expand Up @@ -412,11 +420,10 @@ describe 'reporter', ->
reporter = new m.CoverageReporter customConfig, mockHelper, customLogger
reporter.onRunStart()
browsers.forEach (b) -> reporter.onBrowserStart b
reporter.onRunComplete browsers, results

expect(spy1).to.have.been.called

expect(results.exitCode).to.not.equal 0
return reporter.onRunComplete(browsers, results).then( ->
expect(spy1).to.have.been.called
expect(results.exitCode).to.not.equal 0
)

it 'should not log errors on sufficient coverage and not fail the build', ->
customConfig = helper.merge {}, rootConfig,
Expand Down Expand Up @@ -445,8 +452,7 @@ describe 'reporter', ->
reporter = new m.CoverageReporter customConfig, mockHelper, customLogger
reporter.onRunStart()
browsers.forEach (b) -> reporter.onBrowserStart b
reporter.onRunComplete browsers, results

expect(spy1).to.not.have.been.called

expect(results.exitCode).to.equal 0
return reporter.onRunComplete(browsers, results).then( ->
expect(spy1).to.not.have.been.called
expect(results.exitCode).to.equal 0
)

0 comments on commit f799fe4

Please sign in to comment.