3

Im having an issue where my gitlab coverage badge reports back as "unknown" for a NodeJS project Im working. Ive read through dozens of stackoverflow articles about this and tried all the suggested solutions to no avail, so I figured I would put the specifics of my setup in this post to see if anyone can pinpoint what might be causing the issue.

The job I have in my gitlab-ci.yml file is as follows:

 test_coverage:
  image: node:16
  stage: test_coverage
  before_script:
    - echo "This skips any predefined before scripts to ensure clean environments"
  tags:
    - Nexus
  script:
    - npm run test:ci
  coverage: /All\sfiles.*?\s+(\d+.\d+)/
  artifacts:
    when: always
    reports:
        junit:
            - ./junit.xml
        cobertura: coverage/cobertura-coverage.xml
  dependencies:
    - install
  except:
    - tags

The script npm run test:ci in my package.json files is as follows:

"test:ci": "CI=true NODE_ENV=test ENVIRONMENT=CI_CD NODE_OPTIONS=--experimental-vm-modules jest --coverage --collectCoverage --coverageDirectory=\"./coverage\" --ci --reporter=html --reporter=text --reporter=cobertura --reporters=default --reporters=jest-junit --watchAll=false"

I've got the following regex in the test coverage reporting settings section of gitlab

/All\sfiles.*?\s+(\d+.\d+)/

I have the badge link and URL set up as well in gitlab.

When I run my npm run test:ci script locally, I get a lovely coverage report returned. When my job runs in the gitlab pipeline, my coverage report has 0 for every file. It tells me all my tests ran and passed. It tells me that my junit and cobertura artifacts were successfully completed, and that the job succeeded.

I have a strong feeling that if I were seeing actual coverage numbers in the gitlab job logs like I see when I run the script locally, the badge would work. But I have no ideas why I'm getting all 0s reported. Can anyone see what Im doing wrong based on the info provided here?

Netia Ingram
  • 71
  • 1
  • 3

1 Answers1

0

The test coverage badge on the GitLab project page displays the test coverage on the default branch.


Additionally, two things should be ensured:

  • Firstly, jest.config.js needs to include text-summary and cobertura as reporters.
// jest.config.js
  reporters: ['default', 'jest-junit'],
  coverageReporters: [
    'clover',
    'json',
    'lcov',
    'text',
    'text-summary',
    'cobertura',
  ],

...

Secondly, the cobertura report needs to be uploaded as an artifact to GitLab.

 artifacts:
    paths:
      - coverage/
    reports:
      junit:
        - junit.xml
      coverage_report:
        coverage_format: cobertura
        path: coverage/cobertura-coverage.xml

Lucas
  • 1
  • 1