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.
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-mochaThen 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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With