Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect unwanted functions (fdescribe, describe.only) as Gulp task

How can the usage of unwanted functions in code base be detected in Node, particularly Gulp?

I'm after checking for unintentionally spoiled specs, i.e ddescribe/fdescribe and iit/fit for Jasmine or .only and .skip for 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 () { ... });
});

The task should exit with error and output file names and line numbers where listed functions are used.

The commented ones surely don't have to be detected, as well as functions/properties with the same name (most likely fit), so simple regexp match is not an option here (like it would be for console.*). Some AST-based solution that accepts user-defined function names would be appreciated.

like image 243
Estus Flask Avatar asked Feb 18 '16 15:02

Estus Flask


1 Answers

I would solve it on a static analysis step via ESLint javascript linting utility. To catch the exclusive/focused mocha specs accidentally left in the codebase, there is a no-exclusive-tests rule implemented in the eslint-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.

If you want to tie the eslint run to gulp - use the gulp-eslint plugin.


It might also be a good idea to run the gulp eslint task before commit in a git hook. We've used the pre-git package to install and keep track of the git hooks.

This way the focused or exclusive tests would not get into the codebase in the first place.

like image 148
alecxe Avatar answered Nov 07 '22 21:11

alecxe