Add ability to run tests with coverage report

This commit is contained in:
Kevin Sawicki 2016-08-02 11:53:13 -07:00
parent e17e195479
commit 491f69df80
5 changed files with 36 additions and 27 deletions

View file

@ -22,7 +22,7 @@ glob.sync('**/*.js', {cwd: libPath}).forEach(function (relativePath) {
fs.writeFileSync(generatedPath, generated)
})
var asarPath = path.join(outputPath, 'electron-instrumented.asar')
var asarPath = path.join(outputPath, 'electron.asar')
asar.createPackageWithOptions(path.join(outputPath, 'lib'), asarPath, {}, function (error) {
if (error) {
console.error(error.stack || error)

51
spec/coverage/reporter.js Normal file
View file

@ -0,0 +1,51 @@
const asar = require('asar')
const fs = require('fs')
const glob = require('glob')
const mkdirp = require('mkdirp')
const path = require('path')
const {Collector, Instrumenter, Reporter} = require('istanbul')
const outputPath = path.join(__dirname, '..', '..', 'out', 'coverage')
const libPath = path.join(__dirname, '..', '..', 'lib')
// Add unrequired files to the coverage report so all files are present there
const addUnrequiredFiles = (coverage) => {
const instrumenter = new Instrumenter()
const libPath = path.join(__dirname, '..', '..', 'lib')
glob.sync('**/*.js', {cwd: libPath}).map(function (relativePath) {
return path.join(libPath, relativePath)
}).filter(function (filePath) {
return coverage[filePath] == null
}).forEach(function (filePath) {
instrumenter.instrumentSync(fs.readFileSync(filePath, 'utf8'), filePath)
// When instrumenting the code, istanbul will give each FunctionDeclaration
// a value of 1 in coverState.s,presumably to compensate for function
// hoisting. We need to reset this, as the function was not hoisted, as it
// was never loaded.
Object.keys(instrumenter.coverState.s).forEach(function (key) {
instrumenter.coverState.s[key] = 0
});
coverage[filePath] = instrumenter.coverState
})
}
// Generate a code coverage report in out/coverage/lcov-report
exports.generateReport = () => {
const coverage = window.__coverage__
if (coverage == null) return
addUnrequiredFiles(coverage)
const collector = new Collector()
collector.add(coverage)
const {ipcRenderer} = require('electron')
collector.add(ipcRenderer.sendSync('get-coverage'))
const reporter = new Reporter(null, outputPath)
reporter.addAll(['text', 'lcov'])
reporter.write(collector, true, function () {})
}