React Native + Jest EMFILE: too many open files error
Asked Answered
C

6

22

I am trying to run Jest tests, but I'm getting the following error:

Error reading file: /Users/mike/dev/react/TestTest/node_modules/react-native/node_modules/yeoman-environment/node_modules/globby/node_modules/glob/node_modules/path-is-absolute/package.json /Users/mike/dev/react/TestTest/node_modules/jest-cli/node_modules/node-haste/lib/loader/ResourceLoader.js:88 throw err; ^

Error: EMFILE: too many open files, open '/Users/mike/dev/react/TestTest/node_modules/react-native/node_modules/yeoman-environment/node_modules/globby/node_modules/glob/node_modules/path-is-absolute/package.json' at Error (native) npm ERR! Test failed. See above for more details.

What is interesting to me is that the path listed in the error points to a file in the node_modules directory, which I expected would not be read because of the node_modules entry in testPathIgnorePatterns.

I'm running Node 4.2.1, my install of React-Native is only a week old, I installed Jest today (so I think I'm up to date with everything). I'm on a Mac.

I have run: sudo ulimit -n 10240, closed all Terminal windows, and even tried a reboot. (In my .bash_profile I had previously added ulimit -n 1024. And I've tried even larger numbers.

To make sure the problem is not just in my own project, I created a new project with react-native init TestTest and made RN's suggested changes to the package.json:

{
  "name": "TestTest",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node_modules/react-native/packager/packager.sh",
    "test": "jest"
  },
  "dependencies": {
    "react-native": "^0.14.1"
  },
  "jest": {
    "scriptPreprocessor": "node_modules/react-native/jestSupport/scriptPreprocess.js",
    "setupEnvScriptFile": "node_modules/react-native/jestSupport/env.js",
    "testPathIgnorePatterns": [
      "/node_modules/",
      "packager/react-packager/src/Activity/"
    ],
    "testFileExtensions": [
      "js"
    ],
    "unmockedModulePathPatterns": [
      "promise",
      "source-map"
    ]
  },
  "devDependencies": {
    "jest-cli": "^0.7.1"
  }
}

But I'm getting the same error every time.

Curling answered 8/11, 2015 at 1:40 Comment(1)
After trying other options, I ended up having success with brew install watchman according to this forum post bountysource.com/issues/…Baldhead
C
8

Short answer: adding 'ulimit -n 4096' to ~.bash_profile and opening a new terminal window resolved my issue.

The answer had to do with me not setting the ulimit properly.

sudo ulimit -n 10240

on my Mac silently doesn't change the ulimit. I had originally thought it was not doing anything because 10240 is not an increment of 1024. But it also didn't do anything when I tried 2048, 4096, etc.

So, what is "the" solution?

  • ulimit -n (without a number) will tell you what the current value is
  • for me, typing sudo ulimit -n 2048 in a terminal window did NOT change the ulimit (no matter what number I tried)
  • adding 'ulimit -n 4096' to ~.bash_profile and opening a new terminal solved the problem
Curling answered 11/11, 2015 at 17:21 Comment(2)
If you remove the sudo and just call ulimit -n 10240 it should work on OSXProprioceptor
resetting the ulimit didn't help meGrit
F
28

I had the same problem with a small vuejs project. In my case the fix was just a small configuration property: I put this in to my jest.config.js

  watchPathIgnorePatterns: ['node_modules'],

For anyone withe the same problem with vuejs. Here's my full jest.config.js file:

module.exports = {
  preset: '@vue/cli-plugin-unit-jest',
  transformIgnorePatterns: ['/node_modules/(?!(leaflet))'],
  watchPathIgnorePatterns: ['node_modules'],
  testMatch: [
    '<rootDir>/src/**/*.spec.js'
  ],
};
Frivol answered 6/4, 2020 at 14:48 Comment(5)
Awesome! Worked like a charmPomiferous
This one worked for me, on my new MacBook Air with M1 CPU.Magnate
works for NextJS apps as well (MacOS, Intel)Uncircumcision
This solution worked on mac - NodeJS 16 environment / ExpressJS frameworkVereen
Works for Quasar framework tooRamiroramjet
C
8

Short answer: adding 'ulimit -n 4096' to ~.bash_profile and opening a new terminal window resolved my issue.

The answer had to do with me not setting the ulimit properly.

sudo ulimit -n 10240

on my Mac silently doesn't change the ulimit. I had originally thought it was not doing anything because 10240 is not an increment of 1024. But it also didn't do anything when I tried 2048, 4096, etc.

So, what is "the" solution?

  • ulimit -n (without a number) will tell you what the current value is
  • for me, typing sudo ulimit -n 2048 in a terminal window did NOT change the ulimit (no matter what number I tried)
  • adding 'ulimit -n 4096' to ~.bash_profile and opening a new terminal solved the problem
Curling answered 11/11, 2015 at 17:21 Comment(2)
If you remove the sudo and just call ulimit -n 10240 it should work on OSXProprioceptor
resetting the ulimit didn't help meGrit
M
4

I'm running windows in powershell. Also used Git Bash and had similar problems. I updated my package.json as follows:

  "devDependencies": {
    "jest-cli": "^0.8.2",
  },
  "jest": {
    "testPathDirs": ["__tests__"],
    "testPathIgnorePatterns": [
      "/node_modules/"
    ]
  },
  "scripts": {
    "test": "jest"
  },

Note that "testPathDirs": ["__tests__"], is the directory I place all my tests in.

Now I can run npm test without an issue. Ultimately it seems Jest is navigating the /node_modules/ directory which should be excluded.

Monogenesis answered 2/3, 2016 at 19:3 Comment(2)
This solved my problem. For anyone using react-scripts (create react app), you'll have to replace "testPathIgnorePatterns" with "watchPathIgnorePatterns".Abbevillian
Thank you sir! this actually solves the problem rather dodge it. for CRA, just changing jest option watchPathIgnorePatterns: ['/node_module/'] should fix it. Jest is running test inside of node_modules. installing watchman does not solve that it just make jest power through using resources and slowing down testsBattalion
C
2

I encountered this problem and fixed it by running

brew upgrade watchman
Cleavable answered 4/4, 2020 at 3:23 Comment(0)
C
1

I had a similar issue some time ago but couldn't figure out why jest was not ignoring my node_modules folder.

What I ended up doing is changing "testFileExtensions" from ["js"] to ["spec.js"] and renaming all the tests with the extension .spec.js

Not the proper solution but gives me time until seeing if new versions of jest fix this issue

Courtney answered 11/11, 2015 at 14:58 Comment(1)
This was a great suggestion, but it did not resolve the issue for me. But what it did do was remove one big possibility, which lead me to pursue other possible causes (which I eventually found, see my answer above). Thanks!Curling
O
0

Adjusting watchman limits worked for me. This watchman documentation indicates this is only relevant for macOS <= 10.6, but solved my problem on 10.13.6.

Ornithopter answered 25/2 at 23:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.