Skip to content
This repository has been archived by the owner on Aug 19, 2019. It is now read-only.

Commit

Permalink
➕ remap coverage to original source if it was transpiled
Browse files Browse the repository at this point in the history
  • Loading branch information
deepsweet committed Sep 19, 2016
1 parent 756573f commit b9f0299
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 30 deletions.
19 changes: 19 additions & 0 deletions lib/hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Hooks {
constructor() {
this.hooks = [];
}

add(hook) {
this.hooks.push(hook);
}

clearAll() {
this.hooks = this.hooks.filter((hook) => {
hook();

return false;
});
}
}

export default new Hooks();
10 changes: 7 additions & 3 deletions lib/instrument.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { coverageVariable, addHook, clearHooks } from './utils';
import coverageVariable from './variable';
import hooks from './hooks';
import remapper from './remapper';

export default (options) => (input) => {
return function istanbulInstrument(log) {
Expand All @@ -11,7 +13,8 @@ export default (options) => (input) => {
coverageVariable
});

clearHooks();
hooks.clearAll();
remapper.createStore();

const hook = hookRequire(
// hook requires matches input files
Expand All @@ -22,13 +25,14 @@ export default (options) => (input) => {
(source, file) => {
const result = instrumenter.instrumentSync(source, file);

remapper.addSource(file, source);
log(file);

return result;
}
);

addHook(hook);
hooks.add(hook);
log('require() is hooked');

resolve(input);
Expand Down
32 changes: 32 additions & 0 deletions lib/remapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { createSourceMapStore } from 'istanbul-lib-source-maps';
import { fromSource as getSourceMapFromSource } from 'convert-source-map';

const isLargeSource = true;

class Remapper {
constructor() {
this.sourceMapStore = null;
}

createStore() {
this.sourceMapStore = createSourceMapStore();
}

addSource(file, source) {
const sourceMap = getSourceMapFromSource(source, isLargeSource);

if (sourceMap) {
this.sourceMapStore.registerMap(file, sourceMap.toObject());
}
}

remapCoverageMap(coverageMap) {
const transformedCoverage = this.sourceMapStore.transformCoverage(coverageMap);

if (transformedCoverage) {
return transformedCoverage.map;
}
}
}

export default new Remapper();
13 changes: 8 additions & 5 deletions lib/report.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
import { coverageVariable, clearHooks } from './utils';
import coverageVariable from './variable';
import hooks from './hooks';
import remapper from './remapper';

export default (formats = [ 'lcovonly', 'text-summary' ]) => (input) => {
return function istanbulReport(log) {
const libCoverage = require('istanbul-lib-coverage');
const createCoverageMap = require('istanbul-lib-coverage').createCoverageMap;
const createReporter = require('istanbul-api').createReporter;

return new Promise((resolve, reject) => {
clearHooks();
hooks.clearAll();

if (!global[coverageVariable]) {
return reject('no coverage information was collected');
}

const coverageMap = libCoverage.createCoverageMap(global[coverageVariable]);
const coverageMap = createCoverageMap(global[coverageVariable]);
const remappedCoverageMap = remapper.remapCoverageMap(coverageMap);
const reporter = createReporter();

formats.forEach((format) => {
log(format);

reporter.add(format);
reporter.write(coverageMap);
reporter.write(remappedCoverageMap);
});

resolve(input);
Expand Down
11 changes: 7 additions & 4 deletions lib/thresholds.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import { coverageVariable, clearHooks } from './utils';
import coverageVariable from './variable';
import hooks from './hooks';
import remapper from './remapper';

export default (options = {}) => (input) => {
return function istanbulThresholds(/* log */) {
const libCoverage = require('istanbul-lib-coverage');

return new Promise((resolve, reject) => {
clearHooks();
hooks.clearAll();

if (!global[coverageVariable]) {
return reject('no coverage information was collected');
}

const coverageMap = libCoverage.createCoverageMap(global[coverageVariable]);
const remappedCoverageMap = remapper.remapCoverageMap(coverageMap);
const coverageSummary = libCoverage.createCoverageSummary();

coverageMap.files().forEach((file) => {
const fileCoverage = coverageMap.fileCoverageFor(file);
remappedCoverageMap.files().forEach((file) => {
const fileCoverage = remappedCoverageMap.fileCoverageFor(file);
const fileSummary = fileCoverage.toSummary();

coverageSummary.merge(fileSummary);
Expand Down
13 changes: 0 additions & 13 deletions lib/utils.js

This file was deleted.

1 change: 1 addition & 0 deletions lib/variable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default '__start_istanbul__';
19 changes: 14 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,27 @@
"name": "start-istanbul",
"version": "2.0.0",
"description": "Istanbul tasks for Start",
"keywords": [ "start", "start-task", "istanbul", "coverage" ],
"keywords": [
"start",
"start-task",
"istanbul",
"coverage"
],
"homepage": "https://github.com/start-runner/istanbul",
"repository": "start-runner/istanbul",
"author": "Kir Belevich <kir@soulshine.in> (https://github.com/deepsweet)",
"files": [ "build/" ],
"files": [
"build/"
],
"main": "build/index.js",
"dependencies": {
"babel-runtime": "^6.11.6",
"convert-source-map": "^1.3.0",
"istanbul-api": "^1.0.0-aplha.10",
"istanbul-lib-coverage": "^1.0.0",
"istanbul-lib-hook": "^1.0.0-alpha.4",
"istanbul-lib-instrument": "^1.1.0",
"istanbul-lib-coverage": "^1.0.0",
"istanbul-api": "^1.0.0-aplha.10",
"babel-runtime": "^6.11.6"
"istanbul-lib-source-maps": "^1.0.1"
},
"devDependencies": {
"start-babel-cli": "^2.0.1",
Expand Down

0 comments on commit b9f0299

Please sign in to comment.