How to run sonarqube analyzes in a repository with ReactJS and React-Native projects

Hello guys,
In my company we use SonarQube, we have one repository with a src folder where we have web, mobile and shared folders, we have one jest config to run web ReactJS tests and another jest config to run mobile React-Native tests. Currently, we use this gitlab CI task to run SonarQube:

test-and-sonar:
  stage: audit
  image: docker.tag/sonar-scanner:latest
  before_script:
    - npm install --save-dev jest-junit
  script:
    - apk add nodejs build-base gcc
    - npm install -D typescript
    - npx jest --ci --reporters=default --reporters=jest-junit --coverage
    - sonar-scanner ${SONAR_OPTS}
      -Dsonar.host.url=https://sonarqube.tag
      -Dsonar.jdbc.login="${SONARQUBE_TOKEN}"
      -Dsonar.token="${SONARQUBE_TOKEN}"
      -Dsonar.gitlab.project_id="${CI_PROJECT_PATH}"
      -Dsonar.gitlab.commit_sha="${CI_COMMIT_SHA}"
      -Dsonar.gitlab.ref_name="${CI_COMMIT_REF_NAME}"
      -Dsonar.gitlab.user_token="${SONAR_GITLAB_TOKEN}"
      -Dsonar.java.binaries=build/classes
      -Dsonar.javascript.lcov.reportPaths=coverage/lcov.info
      -Dsonar.sources=src,stories
      -Dsonar.exclusions=android/**,ios/**,**/*.java,**/*.jar
      -Dsonar.tests=__tests__
      -Dsonar.test.inclusions=shared/**/*.test.tsx,shared/**/*.test.ts,web/**/*.test.tsx,web/**/*.test.ts
  needs: []
  dependencies: []
  allow_failure: true
  artifacts:
    reports:
      junit:
        - junit.xml
  only:
    refs:
      - master
      - merge_requests

As we can see, we only run the web ReactJS tests:

    - npx jest --ci --reporters=default --reporters=jest-junit --coverage

Now, we want to run also the mobile React-Native tests and also get the code coverage of SonarQube.

Is it possible to get the SonarQube code analyzes for these 2 projects?

Thanks in advance for your attention.

Hi,

Welcome to the community!

The sonar.javascript.lcov.reportPaths property takes a comma-delimited list of report paths, so you should be good to go.

As a side note,

This analysis parameter hasn’t been used for years. This is a no-op. You can delete it.

I’m also not sure where you got those sonar.gitlab.* properties, and I’m pretty sure they’re not doing anything either.

 
HTH,
Ann

1 Like

Hello @ganncamp
I confirm your suggestion works perfectly.
I can run the jest tests for my web app and also the jest tests for my mobile app. I have also removed some unnecessary configurations.
Finally, it works, thanks so much.
I share my gitlab CI task to run SonarQube:

test-and-sonar:
  stage: audit
  image: docker.tag/sonar-scanner:latest
  before_script:
    - npm install --save-dev jest-junit
  script:
    - apk add nodejs build-base gcc
    - npm install -D typescript
    - npx jest --ci --reporters=default --reporters=jest-junit --coverage
    - npx jest -c jest-mobile.config.js --coverageDirectory='coverage-mobile' --ci --reporters=default --reporters=jest-junit --coverage
    - sonar-scanner ${SONAR_OPTS}
      -Dsonar.host.url=https://sonarqube.tag
      -Dsonar.token="${SONARQUBE_TOKEN}"
      -Dsonar.java.binaries=build/classes
      -Dsonar.javascript.lcov.reportPaths=coverage/lcov.info,coverage-mobile/lcov.info
      -Dsonar.sources=src,stories
      -Dsonar.exclusions=android/**,ios/**,**/*.java,**/*.jar
      -Dsonar.tests=__tests__
      -Dsonar.test.inclusions=shared/**/*.test.tsx,shared/**/*.test.ts,web/**/*.test.tsx,web/**/*.test.ts,mobile/**/*.test.tsx,mobile/**/*.test.ts
  needs: []
  dependencies: []
  allow_failure: true
  artifacts:
    reports:
      junit:
        - junit.xml
  only:
    refs:
      - master
      - merge_requests

Have a nice day!

1 Like