UNPKG

1.86 kBJavaScriptView Raw
1/** Copyright (c) 2018 Uber Technologies, Inc.
2 *
3 * This source code is licensed under the MIT license found in the
4 * LICENSE file in the root directory of this source tree.
5 *
6 * @flow
7 */
8
9/* eslint-env node */
10
11const fs = require('fs');
12
13// Jest writes coverage after the process exits
14// so we need to poll and wait for all coverage files.
15const waitForAllCoverage = dirs => {
16 const maxTries = 10000;
17 let numTries = 0;
18 return Promise.all(
19 dirs.map(
20 dir =>
21 new Promise((resolve, reject) => {
22 const interval = setInterval(() => {
23 const coverageExists = fs.existsSync(
24 `${dir}/coverage/coverage-final.json`
25 );
26 numTries += 1;
27 if (coverageExists) {
28 clearInterval(interval);
29 resolve();
30 } else if (numTries > maxTries) {
31 clearInterval(interval);
32 reject();
33 }
34 }, 100);
35 })
36 )
37 );
38};
39
40module.exports = function(rootDir /*: string */) {
41 return waitForAllCoverage([rootDir]).then(() => {
42 const createReporter = require('istanbul-api').createReporter;
43 const istanbulCoverage = require('istanbul-lib-coverage');
44
45 const map = istanbulCoverage.createCoverageMap();
46 const reporter = createReporter();
47
48 [rootDir].forEach(dir => {
49 // $FlowFixMe
50 const coverage = require(`${dir}/coverage/coverage-final.json`);
51 Object.keys(coverage).forEach(filename => {
52 const obj = coverage[filename];
53 // It seems coverage objects are arbitrarily nested or not
54 // See: https://github.com/fusionjs/fusion-cli/pull/489
55 map.addFileCoverage(obj.data ? obj.data : obj);
56 });
57 });
58
59 reporter.dir = `${rootDir}/coverage`;
60 reporter.addAll(['json', 'lcov', 'text', 'cobertura']);
61 reporter.write(map);
62 });
63};