Retsam Retsam - 11 months ago 106
Javascript Question

How to use ESLint with Jest

I'm attempting to use the ESLint linter with the Jest testing framework.

Jest tests run with some globals like

, which I'll need to tell the linter about; but the tricky thing is the directory structure, with Jest the tests are embedded with the source code in
folders, so the directory structure looks something like:


Normally, I'd have all my tests under a single dir, and I could just add an
file there to add the globals... but I certainly don't want to add a
file to every single

For now, I've just added the test globals to the global
file, but since that means I could now reference
in non-testing code, that doesn't seem like the "right" solution.

Is there a way to get eslint to apply rules based on some pattern based on the directory name, or something like that?

Answer Source

Pattern based configs are scheduled for 2.0.0 release of ESLint. For now, however, you will have to create two separate tasks (as mentioned in the comments). One for tests and one for the rest of the code and run both of them, while providing different .eslintrc files.

P.S. There's a jest environment coming in the next release of ESLint, it will register all of the necessary globals.