1

We have jest unit testing for our react app and need to set a threshold value of 80% test case coverage. I know that we can get the coverage report in npm test -- --coverage --watchAll=false but I am now tasked with failing the pipeline if the coverage goes below 80%. I saw that there is a test pipeline stage which is commented right now. I have the following script, I need to somehow get the coverage, and compare if it is 80 or more else fail the pipleline, what should I do

test:
  stage: test
  image: node:16.13.1
  before_script:
    - npm i
    - npx node -v
    - npx npm -v
  script:
    - echo "running test coverage"
    - npm test -- --coverage --watchAll=false
  coverage: '/All files[^|]*\|[^|]*\s+([\d\.]+)/'
leo
  • 59
  • 7

1 Answers1

1

Within your script section you should be able to use a Regex expression to retrieve the coverage value from the report.

Then you can use some bash scripting to remove the % sign from the output and compare the value with your required minimum. If the value is below, simply return false which I think gitlab should interpret as a failure.

If it’s above your minimum value return true and it should pass the job.

quizguy
  • 161
  • 9
  • I saw gitlab uses something called coverage for getting the regex form but it does not allow how to extract just that value from the npm test -- --coverage --watchAll=false . Not sure how to proceed further. – leo Jun 19 '22 at 00:44
  • You can use something like sed as a separate command within your script: section to grab the same value and use it how I described above. The gitlab coverage keyword will consume the value and use it within some of your pipeline metrics. – quizguy Jun 19 '22 at 11:28
  • Not sure if you made it work but this should help https://stackoverflow.com/questions/70836517/how-can-i-set-a-minimum-unit-test-coverage-with-gitlab-ci – Alec Gerona Jul 07 '22 at 06:23