reactjsreact-nativejestjssonarqube

How to run SonarQube analyses in a repository with React and React Native projects?


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 React 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 React 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 analyses for these 2 projects?


Solution

  • Finally, 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. 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!