Skip to content

Commit f76e51e

Browse files
author
DNoved1
committed
Coverage reports now accurately reflect source
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.
1 parent 2064595 commit f76e51e

File tree

5 files changed

+66
-12
lines changed

5 files changed

+66
-12
lines changed

bin/browser-sync-test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env node
2+
3+
var bs = require('browser-sync').create();
4+
var fs = require('fs');
5+
var path = require('path');
6+
var remap = require('remap-istanbul/lib/remap');
7+
var writeReport = require('remap-istanbul/lib/writeReport');
8+
9+
bs.init({
10+
server: {
11+
directory: true
12+
},
13+
startPath: '../index.html',
14+
files: '../src/**'
15+
}, function() {
16+
bs.sockets.on('connection', function(client) {
17+
client.on('argon:coverage', function(data) {
18+
var coverage = data.coverage;
19+
var sourceMaps = data.sourceMaps;
20+
var sourceJs = data.sourceJs;
21+
22+
var collector = remap(coverage, {
23+
readFile: function(fileName) {
24+
if (sourceJs[fileName]) {
25+
return new Buffer(sourceJs[fileName]);
26+
} else {
27+
throw new Error('No such js file "' + fileName + '".');
28+
}
29+
},
30+
readJSON: function(fileName) {
31+
if (sourceMaps[fileName]) {
32+
return sourceMaps[fileName];
33+
} else {
34+
throw new Error('No such source map "' + fileName + '".');
35+
}
36+
}
37+
});
38+
39+
writeReport(collector, 'html', path.join(__dirname, '../coverage')).then(function() {
40+
console.log('Wrote new coverage report to "' + path.join(__dirname, '../coverage/index.html') + '".');
41+
});
42+
});
43+
});
44+
});

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
}
99
</style>
1010
<h3><a href="/test/index.html" onclick="window.open('/test/index.html', 'Test Runner', 'width=550, height=800'); return false;">Test Runner</a></h3>
11+
<h3><a href="/coverage/index.html">Code Coverage</a></h3>
1112
<h3><a href="/">Documentation</a></h3>
1213
<h3><a href="/example/">Example</a></h3>

jspm.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ SystemJS.config({
88
typescriptOptions: {
99
"tsconfig": true,
1010
"typeCheck": false,
11-
"sourceMap": false
11+
"sourceMap": true
1212
},
1313

1414
meta: {

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
"devDependencies": {
110110
"browser-sync": "^2.11.0",
111111
"jspm": "^0.17.0-beta.9",
112+
"remap-istanbul": "^0.5.1",
112113
"tsconfig-glob": "^0.2.1",
113114
"typedoc": "^0.3.12",
114115
"typescript": "^1.9.0-dev.20160226",
@@ -118,8 +119,7 @@
118119
"tsconfig": "tsconfig .",
119120
"format": "tsfmt -r",
120121
"build": "jspm build src/argon.ts dist/argon.js --format umd --global-name Argon --skip-source-maps",
121-
"browser-sync": "browser-sync start --server --directory --startPath \"index.html\" --files \"src/**\"",
122-
"test": "npm run browser-sync",
122+
"test": "node bin/browser-sync-test.js",
123123
"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"
124124
}
125125
}

test/index.html

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
<div id="mocha"></div>
1111
<script>
1212
System.import('istanbul/lib/instrumenter.js').then(function(Instrumenter) {
13+
// A map from argon source file names to their source maps
14+
var sourceMaps = {};
15+
// A map from argon source file names to their transpiled js
16+
var sourceJs = {};
1317

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

3137
result = result.then(function(code) {
32-
load.source = new Instrumenter({embedSource:true}).instrumentSync(code, fileName);
38+
var codeWithSourceMapComment = '//# sourceMappingURL=' + baseFileName + '.js.map\n' + code;
39+
40+
sourceJs[fileName + '.js'] = codeWithSourceMapComment;
41+
sourceMaps[fileName + '.js.map'] = load.metadata.sourceMap;
42+
sourceMaps[fileName + '.js.map'].sources = [baseFileName + '.ts'];
43+
44+
load.source = new Instrumenter().instrumentSync(codeWithSourceMapComment, fileName + '.js');
3345

3446
// TODO: should also update load.sourceMap
3547
return load.source;
@@ -47,14 +59,11 @@
4759
System.import('test/test.ts!').then(function() {
4860
mocha.checkLeaks();
4961
mocha.run(function() {
50-
// Prompt the user to copy the code coverage data to the
51-
// clipboard.
52-
//
53-
// TODO: it would be better to automatically create the
54-
// file with the browser file api (chrome only) or with web
55-
// sockets (to pass this back to the server where it can
56-
// write the file to disk).
57-
window.prompt("The code coverage data:", JSON.stringify(window.__coverage__));
62+
window.___browserSync___.socket.emit('argon:coverage', {
63+
coverage: window.__coverage__,
64+
sourceMaps: sourceMaps,
65+
sourceJs: sourceJs
66+
});
5867
});
5968
});
6069
});

0 commit comments

Comments
 (0)