最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin11浏览0评论

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.

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.

Share Improve this question asked Jan 3, 2019 at 1:24 user3006381user3006381 2,8953 gold badges25 silver badges33 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

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 mitted 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

Another package doing just this is eslint-plugin-no-only-tests.

Alternatively, this can now be done by following built-in ESLint rules, but they will highlight the whole function body of the callback as error as well:

"no-restricted-syntax": [
  "error",
  {
    "selector": "CallExpression[callee.object.type='Identifier'][callee.object.name='it'][callee.property.type='Identifier'][callee.property.name='only']",
    "message": "Do not mit it.only. Use it instead."
  },
  {
    "selector": "CallExpression[callee.object.type='Identifier'][callee.object.name='describe'][callee.property.type='Identifier'][callee.property.name='only']",
    "message": "Do not mit describe.only. Use describe instead."
  }
]

For an exclusion of fdescribe and fit the following can be used:

"no-restricted-globals": [
  "error",
  {
    "name": "fdescribe",
    "message": "Do not mit fdescribe. Use describe instead."
  },
  {
    "name": "fit",
    "message": "Do not mit fit. Use it instead."
  },
]

My solution was to use a simple bash script with the grep mand, added to the package.json scripts section like so:

"scripts": {
  "lint:only": "RESULT=\"$(grep -rHn '[.]only\\|[.]skip' spec/ --color=always)\"; if [ -n \"$RESULT\" ]; then printf \"Oops! You left a few things in your specs! \n\n${RESULT}\n\n\"; fi",
  "lint": "eslint --ext js src config scripts bin config server; yarn lint:only"
  ...
}

In a nutshell, this checks the spec/ directory for anything matching .only or .skip and outputs the filename + line if anything offending is found. Color is added for clarity and the script is run as part of linting with ESLint and Yarn.

发布评论

评论列表(0)

  1. 暂无评论