Code Coverage Report - DerekJi/ngtetris GitHub Wiki

Generate Code Coverage Report

Run the command ng test --browsers=ChromeHeadless --watch=false --code-coverage or, npm run test:prod, you'll find the coverage files from the folder './coverage/'

'test:prod' is a custom command built in package.json.

Support lcov

The default formats of coverage report are HTML or text-summary, which is defined in karma.conf.js.

    coverageReporter: {
      dir: require('path').join(__dirname, './coverage/ngtetris'),
      subdir: '.',
      reporters: [
        { type: 'html' },
        { type: 'text-summary' }
      ]
    },

Instead, lcov is the option for CodeCov. So we need to add it.

    coverageReporter: {
      dir: require('path').join(__dirname, './coverage/ngtetris'),
      subdir: '.',
      reporters: [
        { type: 'html' },
        { type: 'text-summary' },
        { type: 'lcovonly' },
      ]
    },

With this new option, you'll find a new file lcov.info from the folder ./coverage

Pipeline

Add a new step into the pipeline yaml file, so that coverage report could be uploaded to CodeCov

    - name: Upload coverage reports to Codecov
      run: |
        # Replace `linux` below with the appropriate OS
        # Options are `alpine`, `linux`, `macos`, `windows`
        curl -Os https://uploader.codecov.io/latest/linux/codecov
        chmod +x codecov
        ./codecov -t ${CODECOV_TOKEN}

or, use the official Codecov github action from the marketplace

    - name: Upload coverage to Codecov
      uses: codecov/codecov-action@v1
      with:
        token: ${{ secrets.YOUR_SECRET_TOKEN }}

As this is a public repository, token is not necessary.

CodeCov Setup

  1. Sign up using Github account: https://app.codecov.io/
  2. Follow the instructions and select the github repository you're working on.
  3. Copy and use the token from the settings tab of the CodeCov repository
  4. Copy the CodeCov badge to README.md image

References