SonarCloud集成CircleCI后代码覆盖率显示0%的问题求助
Let’s work through this issue step by step—there are a few common culprits when coverage doesn’t show up even though the build succeeds.
1. Verify the Coverage Report Path Exists & Is Correct
First, double-check that your lcov.info file is actually being generated at the path you specified in sonar-project.properties:
sonar.javascript.lcov.reportPaths=src/app/client/coverage/lcov.info
Add a step in your CircleCI config right before running sonar-scanner to confirm the file exists:
ls -la src/app/client/coverage/
If the file isn’t there, your test command isn’t generating the coverage report properly. Make sure your test runner (like Jest, Mocha) is configured to output an lcov report to that exact directory. For example, with Jest, you’d run:
jest --coverage --coverageReporters=lcov --coverageDirectory=src/app/client/coverage
Or set these options in your jest.config.js:
module.exports = { coverageReporters: ['lcov'], coverageDirectory: 'src/app/client/coverage' };
2. Fix the sonar.tests Path (Possible Duplication)
Looking at your config, the tests path looks redundant:
sonar.tests=src/app/client/src/app/
That path has src/app/client/src/app/—did you mean src/app/client/app/ instead? If the test files aren’t at that duplicated path, SonarCloud can’t map coverage data to your test files, which might lead to 0% coverage. Update this path to point directly to your test files (or the root of your test directory).
3. Ensure sonar-scanner Runs After Coverage is Generated
Your CircleCI job must run the test command (which creates the coverage report) before executing sonar-scanner. Check your .circleci/config.yml to confirm the order is:
- Install dependencies
- Run tests + generate coverage report
- Run
sonar-scanner
Ifsonar-scannerruns first, it’ll look for a coverage report that doesn’t exist yet, resulting in 0% coverage.
4. Check for Path Mismatches in lcov.info
Sometimes the coverage report uses absolute paths instead of relative paths, which SonarCloud can’t match to your source files. Run this in your CircleCI job to inspect the report:
cat src/app/client/coverage/lcov.info
Look for lines starting with SF:—they should be relative to your project root, like:
SF:src/app/client/src/app/components/MyComponent.js
If they’re absolute (e.g., SF:/home/circleci/project/src/...), adjust your test runner to output relative paths. For Jest, you can set rootDir in your config to project root, which usually fixes this.
5. Validate SonarCloud Project Configuration
Double-check that your sonar.projectKey and sonar.organization match exactly what’s set in your SonarCloud dashboard. A mismatch here could cause data to be sent to the wrong project (or not show up at all), but since you said the build succeeds, this is less likely—but worth confirming.
Once you’ve worked through these checks, re-run your CircleCI build and check the SonarCloud dashboard again. Most of the time, the issue is a path mismatch or report generation timing problem.
内容的提问来源于stack exchange,提问作者Harsha

