Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I automatically check for '.only' calls accidentally left in Mocha specs?

I occasionally forget to remove .only calls from my Mocha specs before pushing spec changes. Doing so obviously affects test coverage, which requires addressing the failure(s). I'd like to catch these before I push changes, ideally as part of the linting process with ESLint and with minimal effort.

like image 530
user3006381 Avatar asked Sep 06 '25 03:09

user3006381


2 Answers

You're looking for the mocha/no-exclusive-tests rule, part of the eslint-plugin-mocha plugin. It fails if it finds any describe.only or it.only. Very useful!

From the docs:

This plugin requires ESLint 4.0.0 or later.

npm install --save-dev eslint-plugin-mocha

Then add a reference to this plugin and selected rules in your eslint config:

{
  "plugins": [
    "mocha"
  ],
  "rules": {
    "mocha/no-exclusive-tests": "error"
  }
}

There is also a mocha/no-skipped-tests if you want to prevent it.skip or xit from being committed too. However, I find that .skip is sometimes valid, so I find it best to just prevent .only.

That plugin also has a ton of other useful checks, so be sure to read their docs!

like image 158
Scott Rippey Avatar answered Sep 07 '25 19:09

Scott Rippey


You have a --forbid-only parameter to be passed when executing mocha which will make your tests fail.

You have also the --forbid-pending.

Here from the official doc:

--forbid-only causes test marked with only to fail the suite

--forbid-pending causes pending tests and test marked with skip to fail the suite

like image 22
quirimmo Avatar answered Sep 07 '25 20:09

quirimmo