๐Ÿ“ฆ Turbo87 / eslint-plugin-chai-expect

ESLint plugin that checks for common chai.js expect() mistakes

โ˜… 28 stars โ‘‚ 14 forks ๐Ÿ‘ 28 watching โš–๏ธ MIT License
๐Ÿ“ฅ Clone https://github.com/Turbo87/eslint-plugin-chai-expect.git
HTTPS git clone https://github.com/Turbo87/eslint-plugin-chai-expect.git
SSH git clone git@github.com:Turbo87/eslint-plugin-chai-expect.git
CLI gh repo clone Turbo87/eslint-plugin-chai-expect
renovate[bot] renovate[bot] Lock file maintenance (#542) cd70c6a 3 months ago ๐Ÿ“ History
๐Ÿ“‚ master View all commits โ†’
๐Ÿ“ .github
๐Ÿ“ lib
๐Ÿ“ tests
๐Ÿ“„ .editorconfig
๐Ÿ“„ .gitignore
๐Ÿ“„ .npmignore
๐Ÿ“„ CHANGELOG.md
๐Ÿ“„ eslint.config.mjs
๐Ÿ“„ index.js
๐Ÿ“„ LICENSE
๐Ÿ“„ package.json
๐Ÿ“„ README.md
๐Ÿ“„ yarn.lock
๐Ÿ“„ README.md

eslint-plugin-chai-expect

Build Status

ESLint plugin that checks for common chai.js expect() mistakes

[!IMPORTANT]
The recommended preset is for the ESLint legacy configuration system
(.eslintrc.json). The recommended-flat configuration is for the new flat
configuration system.

Requirements

  • Node.js 6 or above
  • ESLint 4.x or 5.x or 6.x

Installation

npm install --save-dev eslint-plugin-chai-expect

Configuration

Legacy ESLint Configuration Format (.eslintrc.json)

Add a plugins section and specify chai-expect as a plugin:

{
  "plugins": [
    "chai-expect"
  ]
}

Enable the rules that you would like to use:

{
  "rules": {
    "chai-expect/no-inner-compare": 2,
    "chai-expect/no-inner-literal": 2,
    "chai-expect/missing-assertion": 2,
    "chai-expect/terminating-properties": 2
  }
}

Or, if you just want the above defaults, you can avoid all of the above and just extend the config:

{
  "extends": ["plugin:chai-expect/recommended"]
}

Flat ESLint Configuration Format (eslint.config.js)

Add a plugins section and specify chai-expect as a plugin and enable the rules that you would like to use:

import chaiExpectPlugin from 'eslint-plugin-chai-expect';

export default [
  {
    "plugins": {
      "chai-expect": chaiExpectPlugin
    },
    "rules": {
      "chai-expect/no-inner-compare": 2,
      "chai-expect/no-inner-literal": 2,
      "chai-expect/missing-assertion": 2,
      "chai-expect/terminating-properties": 2
    }
  }
];

Or, if you just want the above defaults, you can avoid all of the above and just extend the config:

import chaiExpectPlugin from 'eslint-plugin-chai-expect';

export default [
  chaiExpectPlugin.configs["recommended-flat"],
  {
    // ...
  },
];

Rules

  • no-inner-compare - Prevent using comparisons in the expect() argument
  • no-inner-literal - Prevent using literals in the expect() argument
(undefined, null, NaN, (+|-)Infinity, this, booleans, numbers, strings, and BigInt or regex literals)
  • missing-assertion - Prevent calling expect(...) without an assertion
like .to.be.ok
  • terminating-properties - Prevent calling to.be.ok and other assertion
properties as functions

Additional configuration

terminating-properties rule

A number of extensions to chai add additional terminating properties. For example chai-http adds:

  • headers
  • html
  • ip
  • json
  • redirect
  • text
The terminating-properties rule can be configured to ensure these (or other) additional properties are not used as functions:

{
  "rules": {
    "chai-expect/terminating-properties": ["error", {
      "properties": ["headers", "html", "ip", "json", "redirect", "test"]
    }]
  }
}

License

eslint-plugin-chai-expect is licensed under the MIT License.