Home

Awesome

eslint-plugin-mocha-cleanup

CI Coverage Status Mutation testing badge npm version License Downloads

Installation

You'll first need to install ESLint:

$ npm i -D eslint

Next, install eslint-plugin-mocha-cleanup:

$ npm i -D eslint-plugin-mocha-cleanup

Note: If you installed ESLint globally (using the -g flag) then you must also install eslint-plugin-mocha-cleanup globally.

Supported Rules

Almost each rule (unless otherwise indicated) may be customized to ignore skipped tests/suites (describe.skip, it.skip, xspecify, etc.) by adding skipSkipped: true to the rule options. One can also set "settings": {"mocha-cleanup": {"skipSkipped": true}} to have skipping for all applicable rules by default.

"rules": {
    "mocha-cleanup/asserts-limit": [2, {"assertsLimit": 3}]
}

This rule ignores tests with a done-callback and 0 assertions. Set the option ignoreZeroAssertionsIfDoneExists to false to allow such behavior:

"rules": {
    "mocha-cleanup/asserts-limit": [2, {"ignoreZeroAssertionsIfDoneExists": false}]
}
"rules": {
    "mocha-cleanup/no-same-titles": [2, {"scope": "file"}]
}
"rules": {
    "mocha-cleanup/complexity-it": [2, {"maxAllowedComplexity": 30}]
}
"rules": {
    "mocha-cleanup/no-assertions-in-loop": [2, {"extraMemberExpression": ["forEach"]}]
}
"rules": {
    "mocha-cleanup/disallowed-usage": [
        2,
        {
            "test": [{"o": "myObject", "m": ["myNotAllowedMethod"]}],
            "hook": [{"f": "myNotAllowedFunction"}, {"o": "myObject", "p": ["myNotAllowedProperty"]}]
        }
    ]
}

Usage

Add to your eslint config-file:

"plugins": [
    "mocha-cleanup"
],
"rules": {
    "mocha-cleanup/asserts-limit": 2,
    "mocha-cleanup/disallow-stub-spy-restore-in-it": 2,
    "mocha-cleanup/no-empty-title": 2,
    "mocha-cleanup/no-same-titles": 2,
    "mocha-cleanup/no-nested-it": 2,
    "mocha-cleanup/no-assertions-outside-it": 2,
    "mocha-cleanup/complexity-it": 2,
    "mocha-cleanup/no-eql-primitives": 2,
    "mocha-cleanup/no-assertions-in-loop": 2,
    "mocha-cleanup/no-empty-body": 2,
    "mocha-cleanup/invalid-assertions": 2,
    "mocha-cleanup/no-expressions-in-assertions": 2,
    "mocha-cleanup/disallowed-usage": [
        2,
        {
            "test": [{"o": "myObject", "m": ["myNotAllowedMethod"]}],
            "hook": [{"f": "myNotAllowedFunction"}, {"o": "myObject", "p": ["myNotAllowedProperty"]}]
        }
    ],
    "mocha-cleanup/disallow-stub-window": [
        2,
        {
            "methods": ["setTimeout"]
        }
    ],
    "mocha-cleanup/no-outside-declaration": 2,
    "mocha-cleanup/top-level-assertions": 1
}

Or, if you want the items exactly as above (not including disallowed-usage, disallow-stub-window and top-level-assertions), just add this:

{
  "extends": ["plugin:mocha-cleanup/recommended"]
}

Or, if you want those items above, minus also the following limit-based rules:

...just add this:

{
  "extends": ["plugin:mocha-cleanup/recommended-no-limits"]
}