New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
relative paths (projectRoot) for json reporter #701
Comments
to support relative paths Closes istanbuljs#701
to support relative paths Closes istanbuljs#701
any updates ? This PR is ok for you ? |
@sebaplaza I have the exact same setup (!) with a monorepo (using turborepo) and I am also combining all the coverage reports together into one big one. My research also lead me to the missing |
@raspo i would love to merge this mr, but this project seems dead, there is no one to merge this. I've been dealing with this issue using 2 different cache hashes (one for CI and the other one for local runs). To make a different turborepo hash i'm using an env variable that is only present in ci environments (in my case gitlab).
{
"$schema": "https://turborepo.org/schema.json",
"globalEnv": ["GITLAB_CI"],
"pipeline": {
...
} |
@sebaplaza I opted for another workaround... by (ab)using a custom test reporter. // tools/relative-coverage-reporter.js
const fs = require('fs');
const path = require('path');
function loadCoverageFile(coverageFilePath) {
try {
return require(coverageFilePath);
} catch (e) {
console.error(`There was an error trying to load the coverage file: ${e}`);
}
return false;
}
// NOTE: not technically a reporter, this is a custom script that runs after the tests
// it's a bit of a hack, but it's the only way to get the coverage report to show relative paths
class RelativeCoverageReporter {
constructor(globalConfig, reporterOptions, _reporterContext) {
this._globalConfig = globalConfig;
this._options = reporterOptions;
}
onRunComplete() {
// sanity check
if (!this._globalConfig.collectCoverage) return;
if (this._globalConfig.watch) return;
const coverageFilePath = path.join(this._globalConfig.coverageDirectory, 'coverage-final.json');
// the root path of the monorepo
const projectRoot = this._options.projectRoot || path.resolve(__dirname, '../');
// load coverage file generated by istanbul
const coverageJSON = loadCoverageFile(coverageFilePath);
const newCoverageJSON = Object.keys(coverageJSON).reduce((acc, key) => {
const fileCoverage = coverageJSON[key];
// convert to relative paths
const relativePath = path.relative(projectRoot, fileCoverage.path);
acc[relativePath] = { ...fileCoverage, path: relativePath };
return acc;
}, {});
// overwrite the coverage results file
fs.writeFileSync(coverageFilePath, JSON.stringify(newCoverageJSON));
}
}
module.exports = RelativeCoverageReporter; You can reference it from any module.exports = {
coverageReporters: ['json'],
reporters: ['default', require.resolve('../../tools/relative-coverage-reporter.js')] // or whatever path
// ...
}; At first I thought about writing a custom coverage reporter that would wrap the |
An alternative to the custom reporter might be to using "patch-package" to modify the code. |
Hello,
I have a monorepo with a bunch of projects.
Every project is generating a json coverage report.
I'm using nyc merge and then nyc report all these partial json coverage files into an unique one.
I made a lib to accomplish this.
All this is working great, but i have still an issue...
To optimize CI speeds, i'm caching the coverage task results (json coverage files) via turborepo cache
The generated paths in json coverage are absolutes, and of course, they are different between dev machines and CI. nyc report fails because the physical paths doesn't exists.
I would like to specify a custom path for json coverage files, just like the lcov reporter
The text was updated successfully, but these errors were encountered: