Skip to content
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

Specify eslint-env globally? #122

Closed
pdehaan opened this issue Apr 22, 2015 · 11 comments

Comments

@pdehaan
Copy link

commented Apr 22, 2015

Is there a way to set ESLint environments globally (via config or setting) without having to specify ESLint eslint-env flags in each file? (ie: /*eslint-env and, mocha */)

When I run $ standard in one of our project's directories, I get a bunch of these warnings in dozens of files:

/tests/spec/views/ready.js:109:8: 'it' is not defined.
/tests/spec/views/reset_password.js:7:0: 'define' is not defined.
/tests/spec/views/reset_password.js:28:4: 'describe' is not defined.
/tests/spec/views/reset_password.js:50:6: 'beforeEach' is not defined.
/tests/spec/views/reset_password.js:63:12: '$' is not defined.
/tests/spec/views/reset_password.js:67:6: 'afterEach' is not defined.
/tests/spec/views/reset_password.js:72:8: '$' is not defined.
/tests/spec/views/reset_password.js:77:6: 'describe' is not defined.
/tests/spec/views/reset_password.js:78:8: 'it' is not defined.
/tests/spec/views/reset_password.js:79:20: '$' is not defined.
/tests/spec/views/reset_password.js:82:8: 'it' is not defined.
/tests/spec/views/reset_password.js:113:6: 'describe' is not defined.
/tests/spec/views/reset_password.js:114:8: 'it' is not defined.
/tests/spec/views/tooltip.js:58:8: 'it' is not defined.
/tests/spec/views/tos.js:7:0: 'define' is not defined.
/tests/spec/views/tos.js:17:4: 'describe' is not defined.
/tests/spec/views/tos.js:22:6: 'beforeEach' is not defined.
/tests/spec/views/tos.js:38:6: 'afterEach' is not defined.
/tests/spec/views/tos.js:43:6: 'it' is not defined.
/tests/spec/views/unexpected_error.js:7:0: 'define' is not defined.
/tests/spec/views/unexpected_error.js:16:4: 'describe' is not defined.
/tests/spec/views/unexpected_error.js:19:6: 'beforeEach' is not defined.
/tests/spec/views/unexpected_error.js:28:6: 'afterEach' is not defined.
/tests/spec/views/unexpected_error.js:34:6: 'it' is not defined.
/tests/test_start.js:130:6: 'beforeEach' is not defined.
/tests/test_start.js:134:6: 'afterEach' is not defined.
...
@rlidwka

This comment has been minimized.

Copy link

commented Apr 22, 2015

Put it into .eslintrc file in your /tests folder like this.

@pdehaan

This comment has been minimized.

Copy link
Author

commented Apr 22, 2015

@rlidwka, odd, that didn't seem to work:

$ standard --version # 3.7.0

$ cat tests/.eslintrc
env:
  mocha: true
  amd: true

$ standard tests
$ standard tests --verbose

$ cd tests
$ standard
standard: Use JavaScript Standard Style (https://github.com/feross/standard)
  ./app/tests/lib/blanket_lcov.js:9:0: Unexpected space between function name and paren.
  ./app/tests/lib/helpers.js:7:0: 'define' is not defined.
  ./app/tests/mocks/canvas.js:7:0: 'define' is not defined.
  ./app/tests/mocks/crosstab.js:7:0: 'define' is not defined.
  ./app/tests/mocks/crosstab.js:13:37: Extra semicolon.
  ./app/tests/mocks/dom-event.js:7:0: 'define' is not defined.
  ./app/tests/mocks/file-reader.js:7:0: 'define' is not defined.
  ./app/tests/mocks/fxa-client.js:7:0: 'define' is not defined.
  ./app/tests/mocks/history.js:6:0: 'define' is not defined.
  ./app/tests/mocks/oauth_servers.js:5:0: 'define' is not defined.
  ./app/tests/mocks/profile.js:7:0: 'define' is not defined.
  ./app/tests/mocks/router.js:7:0: 'define' is not defined.
  ./app/tests/mocks/window.js:7:0: 'define' is not defined.
  ./app/tests/mocks/window.js:59:34: There should be no spaces inside this paren.
  ...

$ standard | wc -l
standard: Use JavaScript Standard Style (https://github.com/feross/standard)
    1554
@feross

This comment has been minimized.

Copy link
Member

commented Apr 22, 2015

Just add a /* global define, it, describe */ comment at the top of each file.

Update: See #122 (comment) for a better solution!

@feross feross closed this Apr 22, 2015

@pdehaan

This comment has been minimized.

Copy link
Author

commented Apr 22, 2015

FWIW, we have about 108 .js files in our /app/tests/* directory.
Currently we're using JSHint and can just specify the globals in the .jshintrc file in one place instead of copy/pasting into over a hundred files separately.

@jprichardson

This comment has been minimized.

Copy link
Member

commented Apr 22, 2015

What I like about embedding the globals in the comments is that a JavaScript file then becomes standard "certified" in a sense. External configs remove this.

@pdehaan I know it's painful as I've done it with a couple of projects now. To ease the pain, what if you whipped up a quick script (using recast or just prepend text) to insert the global comments in every file?

@pdehaan

This comment has been minimized.

Copy link
Author

commented Apr 22, 2015

@jprichardson Yeah, it's not difficult/impossible to do, but gets exponentially worse if we change our tests to some other library that uses different globals or syntax. I'd either have to go through and search/replace all these crazy /*global define, it, describe, afterEach, beforeEach, ... */ strings in every file or else manually go through them and tweak them so we don't end up with 2-3 different /*global ... */ blocks or a mix of /*global ... */ and /*eslint-env ... */ blocks.

But I totally agree with the I disagree with rule X, can you change it docs in the README. Plus the Can you make rule X configurable? docs clearly say:

Can you make rule X configurable?
No. Use eslint directly if you want to configure hundreds of options individually.

@jprichardson

This comment has been minimized.

Copy link
Member

commented Apr 22, 2015

@pdehaan Totally understand. If you do change to another testing library, you could use recast to rewrite each file with the new globals. If you haven't used recast yet, it's one of the easiest ways to use esprima and parse/emit JavaScript. It's worth checking out at some point :)

@matthewadams

This comment has been minimized.

Copy link

commented Apr 27, 2016

So I'm a little confused here. I'm see the same behavior as @pdehaan. However, http://eslint.org/docs/user-guide/configuring#specifying-environments says that the environment mocha is explicitly supported. So, is this a problem only when linting with standard? It seems to work correctly if I npm install -g eslint eslint-config-standard then eslint . | grep describe in my project directory (there's no output because nothing got greped).

In my project, I've simply done npm install --save-dev standard, then invoked it via $(npm bin)/standard. When I lint this way, I get 'describe' is not defined messages.

Is this expected behavior from standard? I don't really like the suggestion by @feross (#122 (comment)) to litter all files with eslint directives; it seems like creating a .eslintrc in my test/ directory with content

{
  "env": {
    "node": true,
    "mocha": true
  }
}

should do the trick. I'd appreciate clarification by a knowledgeable somebody. :)

@feross

This comment has been minimized.

Copy link
Member

commented May 5, 2016

.eslintrc and .eslintignore files are not supported by standard. Your options are:

  1. Add /*eslint-env mocha */ to the top of the file with mocha globals.
  2. Or, add "env": [ "mocha" ] to the package.json under the "standard" property.
@ImanMh

This comment has been minimized.

Copy link

commented Apr 14, 2018

I have myFile.js and myFile.test.js side by side in a directory. How can I make it work? It also says that expect is available in myFile.js which is false.

@feross

This comment has been minimized.

Copy link
Member

commented Apr 15, 2018

@ImanMh Your comment has nothing to do with this issue. Please open a new issue and explain your problem thoroughly. Thanks.

@standard standard locked and limited conversation to collaborators Apr 15, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
6 participants
You can’t perform that action at this time.