[英]Prevent commit if below jest test coverage threshold using husky hook
[英]How to start a gitlab pipeline job that fails when test coverage goes below a threshold
我们对我们的 React 应用程序进行了开玩笑的单元测试,需要设置 80% 测试用例覆盖率的阈值。 我知道我们可以在npm test -- --coverage --watchAll=false
中获得覆盖率报告,但如果覆盖率低于 80%,我现在的任务是让管道失败。 我看到现在有一个评论的测试管道阶段。 我有以下脚本,我需要以某种方式获得覆盖率,并比较它是否为 80 或更多,否则流水线失败,我该怎么办
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\.]+)/'
在您的脚本部分中,您应该能够使用正则表达式从报告中检索覆盖率值。
然后,您可以使用一些 bash 脚本从 output 中删除 % 符号,并将该值与所需的最小值进行比较。 如果该值低于此值,只需返回 false,我认为 gitlab 应解释为失败。
如果它高于您的最小值,则返回 true 并且它应该通过作业。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.