6

I've just generated a fresh project with npx react-native init and ESLint is complaining in one of my test files:

   9:1   error  'describe' is not defined    no-undef
  12:5   error  'beforeEach' is not defined  no-undef
  16:5   error  'afterEach' is not defined   no-undef
  20:5   error  'test' is not defined        no-undef
  28:17  error  'fail' is not defined        no-undef
  30:13  error  'expect' is not defined      no-undef

Based on the docs and this thread, I've added:

env: {
    jest: true,
},

to my .eslintrc.js file. However, ESLint is still complaining with:

  28:17  error  'fail' is not defined  no-undef

Has anyone already experienced and solved this issue ?

Here are the jest dependencies versions in package.json:

"babel-jest": "^26.5.2",
"jest": "^26.5.3",
c4k
  • 4,270
  • 4
  • 40
  • 65
  • If you're seeing this message in your editor, make sure to turn it off and on again. Sometimes editors don't pick up that the ESLint configuration changed. – VLAZ Oct 18 '20 at 13:34
  • 1
    Unfortunately, I'm seeing it from my Terminal where I execute the command manually. – c4k Oct 18 '20 at 13:36
  • Other than that, I'm not really sure. I had the exact same problem as you literally yesterday and I solved it by adding the `env` property in the config. I used [this approach](https://stackoverflow.com/questions/56398742/eslint-throws-no-undef-errors-when-linting-jest-test-files) so it's only enabled for the test files (I had to adjust the filter, as well to match my naming scheme of `*.test.ts`) but it did work - ESLint stopped complaining. I also added a small `"rules"` section in the overrides to tweak some stylistic rules for the tests. – VLAZ Oct 18 '20 at 13:37
  • Are you also using `fail` in one of your tests ? – c4k Oct 18 '20 at 13:38
  • No, but let me try quickly if it accepts it. – VLAZ Oct 18 '20 at 13:39
  • 1
    OK, so I added a very simple test it("fails", function() { fail("always"); })` and the test fails (expectedly). ESLint also complains with a `no-undef` but it doesn't have issues with stuff like `describe` or `expect. I'll have to see what that one doesn't work, it's weird. – VLAZ Oct 18 '20 at 13:46

2 Answers2

16

Ok so it turns out Jest uses Jasmine's fail().

Updated .eslintrc.js by adding Jasmine and it works. No more errors.

env: {
    jasmine: true,
    jest: true,
},
Dharman
  • 30,962
  • 25
  • 85
  • 135
c4k
  • 4,270
  • 4
  • 40
  • 65
0

The former runner, jest-jasmine2, is deprecated in Jest since 27.0.0 and jest-circus is getting used instead.

Update .eslintrc.js

env: {
    jest: true,
    circus: true,
}
Athul Raj
  • 349
  • 3
  • 11