首页 > 解决方案 > 如何防止跳过的测试被检入源代码控制

问题描述

使用 jest 测试我们的 javascript / typescript 代码,在编写和调试时包装测试是相当普遍describe.onlyit.only

这将使 jest 跳过文件或块中的所有其他测试,但检查源代码控制是一个简单的更改。我已经做过很多次了。

如果跳过任何测试,导致 CI​​ 测试失败的最佳方法是什么?有短绒或开玩笑的选择吗?我一直找不到。

标签: javascripttypescriptcontinuous-integrationjestjs

解决方案


自从提出这个问题以来,我已经做了一些研究,并且我能够找到 lint 规则来处理这个问题。

对于 Javascript,有一个 eslint 插件:https ://www.npmjs.com/package/eslint-plugin-no-only-tests

对于 Typescript,tslint-microsoft-contrib repo 中有一个 tslint 插件:https ://github.com/Microsoft/tslint-microsoft-contrib称为mocha-avoid-only

我已经测试了打字稿规则,它适用于开玩笑测试。要设置它运行:npm install --save-dev tslint-microsoft-contrib然后将以下内容添加到您的 tslint 配置中。

"rules": {
    "mocha-avoid-only": true
},
"rulesDirectory": [
    "node_modules/tslint-microsoft-contrib"
],

推荐阅读