Nice way to get rid of no-unused-expressions linter error with chai
ChaiEslintChai Problem Overview
In my Chai tests I often find myself wanting to use their assertions that are something like .to.be.empty
, .to.be.true
e.t.c., because I find them to be cleaner to read than .to.be.length(1)
or .to.be.equal(true)
. However, this breaks my linter (I'm using default Airbnb linting).
I could use the // disable-eslint-line
syntax, but then I'd have to add it to every single line that reads like that and that seems tedious.
I've also read about the DirtyChai library, but that would require me to go back through my entire testing library adding brackets to them all which seems like something I shouldn't have to do simply to get my linter to pass something it should probably be OK with in the first place.
Does anyone know a nicer way to handle this than the ways I've outlined above?
Chai Solutions
Solution 1 - Chai
You can disable the rule for the entire file using eslint-disable
at the top of the file in question:
/* eslint-disable no-unused-expressions */
expect(someTrueValue).to.be.true;
However, adding this at the top of every test file can be tedious. To disable this rule for all relevant files, you can:
-
Put a new
.eslintc
configuration file in the same directory as your test files, configured to disable that rule. This allows you to use the default configuration for all other rules while ignoring that rule specifically only on files in that folder. ESLint calls thisConfiguration Cascading
.{ "rules": { "no-unused-expressions": "off" } }
-
Use the
overrides
key in your main.eslintrc
file to disable rules for groups of files with glob pattern matching:{ "overrides": [ { "files": ["*.test.js", "*.spec.js"], "rules": { "no-unused-expressions": "off" } } ] }
This also allows you to disable other rules which become troublesome in testing, such as no-underscore-dangle
when using rewire
.
Solution 2 - Chai
I've made a small plugin called eslint-plugin-chai-friendly
that overrides the default no-unused-expressions
rule and makes it friendly towards chai. The modified rule ignores the expect
and should
statements while keeping default behavior for everything else.
Solution 3 - Chai
Just found another option using Relative Glob Patterns:
In your .eslintrc
file:
overrides: [
{
files: "*.test.js",
rules: {
"no-unused-expressions": "off"
}
}
]
Solution 4 - Chai
Combining jonalvarezz's answer with Ihor Diachenko's answer gave me exactly what I wanted:
npm install --save-dev eslint-plugin-chai-friendly
// .eslintrc.js
module.exports = {
// ...
plugins: ['chai-friendly'],
overrides: [{
files: '*.test.js',
rules: {
'no-unused-expressions': 'off',
'chai-friendly/no-unused-expressions': 'error',
},
}],
// ...
}
This way, the no-unused-expression
rule will only be overridden in *.test.js
files
AND
a no-unused-expression
rule will still be in place to catch any unused expressions in the test files that are unrelated to chai
.
Solution 5 - Chai
In case anyone is stumbling upon this today, I had the same issue and found this solution on eslint documentation. In your eslint configuration file, you can specify one or several environments, which will predefine global variables for this environment. For us, it'd be mocha
, and you'd configure like this in your .eslintrc.json
:
{
"env": {
"mocha": true
},
...
...
...
}
As a result, it will remove all false positive about mocha describe
, it
, beforeEach
, etc. without needing to completely disable eslint or completely disable any specific rule.
Tested with ESLint v.4.11 and mocha 5.0
Solution 6 - Chai
I had this issue with tslint and solved it by simply moving the rule for unused expressions down one level. My ./tslint.json
has all the other rules I care about, then I made ./src/tslint.json
that just looks like
{
"rules": {
"no-unused-expression": true
},
"extends": "../tslint.json"
}
tslint automatically checks for a config file in every level as it descends the tree (with --project
or using the VSCode extension) so this means that my tests (under ./test/
) have all the other rules applied, but no-unused-expression
only applies to files under ./src/
.