New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add option for no-unused-expressions #2102
Comments
Just turn the rule off, at least for your test directory. This is a pretty explicit violation of exactly what this rule is trying to prevent. |
@ethanresnick can you please provide a sample code snippet and the exact console output that ESLint is giving you? On the surface, I'm inclined to agree with @michaelficarra that your best option is to disable the rule in your tests, where it's arguably less useful. |
Sure! Here's the code, in the most reduced form:
And the console output:
With the config:
As far as just turning the expression off goes: yes, that's always an option. But, of course, if there's a way to leave it on to get whatever other utility it provides, while telling it not to flag these usages that are beyond my control, that would be nice :) |
I am facing this exact same issue. I currently use the |
👍 |
Yup, we are aware. We still don't have a solution that doesn't involve building-in knowledge of Chai. If we can't find one soon, we are going to just recommend you turn off the rule for tests. |
Hitting this same issue. Would love a work around, perhaps to exclude that rule from a pattern e.g. *.spec.js (apologies if this is possible already, i could not work out how). |
@a-c-m it is possible through multiple eslint invocations |
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. While it's unfortunate and not ideal, it's up to you to turn this off where appropriate. You can do that in a config or using online comments. See our docs for more: http://eslint.org/docs/user-guide/configuring I'm closing this issue as we don't plan on addressing this. |
FYI, a small chai plugin which gives you these property getters as functions: https://www.npmjs.com/package/chai-lint |
dirty-chai works for me. It also automatically converts plugin assertions. |
@justin-lau dirty-chai addressed this issue fantastically for me, thanks for sharing |
👍 for 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); |
There is no way to make eslint shut up about chai's assertion expressions (`expect(foo).to.be.true`) without disabling this rule. See eslint/eslint#2102
(1) .eslintrc: Ignore testing globals Mocha globals like `it` & `describe` trigger eslint warnings because they are not imported explicitly. Setting them as `globals` in .eslintrc fixes that. (2) Make eslintrc compatible with chai assertion expressions There is no way to make eslint shut up about chai's assertion expressions (`expect(foo).to.be.true`) without disabling this rule. See eslint/eslint#2102 (3) Lint resolver.test.js
👍 for dirty-chai |
Might be a lot of work for not a lot of gain, but wouldn't a rule that ignores "no-unused-expressions" when "env" is set to "mocha" and only for the few mocha magic properties (ok, undefined, ...) make sense? |
In core ESLint, no, but you are absolutely welcome to write a plugin rule which emulates no-unused-expressions except in the circumstances you described. |
Ok maybe some day when I'm really bored :) After a little more thought I don't think it's worth it or even conceptually such a good idea, eslint-disable-next-line is not so hard to write and provides more information to the reader. |
@nzakas with all respect, you said:
But actually, ESLint, as JSHint, does support "environment options" such as 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 This library agnostic option can be interesting but:
|
This eslint plugin seems to work for me: https://www.npmjs.com/package/eslint-plugin-chai-friendly I agree an environment option, similar to mocha, is the best way forward. |
Environments in ESLint are used to define global variables, and occasionally set parser options. By design, they cannot change the behavior of rules like Additionally, while ESLint used to add environments for custom libraries, it no longer does this.
ESLint is a static analysis tool, so it's not possible to tell whether any given property access triggers a getter. Providing a list of accessor properties via configuration is feasible; #8075 is another option. |
@not-an-aardvark Thank for your feedback Even if it's a bit frustrating, I've seen in the meantime, and understand, that supporting other environment configs are not exactly the chosen path for either JSHint or ESLint. Probably is it better to get a list of environment lint plugins so they could be co-supported by some of the target platform contributors.
Well, I started to work on AST transforms. As far as I saw (both babylon & Espree (used by ESLint) being based on Acorn), it looks quite possible from a static analysis tool to see when a property is defined via
But yes there is a limitation when working in cross file contexts...
It is the direction that might be taken on the JSHint side as mentioned on this proposal |
I am having a similar issue in the form of getting the following error:
It comes from the following line:
I have tried |
Fix failing tests by ignoring the ESLint rule `no-unused-expressions` in both `*.spec.js` files and by using the new NodeJS buffer syntax as seen on the following sources: - eslint/eslint#2102 - https://nodejs.org/dist/latest-v6.x/docs/api/buffer.html Update chai and sinon to versions `2.3.4` and `4.0.2`, respectively. As a result of updating chai, change wording in unit test to reflect the new `4.x` API. Namely, from `.to.contain` to `.to.deep.include`. https://greenkeeper.io/
Fix failing tests by ignoring the ESLint rule `no-unused-expressions` in both `*.spec.js` files and by using the new NodeJS buffer syntax as seen on the following sources: - eslint/eslint#2102 - https://nodejs.org/dist/latest-v6.x/docs/api/buffer.html Update chai and sinon to versions `2.3.4` and `4.0.2`, respectively. As a result of updating chai, change wording in unit test to reflect the new `4.x` API. Namely, from `.to.contain` to `.to.deep.include`. https://greenkeeper.io/
@nzakas you are one of the expert amongst us, What are the advantages in chai implementation, to omit the useage of the parentheses |
@aaron-goshine You're absolutely right. The advantage is no parentheses. The disadvantage is mass confusion and many broken assumptions. Nobody should ever have side-effecting getters, without exception. |
I'm using Chai's expect syntax for testing, and I'm getting
no-unused-expression
errors. I understand that this is because expressions likeexpect(...).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.The text was updated successfully, but these errors were encountered: