forked from diverted247/txtjs
-
Notifications
You must be signed in to change notification settings - Fork 3
/
testem-coverage-server.js
74 lines (63 loc) · 1.77 KB
/
testem-coverage-server.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
/** eslint-env node */
const path = require("path");
const http = require("http");
const fs = require("fs");
const shell = require("shelljs");
const COVERAGE_SERVER_PORT = 7358;
let server;
function startCoverageServer(callback) {
// 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
let outputFile = ".nyc_output.json";
req.pipe(fs.createWriteStream(path.join(__dirname, "tmp", outputFile)));
// make sure we've got it all
req.on("end", res.end.bind(res));
})
.listen(COVERAGE_SERVER_PORT, function(serverErr) {
console.error("------ Listening for coverage on " + COVERAGE_SERVER_PORT);
// when server is ready
// pass control back to testem
callback(serverErr);
});
}
function shutdownCoverageServer(callback) {
// shutdown teh server
server.close();
// generate report
shell.exec("npx nyc report", function(code, output) {
if (code) return callback(code, output);
// check on generated report
const lcov = shell.grep(
"end_of_record",
path.join(__dirname, "coverage/lcov.info")
);
const 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);
});
}
module.exports = {
proxies: {
"/coverage": {
target: "http://localhost:" + COVERAGE_SERVER_PORT,
},
},
startCoverageServer,
shutdownCoverageServer,
clientFile: "testem-client-hook.js",
};