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
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 this Configuration 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.
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
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.
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/
.
Just found another option using Relative Glob Patterns:
In your .eslintrc
file:
overrides: [
{
files: "*.test.js",
rules: {
"no-unused-expressions": "off"
}
}
]
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
.