检测不需要的功能(fdescribe,describe.only)作为Gulp任务

Est*_*ask 14 mocha.js node.js jasmine gulp

如何在Node中检测到代码库中不需要的函数的使用,尤其是Gulp?

我检查无意中被宠坏的规格之后,即是ddescribe/ fdescribeiit/ fit茉莉或.only.skip对摩卡:

// should be reported
fdescribe(function () {
  // should not be reported
  it(function () {
    var fit = ...;
    this.fit = ...;
  });

  // should not be reported
  // fit(function () { ... });

  //  should be reported
  xit(function () { ... });

  //  should be reported
  fit(function () { ... });
});
Run Code Online (Sandbox Code Playgroud)
// should be reported
describe.only(function () {
  // should not be reported
  it(function () { ... });

  // should not be reported
  // it.only(function () { ... });

  //  should be reported
  it.skip(function () { ... });

  //  should be reported
  it.only(function () { ... });
});
Run Code Online (Sandbox Code Playgroud)

任务应该以错误退出,并输出使用列出的函数的文件名和行号.

肯定不需要检测注释的那些,以及具有相同名称的函数/属性(最有可能fit),因此这里的简单正则表达式匹配不是一个选项(就像它一样console.*).一些基于AST的解决方案接受用户定义的函数名称将不胜感激.

ale*_*cxe 8

我会通过ESLintjavascript linting实用程序在静态分析步骤中解决它.为了捕获代码库中意外遗留的独占/专注的mocha规范,插件中实现了一条no-exclusive-tests规则:eslint-plugin-mocha

Mocha有一个功能,允许您通过附加.only到测试套件或测试用例来独家运行测试.此功能对调试失败测试非常有用,因此您无需执行所有测试.修复测试之后,在提交更改之前,必须删除.only以确保在构建系统上执行所有测试.

此规则提醒您.only在使用排他性功能时通过发出警告来从测试中删除.

如果你想将eslint运行绑定到gulp- 使用gulp-eslint插件.


在git hook中提交之前运行gulp eslint任务可能也是个好主意.我们已经使用该软件包来安装和跟踪git钩子.pre-git

这样,重点或排他性测试就不会首先进入代码库.