React Native + Jest EMFILE: 打开文件太多错误

20

我正在尝试运行Jest测试,但是出现以下错误:

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.

对我来说有趣的是,错误中列出的路径指向node_modules目录中的一个文件,我原本以为由于testPathIgnorePatterns中包含了node_modules条目,所以不会读取这个文件。

我正在运行Node 4.2.1,React-Native安装仅一周,今天安装了Jest(所以我认为我已经更新了所有内容)。 我在Mac上运行。

我运行过: sudo ulimit -n 10240,关闭了所有终端窗口,甚至尝试了重新启动。 (在我的.bash_profile文件中,我之前添加了ulimit -n 1024。我甚至尝试过更大的数字。

为了确保问题不仅存在于我的项目中,我使用react-native init TestTest创建了一个新项目,并对package.json进行了RN建议的更改:

{
  "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"
  }
}

但是我每次都遇到相同的错误。


在尝试其他选项后,我最终通过按照这篇论坛帖子 https://www.bountysource.com/issues/37982520-watch-mode-stopped-working-on-macos-sierra 进行 brew install watchman 安装成功。 - Brandon Culley
5个回答

24

我在一个小的vuejs项目中遇到了同样的问题。在我的情况下,解决方法只是一个小配置属性:我将其放入了我的jest.config.js中。

  watchPathIgnorePatterns: ['node_modules'],

对于任何有vuejs问题的人。这是我的完整jest.config.js文件:

module.exports = {
  preset: '@vue/cli-plugin-unit-jest',
  transformIgnorePatterns: ['/node_modules/(?!(leaflet))'],
  watchPathIgnorePatterns: ['node_modules'],
  testMatch: [
    '<rootDir>/src/**/*.spec.js'
  ],
};

太棒了!像魔法一样顺利。 - Henrique Liberato
1
这个对我来说有效,在我的新 MacBook Air 上使用 M1 CPU。 - jtompl
适用于 NextJS 应用程序(MacOS,Intel)的工作。 - oyalhi
这个解决方案在 Mac - NodeJS 16 环境 / ExpressJS 框架上运行成功。 - Hashan
同样适用于 Quasar 框架 - rodvlopes

8

简短回答:在~.bash_profile中添加“ulimit -n 4096”并打开一个新的终端窗口解决了我的问题。

问题出在我没有正确地设置ulimit。

sudo ulimit -n 10240

在我的Mac上静默地不会改变ulimit。一开始我以为它没有起作用是因为10240不是1024的增量。但是当我尝试2048、4096等时,它也没有做任何事情。

那么,“the”解决方案是什么?

  • ulimit -n(不带数字)将告诉您当前值是多少
  • 对我来说,在终端窗口中键入sudo ulimit -n 2048并不会更改ulimit(无论我尝试什么数字)
  • 将'ulimit -n 4096'添加到~.bash_profile并打开一个新的终端即可解决问题

4
如果您移除sudo并直接调用ulimit -n 10240,它应该在OSX上运行。 - Dana Woodman
重置ulimit并没有帮助我。 - Sunil Kumar Singh

4

我正在使用PowerShell运行Windows。我之前也尝试了Git Bash,但遇到了类似的问题。我按照以下方式更新了package.json:

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

请注意,"testPathDirs": ["__tests__"]是我放置所有测试文件的目录。
现在我可以毫无问题地运行npm test。最终,Jest似乎正在导航到应该被排除的/node_modules/目录。

这解决了我的问题。对于使用react-scripts(创建React应用程序)的任何人,您将不得不用“watchPathIgnorePatterns”替换“testPathIgnorePatterns”。 - Don Brody
谢谢您!这实际上解决了问题,而不是回避它。对于 CRA,只需更改 jest 选项 watchPathIgnorePatterns: ['/node_module/'] 即可解决问题。Jest 在 node_modules 中运行测试。安装 watchman 并不能解决这个问题,它只会让 Jest 使用资源并减慢测试速度。 - Femi Oni

2
我遇到了这个问题,并通过运行以下命令解决了它:
brew upgrade watchman

1

我之前遇到过类似的问题,但是无法弄清楚为什么jest没有忽略我的node_modules文件夹。

最终我做的是将"testFileExtensions"["js"]更改为["spec.js"]并将所有测试用例的扩展名重命名为.spec.js

这不是正确的解决方案,但可以让我有时间看看jest的新版本是否会解决这个问题。


这是一个很好的建议,但对我来说并没有解决问题。但它确实排除了一个重要的可能性,让我去追寻其他可能的原因(最终在上面的答案中找到了)。谢谢! - Mike Hedman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接