首页 > 解决方案 > 测试 React Native App 时如何避免集成 ios?

问题描述

我对 react-native-firebase 有一些问题,我有一个Windows 环境,我想使用 Jest 进行测试,所以当我运行npm test测试套件失败时

因为我有一个 react-native-firebase 包,它应该集成在 IOS 中,我只是重命名文件Android 并取消链接 react-native-firebase 以避免这些,但它对我不起作用!那么,如果我没有 Mac,我该如何处理

测试代码

/**
 * @format
 * @lint-ignore-every XPLATJSCOPYRIGHT1
 */

import "react-native";
import React from "react";
import App from "../App";
import SignIn from "../src/screens/SignIn";

// Note: test renderer must be required after react-native.
import renderer from "react-test-renderer";

// it("renders correctly", () => {
//   renderer.create(<App />);
// });

test("renders correctly", () => {
  const tree = renderer.create(<SignIn />).toJSON();
  expect(tree).toMatchSnapshot();
});

测试结果

 FAIL  __tests__/App.js
  ● Test suite failed to run

    RNFirebase core module was not found natively on iOS, ensure you have correctly included the RNFirebase pod in your projects `Podfile` and have run `pod install`.

     See http://invertase.link/ios for the ios setup guide.

      at new Firebase (node_modules/react-native-firebase/dist/modules/core/firebase.js:28:13)
      at Object.<anonymous> (node_modules/react-native-firebase/dist/modules/core/firebase.js:97:21)
      at Object.<anonymous> (node_modules/react-native-firebase/dist/index.js:1:1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        6.137s
Ran all test suites.
npm ERR! Test failed.  See above for more details.

标签: reactjsunit-testingreact-nativejestjsreact-native-firebase

解决方案


您可以使用import {Platform} from 'react-native';

import {Platform} from 'react-native';

if(Platform.OS !== 'ios') {
test("renders correctly", () => {
  const tree = renderer.create(<SignIn />).toJSON();
  expect(tree).toMatchSnapshot();
});

}

推荐阅读