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

问题描述 投票:0回答:6

我正在尝试运行 Jest 测试,但收到以下错误:

读取文件时出错:

/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 抛出错误; ^

错误:EMFILE:打开的文件太多,打开'/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' 错误时(本地) npm 错误!测试失败。请参阅上文了解更多详情。

令我感兴趣的是,错误中列出的路径指向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"
  }
}

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

reactjs react-native jestjs ulimit
6个回答
26
投票

我在一个小型 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'
  ],
};

8
投票

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

答案与我没有正确设置 ulimit 有关。

sudo ulimit -n 10240

在我的 Mac 上,默默地不会更改 ulimit。我原本以为它没有做任何事情,因为 10240 不是 1024 的增量。但当我尝试 2048、4096 等时,它也没有做任何事情

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

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

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/
目录。


2
投票

我遇到了这个问题并通过运行修复了它

brew upgrade watchman

1
投票

我前段时间也遇到过类似的问题,但不明白为什么 jest 没有忽略我的 node_modules 文件夹。

我最终所做的是将

"testFileExtensions"
["js"]
更改为
["spec.js"]
并将所有测试重命名为扩展名
.spec.js

这不是正确的解决方案,但给了我时间,看看新版本的 jest 是否解决了这个问题


0
投票

调整看守人限制对我有用。 此 watchman 文档 表明这仅与 macOS 相关 <= 10.6, but solved my problem on 10.13.6.

© www.soinside.com 2019 - 2024. All rights reserved.