Skip to content

Commit

Permalink
Coverage reports now accurately reflect source
Browse files Browse the repository at this point in the history
Aka the previous coverage reports over the transpiled js (from ts) now
have sourcemaps applied to them so that the coverage reports are now
over ts.

Also made coverage reports be automatically written as the tests are
run.
  • Loading branch information
DNoved1 committed Mar 8, 2016
1 parent 2064595 commit f76e51e
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 12 deletions.
44 changes: 44 additions & 0 deletions bin/browser-sync-test.js
@@ -0,0 +1,44 @@
#!/usr/bin/env node

var bs = require('browser-sync').create();
var fs = require('fs');
var path = require('path');
var remap = require('remap-istanbul/lib/remap');
var writeReport = require('remap-istanbul/lib/writeReport');

bs.init({
server: {
directory: true
},
startPath: '../index.html',
files: '../src/**'
}, function() {
bs.sockets.on('connection', function(client) {
client.on('argon:coverage', function(data) {
var coverage = data.coverage;
var sourceMaps = data.sourceMaps;
var sourceJs = data.sourceJs;

var collector = remap(coverage, {
readFile: function(fileName) {
if (sourceJs[fileName]) {
return new Buffer(sourceJs[fileName]);
} else {
throw new Error('No such js file "' + fileName + '".');
}
},
readJSON: function(fileName) {
if (sourceMaps[fileName]) {
return sourceMaps[fileName];
} else {
throw new Error('No such source map "' + fileName + '".');
}
}
});

writeReport(collector, 'html', path.join(__dirname, '../coverage')).then(function() {
console.log('Wrote new coverage report to "' + path.join(__dirname, '../coverage/index.html') + '".');
});
});
});
});
1 change: 1 addition & 0 deletions index.html
Expand Up @@ -8,5 +8,6 @@
}
</style>
<h3><a href="/test/index.html" onclick="window.open('/test/index.html', 'Test Runner', 'width=550, height=800'); return false;">Test Runner</a></h3>
<h3><a href="/coverage/index.html">Code Coverage</a></h3>
<h3><a href="/">Documentation</a></h3>
<h3><a href="/example/">Example</a></h3>
2 changes: 1 addition & 1 deletion jspm.config.js
Expand Up @@ -8,7 +8,7 @@ SystemJS.config({
typescriptOptions: {
"tsconfig": true,
"typeCheck": false,
"sourceMap": false
"sourceMap": true
},

meta: {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -109,6 +109,7 @@
"devDependencies": {
"browser-sync": "^2.11.0",
"jspm": "^0.17.0-beta.9",
"remap-istanbul": "^0.5.1",
"tsconfig-glob": "^0.2.1",
"typedoc": "^0.3.12",
"typescript": "^1.9.0-dev.20160226",
Expand All @@ -118,8 +119,7 @@
"tsconfig": "tsconfig .",
"format": "tsfmt -r",
"build": "jspm build src/argon.ts dist/argon.js --format umd --global-name Argon --skip-source-maps",
"browser-sync": "browser-sync start --server --directory --startPath \"index.html\" --files \"src/**\"",
"test": "npm run browser-sync",
"test": "node bin/browser-sync-test.js",
"typedoc": "typedoc --out docs --gaID UA-63191442-2 --name argon.js --readme README.md --target ES5 --module commonjs --experimentalDecorators --excludeNotExported --excludeExternals src/argon.ts src/context.ts src/device.ts src/reality.ts src/view.ts src/session.ts src/timer.ts src/utils.ts src/vuforia.ts src/definitions jspm_packages/github/aurelia/dependency-injection@0.12.1/aurelia-dependency-injection.d.ts jspm_packages/github/aurelia/logging@0.9.0/aurelia-logging.d.ts jspm_packages/github/aurelia/metadata@0.10.1/aurelia-metadata.d.ts jspm_packages/github/aurelia/pal@0.3.0/aurelia-pal.d.ts typings/browser.d.ts"
}
}
27 changes: 18 additions & 9 deletions test/index.html
Expand Up @@ -10,6 +10,10 @@
<div id="mocha"></div>
<script>
System.import('istanbul/lib/instrumenter.js').then(function(Instrumenter) {
// A map from argon source file names to their source maps
var sourceMaps = {};
// A map from argon source file names to their transpiled js
var sourceJs = {};

// Override the systemjs translate function so that our typescript
// is instrumented by istanbul for code coverage.
Expand All @@ -27,9 +31,17 @@
var fileNameStart = load.address.indexOf('src/');
fileNameStart = fileNameStart === -1 ? load.address.indexOf('test/') : fileNameStart;
var fileName = load.address.substring(fileNameStart);
fileName = fileName.slice(0, -3);
var baseFileName = fileName.slice(fileName.lastIndexOf('/') + 1);

result = result.then(function(code) {
load.source = new Instrumenter({embedSource:true}).instrumentSync(code, fileName);
var codeWithSourceMapComment = '//# sourceMappingURL=' + baseFileName + '.js.map\n' + code;

sourceJs[fileName + '.js'] = codeWithSourceMapComment;
sourceMaps[fileName + '.js.map'] = load.metadata.sourceMap;
sourceMaps[fileName + '.js.map'].sources = [baseFileName + '.ts'];

load.source = new Instrumenter().instrumentSync(codeWithSourceMapComment, fileName + '.js');

// TODO: should also update load.sourceMap
return load.source;
Expand All @@ -47,14 +59,11 @@
System.import('test/test.ts!').then(function() {
mocha.checkLeaks();
mocha.run(function() {
// Prompt the user to copy the code coverage data to the
// clipboard.
//
// TODO: it would be better to automatically create the
// file with the browser file api (chrome only) or with web
// sockets (to pass this back to the server where it can
// write the file to disk).
window.prompt("The code coverage data:", JSON.stringify(window.__coverage__));
window.___browserSync___.socket.emit('argon:coverage', {
coverage: window.__coverage__,
sourceMaps: sourceMaps,
sourceJs: sourceJs
});
});
});
});
Expand Down

0 comments on commit f76e51e

Please sign in to comment.