Coverage Cypress tests

Hello,

I’m trying to setup Sonar for an Angular app. Here is a part of my bitbucket-pipelines.yml:

image: bitbucket-pipeline:3.2

clone:
  depth: full              # SonarCloud scanner needs the full history to assign issues properly

definitions:
  caches:
    sonar: ~/.sonar/cache  # Caching SonarCloud artifacts will speed up your build
  services:
    docker:
      memory: 6144
  steps:
    - step: &build-test-sonarcloud
        size: 2x
        name: Build, test and analyze on SonarCloud
        services:
          - docker
        script:
          - npm install --quiet
          - npm run test -- --code-coverage --no-watch --no-progress -browsers=ChromeHeadlessNoSandbox  
          - pipe: sonarsource/sonarcloud-scan:1.4.0
            variables:
              SONAR_TOKEN: ${SONAR_TOKEN}
              EXTRA_ARGS: '-Dsonar.sources=src
                -Dsonar.tests=src/app
                -Dsonar.test.inclusions="**/testing/**,**/*.spec.ts"
                -Dsonar.coverage.exclusions="**/node_modules/**,**/*.spec.ts"
                -Dsonar.typescript.lcov.reportPaths=coverage/lcov.info'
          # Use the following pipe if we want the pipeline to fail when quality gate fails
          # - pipe: sonarsource/sonarcloud-quality-gate:0.1.6
  • I’m using unit tests (Jasmine/Karma) and e2e tests (Cypress, folder /cypress). Is it possible to scan Cypress tests too ?
  • The coverage is all the time 0%. While I don’t have a lot of tests yet, it sounds like the coverage isn’t working fine. Am I missing something ?
  • Do I need to include pipe sonarsource/sonarcloud-quality-gate:0.1.6 ? I want the code to be analyze but I don’t want the pipeline to fail when the quality gate fails.

Thank’s a lot.

Hey there.

SonarCloud relies completely on the coverage reports you specify to understand what code has been covered by tests. Is there actually coverage from your tests in the coverage/lcov.info you’ve passed to sonar.typescript.lcov.reportPaths?

You might find it useful to run a cat coverage/lcov.info in your pipeline prior to running the analysis.

There will also be logs from the sonarsource/sonarcloud-scan pipe that describe what’s happening during coverage import.

For example:

INFO: Sensor JavaScript/TypeScript Coverage [javascript]
INFO: Analysing [/home/vsts/work/1/s/test-results/coverage/lcov.info]
INFO: Sensor JavaScript/TypeScript Coverage [javascript] (done) | time=48ms

You only need to use this if you want to fail the pipeline on the Quality Gate.

Hi Colin,

I don’t have such file coverage/lcov.info. The generated coverage folder reports looks like the following:

image

Here is my karm.conf.js:

// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html
process.env.CHROME_BIN = require('puppeteer').executablePath();
module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', '@angular-devkit/build-angular'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage-istanbul-reporter'),
      require('karma-coverage'),
      require('@angular-devkit/build-angular/plugins/karma')
    ],
    client: {
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    coverageIstanbulReporter: {
      reports: ['html', 'lcovonly', 'text-summary'],
      fixWebpackSourcePaths: true
    },
    angularCli: {
      environment: 'dev'
    },
    reporters: ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['ChromeHeadlessNoSandbox'],
    singleRun: false,
    customLaunchers: {
      ChromeHeadlessNoSandbox: {
        base: "ChromeHeadless",
        flags: ["--no-sandbox"]
      }
    },
    check: {
      global: {
        statements: 80,
        branches: 80,
        functions: 80,
        lines: 80
      }
    }
  });
};

Well you… specified it in your analysis configuration.

SonarCloud only reads coverage reports, it isn’t involved in their generation. I would suggest going back to your coverage tool and making sure an lcov.info file is generated, so that you can properly pass it to the right test coverage parameter

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.