/
testem.js
77 lines (67 loc) · 2.19 KB
/
testem.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
var path = require('path');
var http = require('http');
var fs = require('fs');
var shell = require('shelljs');
var port = 7358;
var server;
module.exports = {
serve_files: [
'instrumented/*.js'
],
src_files: [
'src/*.js'
],
// proxy to the create http server
// phantomjs has problems with POSTs to different port
"proxies": {
"/coverage": {
"target": "http://localhost:7358"
}
},
// instrument files, spin up http server to write coverage data to disk
before_tests: function(config, data, callback)
{
shell.exec('node ./node_modules/istanbul/lib/cli.js instrument --output instrumented src', function(code, output)
{
if (code) {
callback(code, output);
return;
}
// if instrumented successfully
// start the server
server = http.createServer(function(req, res) {
console.error('... Received coverage of', req.headers['content-length'], 'length');
// need separate files per browser/client
req.pipe(fs.createWriteStream(path.join(__dirname, 'coverage-' + Math.random() + '.json')));
// make sure we've got it all
req.on('end', res.end.bind(res));
}).listen(port, function(serverErr) {
console.error('------ Listening for coverage on ' + port);
// when server is ready
// pass control back to testem
callback(serverErr);
});
});
},
// after tests done, stop http server, combined coverage data into the report
after_tests: function(config, data, callback)
{
// shutdown teh server
server.close();
// generate report
shell.exec('node ./node_modules/istanbul/lib/cli.js report', function(code, output) {
if (code) return callback(code, output);
// check on generated report
var lcov = shell.grep('end_of_record', path.join(__dirname, 'coverage/lcov.info'));
var report = shell.grep('src/index.html', path.join(__dirname, 'coverage/lcov-report/index.html'));
if (!lcov || !report) {
callback(new Error('Unable to generate report'));
return;
}
// everything is good
callback(null);
});
},
test_page: 'tests.html',
launch_in_dev: ['chrome']
}