node.js - 将不需要的功能(fdescribe、describe.only)检测为 Gulp 任务

标签 node.js jasmine gulp mocha.js

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

我正在检查无意破坏的规范,即 ddescribe/fdescribeiit/fit Jasmine 或 .only.skip 用于 Mocha:

// 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 () { ... });
});

// 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 () { ... });
});

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

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

最佳答案

我将通过 ESLint 在静态分析步骤中解决它javascript linting 实用程序。为了捕捉代码库中不小心留下的独家/重点 Mocha 规范,有一个 no-exclusive-tests ruleeslint-plugin-mocha plugin 中实现:

Mocha has a feature that allows you to run tests exclusively by appending .only to a test-suite or a test-case. This feature is really helpful to debug a failing test, so you don’t have to execute all of your tests. After you have fixed your test and before committing the changes you have to remove .only to ensure all tests are executed on your build system.

This rule reminds you to remove .only from your tests by raising a warning whenever you are using the exclusivity feature.

如果你想将 eslint 运行绑定(bind)到 gulp - 使用 gulp-eslint插件。


在 git 钩子(Hook)中运行 gulp eslint 任务 before 也是一个好主意。我们使用了pre-git用于安装和跟踪 git hooks 的包。

这样,重点测试或排他测试一开始就不会进入代码库。

关于node.js - 将不需要的功能(fdescribe、describe.only)检测为 Gulp 任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35485625/

相关文章:

javascript - Gulp.js - 在连接时重写嵌入在 css 中的 url

javascript - Electron 没有在应用程序中完全更新已编辑的 index.html

node.js - process.cwd() 与 __dirname 有什么区别?

unit-testing - 使用 angular.js 加载用于 jasmine 测试的 json 文件

angularjs - 如何测试使用 async/await 的方法?

node.js - Gulp - 将多个文件复制到单独的路径

javascript - 通过 puppeteer 单击 "load more"按钮

node.js - 使用 WebdriverJS 选择父元素

jquery - 无法监视 Jasmine 单元测试中 ajax 的交错成功回调

mysql - 如何在 gulpfile 中将 mysqldump 与 --hex-blob 一起使用?