Nice way to get rid of no-unused-expressions linter error with chai

eslint-disable-next-line
eslint describe' is not defined
eslint warning instead of error
eslint mocha
tslint unused method
eslint-disable file
eslint plugins
eslint comments

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?

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:

  1. 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"
        }
    }
    
  2. 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.

Nice way to get rid of no-unused-expressions linter error with chai, There are a couple of solutions/approaches you can take. You could just disable the rule for the entire file using eslint-disable at the top of the file in question: /*eslint no-unused-expressions: "error"*/ {} // In this context, this is a block statement, not an object literal {myLabel: someVar} // In this context, this is a block statement with a label and expression, not an object literal function namedFunctionDeclaration {} (function aGenuineIIFE {}()); f() a = 0 new C delete a.b void a

Just found another option using Relative Glob Patterns:

In your .eslintrc file:

overrides: [
    {
        files: "*.test.js",
        rules: {
            "no-unused-expressions": "off"
        }
    }
]

Handle 'no-unused-expressions' violations due to use of Chai , Using a Chai expect expression in a test with the StandardJS ESLint config @​jennifer-shehane Just tested this out. Because of the way ESLint plugins work, we can't bundle a plugin if they're not using the no-unused-expressions rule), they'll get an error You signed in with another tab or window. This rule aims to eliminate unused expressions which have no effect on the state of the program. This rule does not apply to function calls or constructor calls with the new operator, because they could have side effects on the state of the program.

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.

Add option for no-unused-expressions · Issue #2102 · eslint/eslint , I'm using Chai's expect syntax for testing, and I'm getting no-unused-expression errors. *.spec.js (apologies if this is possible already, i could not work out how). Improve eslint config, lint … [Proposal] no-unused-expressions: 0 (best-​practices.js) #5 ignoring rule because it doesn't play nice with chai Viewing the Chai expect / should documentation, there are several ways to do this test. Note that you can chain using "and" but apparently not "or" - wish they had this functionality. Check whether an object passes a truth test:.satisfy(method)

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.

eslint-plugin-chai-friendly, This plugin makes 'no-unused-expressions' rule friendly towards chai expect Get unlimited public & private packages + package-based  By design, they cannot change the behavior of rules like no-unused-expressions, so it wouldn't be possible to use an environment in the way that you're suggesting. Additionally, while ESLint used to add environments for custom libraries, it no longer does this.

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

eslint/eslint, In this kind of test i get the error on expect saying the rule is no-unused-​expressions rather than doing a method call or other expression with obvious side-effects. look at https://github.com/ihordiachenko/eslint-plugin-chai-friendly; perhaps it VSCode always need to re-open the file in order to get rid of the eslint error? How does the expect().to.be.true work in Chai? How to test react-router with enzyme ; How to unit test a method of react component?

Package, And yes..having a rule specifically for PascalCase would be nice. I've been wondering if we should get rid of eslint.org/parser in favor of ASTExplorer, I've found running npm run lint lints the files, running npm run lint:err throws the error above :/ "chai-friendly/no-unused-expressions": 2, How should I fix this error? We make Stack Overflow and 170+ other community-powered Q&A sites.

Change Log, Run npm install chai --save-dev and npm install chai-http --save dev . Note that this is not possible with the real Spotify API - you'd have to go through the ts-lint - Make sure Typescript is formatted correctly for good readability and code that is less error Add /* tslint:disable:no-unused-expression */ to the top of test files. jonalvarezz. Senior Frontend Developer at TEAM International Services Inc. chai. Score 26. Posts 1. html5-video. Score 14. Posts 1. google-chrome. 26 Nice way

Relax rule (temporarily, to workaround a bug): Disallow missing parentheses of the ECMAScript specification. eslint-config-standard/e04e06; Lint *.mjs and *.cjs Remove bundle.js from the default list of ignored files #743 lintTextSync method Allow tagged template string expressions (no-unused-expressions) #​822  Disallow Unused Expressions (no-unused-expressions) Unused expressions are those expressions that evaluate to a value but are never used. For example:

Comments
  • That's a really cool feature (the Configuration Cascading) I didn't know about eslint thanks! Not the perfect solution to what I'd like, as I'd love for it to only care about those specific use cases, but suffices for what I'm trying to do.
  • Why is this answer about no-underscore-dangle? Can you explain what to put in .eslintrc to answer op's original question?
  • +1. This should be the accepted answer because it means you don't have to turn off the rule for the entire test file (so you still get the benefits of the rule when it applies). Thanks for the plugin!
  • If you're writing typescript, there's now an equivalent plugin on npm for tslint: tslint-no-unused-expression-chai.
  • Great idea. Shouldn't the second rule in your code sample be enabled though? i. e. 'chai-friendly/no-unused-expressions': 2
  • You are correct. I looked back at my code and I have it as chai-friendly/no-unused-expressions': 'error'. Feel free to submit an edit, or if not I'll do it in a few days.
  • That's the right thing to do. In my experience the Chai assertion would still fail though as Chai and Mocha are two different things so you want to take care of it separately. Scott's answer above seems to be the most efficient fix for Chai.
  • This does not answer the question, which is about Chai's should() and assert methods.