question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add option for no-unused-expressions

See original GitHub issue

I’m using Chai’s expect syntax for testing, and I’m getting no-unused-expression errors. I understand that this is because expressions like expect(...).to.be.undefined look like property access, even though chai’s actually using the property’s getter to call a method behind the scenes that causes some side effects.

I did a bit of googling and at least a few other people have encountered this.

I’m not sure what the best solution is (I don’t know enough about parsing JS to say) but it seems like one possibility might be an option that would not flag property access chains that start with certain identifiers. So the user could specify "expect" as the option’s value and then all these chai calls would be let through.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:27
  • Comments:24 (10 by maintainers)

github_iconTop GitHub Comments

32reactions
AMorgautcommented, Dec 1, 2016

@nzakas with all respect, you said:

Sorry all, we still don’t have a way of resolving this without baking-in knowledge of Chai. As a general principle, we do not put library-specific code in ESLint.

But actually, ESLint, as JSHint, does support “environment options” such as jasmine, mocha, and qunit, plus protractor, atomtest, and embertest on ESLint side… so I think that adding a chai environment option would not break existing ESLint library approach.

I just wrote a related feature request to the JSHint project

As I described in that feature request, another non ‘library related’ approach could be to add a expr: 'accessor' sub-option to not raise the warning when we ends an expression with the access to an accessor property(cf ES5 Property Attributes) . I mentioned related use cases like forced singleton instantiation.

This library agnostic option can be interesting but:

  • it would probably be more complicated to support than just supporting the chai use case
  • it could hide expected warnings if we only want it on the chai API (such sub option is probably more legitimate to be activated at line level than at file level)
14reactions
niftylettucecommented, Nov 27, 2015

👍 for dirty-chai

npm install --save-dev dirty-chai
var chai = require('chai');
+// Load dirty chai first to hook plugin extensions
+var dirtyChai = require('dirty-chai');
+chai.use(dirtyChai);
Read more comments on GitHub >

github_iconTop Results From Across the Web

no-unused-expressions - ESLint - Pluggable JavaScript Linter
This rule aims to eliminate unused expressions which have no effect on the state of the program. This rule does not apply to...
Read more >
Nice way to get rid of no-unused-expressions linter error with ...
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 ...
Read more >
no-unused-expressions | typescript-eslint
This rule extends the base eslint/no-unused-expressions rule. It adds support for optional call expressions x?.() , and directive in module declarations.
Read more >
Solved: Optional Chaining no-unused-expressions ESLint Error
Fix for "no-unused-expressions" ESLint error when using JavaScript optional chaining.
Read more >
no-unused-expression - Rule
Disallows unused expression statements. Unused expressions are expression statements which are not assignments or function calls (and thus usually no-ops).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found