siesta-lite
Version:
Stress-free JavaScript unit testing and functional testing tool, works in NodeJS and browsers
113 lines (82 loc) • 3.33 kB
JavaScript
/*
Siesta 5.6.1
Copyright(c) 2009-2022 Bryntum AB
https://bryntum.com/contact
https://bryntum.com/products/siesta/license
*/
!function () {
const Nyc = require('nyc')
const libReport = require('istanbul-lib-report')
const reports = require('istanbul-reports')
const path = require('path')
const fs = require('fs')
const Yargs = require('yargs/yargs')
const uuid = require('uuid').v4
Role('Siesta.Launcher.Role.CanWorkWithNyc', {
methods : {
buildNycArgv : function (rawNycArgs) {
const { setupOptions } = require('nyc/lib/commands/helpers')
const yargs = Yargs([])
setupOptions(yargs, null, process.cwd())
return yargs.parse(rawNycArgs)
},
buildNyc : async function (argv) {
if (!argv.instrument)
argv.instrumenter = './lib/instrumenters/noop'
else
argv.instrumenter = './lib/instrumenters/istanbul'
// otherwise caching of the instrumentation results will be disabled
argv.isChildProcess = true
let nyc = new Nyc(argv)
if (argv.clean) {
await nyc.reset()
} else {
await nyc.createTempDirectory()
}
return nyc
},
// tweaked version of nyc's `nycWriteCoverageFile` method, does not use hard-coded `coverageFinder`
nycWriteCoverageFile : function (nyc, coverage) {
// Remove any files that should be excluded but snuck into the coverage
Object.keys(coverage).forEach(function (absFile) {
if (!nyc.exclude.shouldInstrument(absFile)) {
delete coverage[absFile]
}
}, nyc)
if (nyc.cache) {
Object.keys(coverage).forEach(function (absFile) {
if (nyc.hashCache[absFile] && coverage[absFile]) {
coverage[absFile].contentHash = nyc.hashCache[absFile]
}
}, nyc)
}
var id = uuid()
var coverageFilename = path.resolve(nyc.tempDirectory(), id + '.json')
fs.writeFileSync(
coverageFilename,
JSON.stringify(coverage),
'utf-8'
)
},
// tweaked version of nyc's `report` method, uses custom source finder
nycReport : async function (nyc, sourceFinder) {
const libReport = require('istanbul-lib-report')
const reports = require('istanbul-reports')
const context = libReport.createContext({
dir : nyc.reportDirectory(),
watermarks : nyc.config.watermarks,
coverageMap : await nyc.getCoverageMapFromAllCoverageFiles(),
sourceFinder : sourceFinder
})
nyc.reporter.forEach((_reporter) => {
reports.create(_reporter, {
skipEmpty : nyc.config.skipEmpty,
skipFull : nyc.config.skipFull,
projectRoot : nyc.cwd,
maxCols : process.stdout.columns || 100
}).execute(context)
})
}
}
})
}();