node.jsgitlabgitlab-cicode-coverage

Gitlab test coverage visualization is not working


Hi guys I have a express application and I'm playing around with gitlab to add Gitlab test coverage visualization

Here is my .gitlab-ci.yml

stages:
  - test
  - dockerize
  - staging
  - production

unit-tests:
  stage: test
  script:
    - npm install
    - npm run test
    - npm run test-coverage
    - cat coverage/cobertura-coverage.xml
    - "echo 'Code coverage: 90.90'" 
  coverage: '/Code coverage: \d+\.\d+/'
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
    - if: '$CI_COMMIT_BRANCH == "master"'
    - if: '$CI_COMMIT_BRANCH == "release-v1"'
  artifacts:
    reports:
      cobertura: coverage/cobertura-coverage.xml
  tags:
   - demo

dockerize-application:
  stage: dockerize
  script:
    - echo "dockerizing application"
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
    - if: '$CI_COMMIT_BRANCH == "master"'
    - if: '$CI_COMMIT_BRANCH == "release-v1"'
  tags:
   - demo


deploy_to_staging:
  stage: staging
  script:
    - echo "deploying to staging"
  rules:
    - if: '$CI_COMMIT_BRANCH == "master"'
  tags:
   - demo

deploy_to_production:
  stage: staging
  script:
    - echo "deploying to production"
  rules:
    - if: '$CI_COMMIT_BRANCH == "release-v1"'
  tags:
   - demo

In my package json, here is the important part:

"scripts": {
    "start": "node ./bin/www",
    "debug": "nodemon ./bin/www",
    "test": "npx nyc --reporter text mocha",
    "test-coverage": "npx nyc --reporter cobertura mocha"
  }

In the logs, I can see that the coverage file is uploaded.

Runtime platform                                    arch=amd64 os=windows pid=19548 revision=775dd39d version=13.8.0
coverage/cobertura-coverage.xml: found 1 matching files and directories 
Uploading artifacts as "cobertura" to coordinator... ok  id=1097622443 responseStatus=201 Created token=6wcrN_d_

I did a lot of research and I still can't figure out why! I figured that was a feature flag turning this feature off by default but not any more:

https://gitlab.com/gitlab-org/gitlab/-/merge_requests/43711

Here is the link to my project: I mainly created to experiment with gitlab ci/cd


Solution

  • It's now working. The issue was that I needed to wait for the WHOLE pipeline to succeed before I could actually see the visualization.

    Here is an issue open in Gitlab:

    https://gitlab.com/gitlab-org/gitlab/-/issues/236248