Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SonarQube with Jest Unit Tests

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.

like image 296
Matt B. Avatar asked Dec 20 '17 20:12

Matt B.


People also ask

Does SonarQube run unit tests?

SonarQube doesn't run your tests or generate reports. It only imports pre-generated reports. Below you'll find language- and tool-specific analysis parameters for importing coverage and execution reports. In the Guides category of the SonarSource Community forum you might find instructions on generating these reports.

How do I add unit test coverage in Sonar?

To add coverage to your Maven project you need to use the jacoco-maven-plugin and its report goal to create a code coverage report. Typically, you would create a specific Maven profile for executing the unit tests with instrumentation and producing the coverage report only on demand.

Can jest be used for unit testing?

Jest JavaScript resting framework with a focus on simplicity. Jest was created by Facebook engineers for its React project. Unit testing is a software testing where individual units (components) of a software are tested. The purpose of unit testing is to validate that each unit of the software performs as designed.

What is not covered by tests in SonarQube?

In windows 10 Local machine, eclipse maven project test cases are working but in build server new lines of code is not covered by sonarqube.


1 Answers

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

like image 102
Swashata Ghosh Avatar answered Sep 28 '22 00:09

Swashata Ghosh