npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

eslint-plugin-mocha-cleanup

v1.11.3

Published

Check mocha tests for empty titles and equal titles, huge number of assertions in the one test-block, invalid sinon usage etc. TDD and BDD syntaxes are supported

Downloads

12,058

Readme

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.

  • asserts-limit Rule to disallow use of more than an allowed number of assertions. Tests without any assertions are also disallowed. Rule may be customized with setting the maximum number of allowed asserts (Defaults to 3):
"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}]
}
  • disallow-stub-spy-restore-in-it Rule to disallow stub/spy/restore in tests (they should instead be in hooks)

  • no-empty-title Rule to disallow empty titles in suites and tests

  • no-same-titles Rule to disallow identical titles for tests inside of one suite or within the whole file. It depends on the scope value - may be file or suite. Defaults to suite

"rules": {
    "mocha-cleanup/no-same-titles": [2, {"scope": "file"}]
}
  • no-nested-it Rule to disallow nested tests (not suites)

  • no-assertions-outside-it Rule to disallow assertions outside tests

  • complexity-it Counts test-body complexity. May be customized with setting maximum complexity (Defaults to 40):

"rules": {
    "mocha-cleanup/complexity-it": [2, {"maxAllowedComplexity": 30}]
}
  • no-eql-primitives Rule to disallow eql, deep.equal, assert.deepEqual, assert.notDeepEqual with primitives

  • no-assertions-in-loop Rule to disallow assertions inside loops. Rule may be customized with setting additional loops like forEach:

"rules": {
    "mocha-cleanup/no-assertions-in-loop": [2, {"extraMemberExpression": ["forEach"]}]
}
  • no-empty-body Rule to disallow empty tests, suites and hooks

  • invalid-assertions Rule to check expect and should assertions for completeness. It detects assertions that end with "chainable" words or even raw calls for expect and should

  • no-expressions-in-assertions Rule to detect expressions in expect and assert assertions

  • disallowed-usage Rule to disallow usage of some functions, methods or properties in the tests and hooks

"rules": {
    "mocha-cleanup/disallowed-usage": [
        2,
        {
            "test": [{"o": "myObject", "m": ["myNotAllowedMethod"]}],
            "hook": [{"f": "myNotAllowedFunction"}, {"o": "myObject", "p": ["myNotAllowedProperty"]}]
        }
    ]
}
  • disallow-stub-window Rule to disallow stubbing some window-methods. IMPORTANT This rule doesn't have a skipSkipped option

  • no-outside-declaration Rule to disallow variable declarations outside tests and hooks

  • top-level-assertions Rule to check if some assertions are not on top of the test-block. Rule is a "next-level" for no-assertions-in-loop.

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:

  • asserts-limit
  • complexity-it

...just add this:

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