Vitest测试覆盖率未达标却未触发失败,求解决方法
解决Vitest覆盖率未达标但测试仍通过的问题
要让Vitest在覆盖率未达阈值时直接执行失败,只需在你的coverage配置中添加强制校验项即可,有两种写法可选:
写法一:直接在coverage下添加enforce配置
export default defineConfig({ plugins: [vue()], test: { environment: "happy-dom", exclude: [...configDefaults.exclude, "**/tests/e2e/*"], coverage: { reporter: ['text', 'json', 'html'], // 开启强制校验,未达阈值则测试失败 enforce: true, lines: 80, functions: 80, branches: 80, statements: 80, } }, })
写法二:将阈值嵌套到thresholds对象中(更规范)
export default defineConfig({ plugins: [vue()], test: { environment: "happy-dom", exclude: [...configDefaults.exclude, "**/tests/e2e/*"], coverage: { reporter: ['text', 'json', 'html'], thresholds: { // 开启强制校验 enforce: true, lines: 80, functions: 80, branches: 80, statements: 80, } } }, })
原理说明
默认情况下,Vitest仅会输出覆盖率未达标的警告信息,但不会终止测试进程或标记任务失败。开启enforce: true后,一旦任意一项覆盖率未达到设定阈值,Vitest会直接让整个测试任务以失败状态退出,返回非0的退出码,完全符合你想要的效果。
内容的提问来源于stack exchange,提问作者Tambua Rex
相关产品推荐
相关产品推荐

