29

I've been trying to find out how to populate SonarQube with both my Jest Unit Tests and the .net Unit Tests.

I have a local version of SQ 6.7 and all the latest versions of the Javascript and C# Plugins.

When it comes to Jest, I have the sonar-jest-reporter to export a test-report.xml file, and also have the lcov.info file being generated.

SonarQube is able to read the lcov.info and I see a coverage %, but no matter what I set as include/exclude, or tests path, it will not show the tests associated with the source file.

The file structure is all .js and the .test.js are in the same directory with each module.

Any help with pointing me in the correct direction, or others who have encountered and overcome this issue that would be appreciated.

Paul T. Rawkeen
  • 3,994
  • 3
  • 35
  • 51
Matt B.
  • 397
  • 1
  • 4
  • 7
  • 1
    Why do you want it to show the test files? The coverage is for your `.js` files not your `.tests.js`. Am I misunderstanding what you are trying to do? As far as I know the lcov file doesn't include data about which tests are associated with which source files. It only includes data on which lines/branches are covered by tests. – tehbeardedone Dec 20 '17 at 22:24
  • Where did you read this is possible in SonarQube? – Jeroen Heier Dec 21 '17 at 04:47
  • I see the count of unit tests, but when you select it, they are blank. That's what I am trying to figure out. Isn't that a feature of SQ to see the coverage and the tests? – Matt B. Dec 21 '17 at 14:53

2 Answers2

49

Looks like I've figured out the way to make it work. The trick is to have both sonar.sources and sonar.tests point to same directory (because we have both tests and source in the same directory) then use sonar.tests.inclusions to pattern match test files with .test.js or .spec.js extension.

Here's a sample sonar-project.properties which assumes the following:

  1. src/components/Foo/Foo.jsx the main component.
  2. src/components/Foo/Foo.spec.jsx the test file.
# Source
sonar.sources=src
# Where to find tests file, also src
sonar.tests=src
# But we get specific here
# We don't need to exclude it in sonar.sources because it is automatically taken care of
sonar.test.inclusions=src/**/*.spec.js,src/**/*.spec.jsx,src/**/*.test.js,src/**/*.test.jsx

# Now specify path of lcov and testlog
sonar.javascript.lcov.reportPaths=coverage/jest/lcov.info
sonar.testExecutionReportPaths=coverage/jest/testlog.xml

Now your test files will also show up.

More information here

Paul T. Rawkeen
  • 3,994
  • 3
  • 35
  • 51
Swashata Ghosh
  • 988
  • 8
  • 15
  • I am using the Gradle plugin org.sonarqube (Version 3.3 at the moment) and had to modify the configuration a bit: In the property sonar.test.inclusions I had to _remove_ the preceding `src/` as it seems that the plugin looked for tests with "src/src/..." in the path and complained that I didn't have that path for my tests. (Actually it didn't exactly complain about that, only read it from a stack trace). – cyberbrain Jul 27 '21 at 12:15
  • when I set `sonar.sources` and `sonar.tests` to the same path I get this error: ##[error]ERROR: Error during SonarScanner execution ERROR: File App.config can't be indexed twice. Please check that inclusion/exclusion patterns produce disjoint sets for main and test files Setting inclusion/exclusion patterns would require a lot of effort in trying matching all the cases. I'm talking about a Visual Studio solution with C# and NUnit for test runner, Javascript and React with Jest test runner. Any suggestion how to go about it? – Riccardo Merlin Feb 18 '22 at 10:53
1

If coverage is not shown for your js file then try to use absolute path of your lcov or try to use the lcov path as sonar.javascript.lcov.reportPaths=./coverage/jest/lcov.info Not sure if this help you or not but it worked for me.

Gampesh
  • 4,024
  • 2
  • 12
  • 9