【问题标题】:Loading React Native: UnhandledPromiseRejectionWarning: Error: Cannot find module 'View'加载 React Native:UnhandledPromiseRejectionWarning:错误:找不到模块“视图”
【发布时间】:2019-02-23 21:00:39
【问题描述】:

我正在尝试为测试库做出贡献。该库应该为react-test-renderer 提供一个包装器,就像react-native-testing-library 一样。

为了查看我的代码是否有效,我使用我为库编写的代码编写了一个简单的单元测试。由于这是对 React Native 的第一次测试,我添加了 react-native 作为开发依赖项以及 metro-react-native-babel-preset。此外,我在 babel 中添加了预设。

{
  "presets": [
    [
      "env",
      {
        "targets": {
          "browsers": ["last 2 versions", "safari >= 7"]
        }
      }
    ],
    "react",
    "module:metro-react-native-babel-preset"
  ]
}

该库使用tape 进行断言。

现在,每个测试 (npm test) 都会抛出错误:

> node -r babel-register -r babel-polyfill source/test
/path/to/node_modules/babel-core/lib/transformation/file/options/option-manager.js:328
        throw e;
        ^

Error: Couldn't find preset "module:metro-react-native-babel-preset" relative to directory

当我从 babel 配置中删除预设时,我收到错误:

(node:1841) UnhandledPromiseRejectionWarning: Error: Cannot find module 'View'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
    at Function.Module._load (internal/modules/cjs/loader.js:507:25)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Object.get View [as View] (/path/to/node_modules/react-native/Libraries/react-native/react-native-implementation.js:165:12)
    at _callee7$ (/path/to/source/test.js:110:6)
    at tryCatch (/path/to/node_modules/babel-polyfill/node_modules/regenerator-runtime/runtime.js:65:40)
    at Generator.invoke [as _invoke] (/path/to/node_modules/babel-polyfill/node_modules/regenerator-runtime/runtime.js:303:22)
    at Generator.prototype.(anonymous function) [as next] (/path/to/node_modules/babel-polyfill/node_modules/regenerator-runtime/runtime.js:117:21)
    at step (/path/to/source/test.js:27:191)
(node:1841) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)
(node:1841) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
not ok 10 test exited without ending
  ---
    operator: fail
    at: process.<anonymous> (/path/to/node_modules/tape/index.js:90:19)
    stack: |-
      Error: test exited without ending
          at Test.assert [as _assert] (/path/to/node_modules/tape/lib/test.js:226:54)
          at Test.bound [as _assert] (/path/to/node_modules/tape/lib/test.js:77:32)
          at Test.fail (/path/to/node_modules/tape/lib/test.js:319:10)
          at Test.bound [as fail] (/path/to/node_modules/tape/lib/test.js:77:32)
          at Test._exit (/path/to/node_modules/tape/lib/test.js:191:14)
          at Test.bound (/path/to/node_modules/tape/lib/test.js:77:32)
          at process.<anonymous> (/path/to/node_modules/tape/index.js:90:19)
          at process.emit (events.js:194:15)

如何让 React Native 组件在库中的测试中运行(以及在 expo initreact-native init 项目之外?

编辑:我发现 repo 使用的是 Babel 6,这就是 Metro 预设似乎不起作用的原因。所以我把它换成了babel-preset-react-native,现在我又回到了最初的错误,即找不到View

编辑 2: package.json

  "scripts": {
    "lint": "eslint source && echo 'Lint complete.'",
    "typecheck": "npx -p typescript tsc --rootDir . source/test.js --allowJs --checkJs --noEmit --lib es6 --jsx react && echo 'TypeScript check complete.'",
    "ts": "npm run -s typecheck",
    "test": "node -r babel-register -r babel-polyfill source/test",
    "watch": "watch 'clear && npm run -s test | tap-nirvana && npm run -s lint && npm run -s typecheck' source",
    "precommit": "npm run -s test && npm run -s lint && npm run -s typecheck"
  },
  "devDependencies": {
    "@types/node": "10.12.27",
    "babel-cli": "6.26.0",
    "babel-eslint": "10.0.1",
    "babel-preset-env": "1.7.0",
    "babel-preset-react": "6.24.1",
    "babel-preset-react-native": "4.0.1",
    "babel-register": "6.26.0",
    "eslint": "5.14.1",
    "eslint-plugin-react": "7.12.4",
    "react": "16.8.3",
    "react-native": "0.58.5",
    "tap-nirvana": "1.1.0",
    "typescript": "3.3.3333",
    "watch": "1.0.2"
  },
  "dependencies": {
    "cheerio": "1.0.0-rc.2",
    "esm": "3.2.6",
    "react-dom": "16.8.3",
    "react-test-renderer": "16.8.3",
    "tape": "4.10.1"
  }

编辑 3: 抱歉,显然我没有包含我的代码,尽管它有助于解决这个问题。这是非常基本的。我只是想使用ReactTestRenderer 渲染&lt;View /&gt;

import TestRenderer from 'react-test-renderer';
import { Text, View } from 'react-native';
import { describe } from 'riteway';

describe('renderReactNativeComponent', async assert => {
  const text = 'Foo';
  const component = TestRenderer.create(
    <View>
      <Text>{text}</Text>
    </View>
  );
  console.log(Text);

  assert({
    given: 'A React Native component',
    should: 'return a working react test renderer instance',
    actual: component.findByType('text'),
    expected: text
  });
});

【问题讨论】:

  • 你能分享你的package.json 吗?并且 babel 预设是否添加到 .babelrcbabel.config.js 中?
  • @PritishVaidya 我添加了package.json。这是一个.babelrc
  • 您没有包含实际代码,所以这似乎基本上无法回答。引用View 的代码是什么? View 本身是什么?你是怎么加载它的?
  • @loganfsmyth 我编辑了这个问题。让我知道这是否有帮助,我仍然坚持这个????感谢您的帮助!

标签: javascript react-native babeljs tap babel-polyfill


【解决方案1】:

根据docs,“从 react-native 版本 0.38 开始,运行 react-native init 时默认包含 Jest 设置。”.

"react-native 附带了一个 Jest 预设,因此你的 package.json 的 jest.preset 字段应该指向 react-native。预设是一个节点环境,它模仿了 React Native 应用程序的环境。因为它不加载任何 DOM 或浏览器 API,它极大地改善了 Jest 的启动时间。”

关键是 Jest 应该与 React-Native 一起使用,而 riteway 不是。当您运行 react-native init 时,会添加以下依赖项:"jest-react-native"。并在package.json 中添加了一个笑话预设:

"jest": {
   "preset": "react-native"
}

显然目前还没有任何 riteway 预设。

【讨论】:

【解决方案2】:

删除节点模块并重新安装

rm -rf node_modules
npm install

【讨论】:

  • 如果它成功了我会笑的,但不,同样的错误:UnhandledPromiseRejectionWarning: Error: Cannot find module 'View'。谢谢你,想!
  • 我也有同样的问题,但不知何故对我有用
  • 这对我有用:P 节点:v14.15.1 npm:6.14.8 yarn:1.22.4 expo:~39.0.2
【解决方案3】:

你没有返回任何东西,你需要返回一个“视图”

【讨论】:

    【解决方案4】:

    您可能只想使用https://reactjs.org/docs/test-renderer.html 中的建议示例,以确保您编写的代码没有问题,然后添加您自己的逻辑来改进它(只是调试它的想法)。可以添加到https://snack.expo.io 吗?或者github,我帮你调试。

    【讨论】:

    • 谢谢,我(几乎)使用 React-Test-Renderer 文档对示例进行了逐字测试。 (我刚刚将&lt;Link /&gt; 更改为&lt;View /&gt;。)仍然是同样的错误(找不到&lt;View /&gt;)。 Expo Snack 无济于事,因为代码在 react-native initexpo init 应用程序中有效(因此也在 Snack 中)。问题是让 React Native 在 RN 项目的外部运行。
    猜你喜欢
    • 1970-01-01
    • 2016-10-27
    • 2017-02-17
    • 2022-12-01
    • 2016-03-31
    • 2021-09-13
    • 2020-07-09
    • 2022-11-28
    • 2017-07-04
    相关资源
    最近更新 更多