maputnik/test/functional/util/coverage.js

57 lines
1.7 KiB
JavaScript
Raw Normal View History

2018-01-10 14:19:34 +01:00
var artifacts = require("../../artifacts");
var fs = require("fs");
var istanbulCov = require('istanbul-lib-coverage');
var COVERAGE_PATH = artifacts.pathSync("/coverage");
var coverage = istanbulCov.createCoverageMap({});
2018-01-17 18:36:46 +01:00
// Capture the coverage after each test
afterEach(async function() {
2018-01-17 18:36:46 +01:00
// Code coverage
var results = await browser.execute(function() {
2018-01-17 18:36:46 +01:00
return window.__coverage__;
});
2018-01-10 14:19:34 +01:00
if (results) {
coverage.merge(results);
}
2018-01-17 18:36:46 +01:00
})
2018-01-10 14:19:34 +01:00
2018-01-17 18:36:46 +01:00
// Dump the coverage to a file
after(function() {
// Sometimes istanbul copies same location entry with null values
// crashing the final coverage step. This is just a workaround for now,
// since istanbul will be replaced by nyc.
const coverageJson = JSON.stringify(coverage, null, 2);
let newCoverage = JSON.parse(coverageJson);
Object.values(newCoverage).forEach(fileCov => {
if (fileCov.branchMap) {
Object.values(fileCov.branchMap).forEach(branchMapEntry => {
let prevLocation = {};
branchMapEntry.locations.forEach(curLocation => {
if (curLocation.start && curLocation.end &&
curLocation.start.column && curLocation.start.line &&
curLocation.end.column && curLocation.end.line)
{
prevLocation = curLocation;
}
else
{
curLocation.start.column = prevLocation.start.column;
curLocation.start.line = prevLocation.start.line;
curLocation.end.column = prevLocation.end.column;
curLocation.end.line = prevLocation.end.line;
}
})
})
}
})
const newCoverageJson = JSON.stringify(newCoverage, null, 2);
fs.writeFileSync(COVERAGE_PATH+"/coverage.json", newCoverageJson);
2018-01-10 14:19:34 +01:00
})