metro/scripts/mapCoverage.js

50 lines
1.6 KiB
JavaScript
Raw Permalink Normal View History

2017-01-26 12:56:40 +00:00
/**
* Copyright (c) 2014-present, Facebook, Inc.
2017-01-26 12:56:40 +00:00
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
2017-01-26 12:56:40 +00:00
*/
/**
* Because we have a build step, sometimes we can test files from both
* `packages/metro-whatever/build/*` and `packages/metro-whatever/src/*`
*
* If we require file by its relative path like:
* // inside `metro-whatever/src/__tests__/index.js`
* require('../index.js'); // this will require `metro-whatever/src/index.js`
*
* But if we require it by a package name, this will go through node_modules
* and lerna index.js link. So the actual file will be required from `build/`
* // inside another packages
* // this will go through lerna and require `metro-whatever/build/index.js
* require('metro-whatever')
*
* these files are identical (one is preprocessed, another is transformed on
* the fly), but the coverage paths are different.
* This script will map coverage results from both locations to one and
* produce a full coverage report.
*/
const createReporter = require('istanbul-api').createReporter;
const istanbulCoverage = require('istanbul-lib-coverage');
2017-05-22 10:51:24 +00:00
const coverage = require('../coverage/coverage-final.json');
2017-01-26 12:56:40 +00:00
const map = istanbulCoverage.createCoverageMap();
const reporter = createReporter();
const mapFileCoverage = fileCoverage => {
2017-05-22 10:51:24 +00:00
fileCoverage.path = fileCoverage.path.replace(
/(.*packages\/.*\/)(build)(\/.*)/,
'$1src$3'
2017-05-22 10:51:24 +00:00
);
2017-01-26 12:56:40 +00:00
return fileCoverage;
};
2017-05-22 10:51:24 +00:00
Object.keys(coverage).forEach(filename =>
map.addFileCoverage(mapFileCoverage(coverage[filename]))
2017-01-26 12:56:40 +00:00
);
reporter.addAll(['json', 'lcov', 'text']);
reporter.write(map);