平台特定于开玩笑的反应本机的测试



我正在执行的RN项目的目的是"仅Android",因此无需将诸如" React-Native-Firebase"之类的库与CocoApods添加。我必须事先提及我无法访问任何Mac或MacBook。这是来自package.json

的摘要
...
"scripts": {
  "test": "jest"
},
"jest": {
  "preset": "react-native"
},
...

和babel.config.js

module.exports = {
    presets: [
      [
        'module:metro-react-native-babel-preset',
        {
          targets: {
            node: 'current',
          },
          loose: true,
          modules: 'auto',
        },
      ],
    ],
  };

开玩笑测试的虚拟功能:

// testing jest
const generateText = (name, age) => {
  return `${name} (${age} years old)`;
};

和单元测试:

import { generateText } from '../actions/authActions';
test('Some test', () => {
  const text = generateText('Zuul', 300);
  expect(text).toBe('Zuul (300 years old)');
});

运行yarn test的所有设置提供此输出:

    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`.

问题是,有没有一种方法可以使玩笑跳过iOS相关的支票,而只专注于Android?

更新:显然,有一种方法可以覆盖开玩笑的预设,所以我将其添加到包装中。

"jest": {
  "preset": "react-native",
  "haste": {
    "defaultPlatform": "android",
    "platforms": [
      "android",
      "ios",
      "native"
    ],
    "providesModuleNodeModules": [
      "react-native"
    ]
  }
},

现在我有此错误消息:

RNFirebase core module was not found natively on Android,
ensure you have correctly added the RNFirebase and Firebase
gradle dependencies to your `android/app/build.gradle` file.

这根本不是这样,因为将所有必需的依赖项添加到build.gradle:

dependencies {
    ...
    implementation project(':react-native-firebase')
    // Firebase dependencies
    implementation "com.google.android.gms:play-services-base:16.0.1"
    implementation "com.google.firebase:firebase-core:16.0.6"
    implementation('com.crashlytics.sdk.android:crashlytics:2.9.5@aar') {
        transitive = true
    }
    implementation "com.google.firebase:firebase-messaging:17.3.4"
    implementation 'me.leolin:ShortcutBadger:1.1.21@aar'
    ...
}

显然这是一个众所周知的问题。我在这里找到了解决方案。基本上,我将Jest设置从package.json移到jest.config.js:

//jest.config.js
module.exports = {
  preset: 'react-native',
  haste: {
    defaultPlatform: 'android',
    platforms: [
      'android',
      'ios',
      'native',
    ],
    providesModuleNodeModules: [
      'react-native',
    ],
  },
  setupFilesAfterEnv: ['./src/__mocks__/mockNativeLibs.js'],
  automock: false,
  moduleNameMapper: {
    '\.(css|less)$': 'identity-obj-proxy',
    '^.+\.(jpg|jpeg|gif|png|mp4|mkv|avi|webm|swf|wav|mid)$': 'jest-static-stubs/$1',
  },
  globals: {
    __DEV__: true,
  },
  collectCoverageFrom: [
    '**/src/**/*.{js,jsx}',
    '!**/src/**/style.js',
    '!**/src/**/index.js',
    '!**/src/theme/**',
    '!**/android/**',
    '!**/ios/**',
    '!**/node_modules/**',
    '!**/scripts/**',
    '!**/__test__/**',
  ],
  verbose: true,
  testPathIgnorePatterns: ['/node_modules/'],
};

并添加了此文件:

//mockNativeLibs.js
jest.mock('react-native-firebase', () => {
  return {
    messaging: jest.fn(() => {
      return {
        hasPermission: jest.fn(() => Promise.resolve(true)),
        subscribeToTopic: jest.fn(),
        unsubscribeFromTopic: jest.fn(),
        requestPermission: jest.fn(() => Promise.resolve(true)),
        getToken: jest.fn(() => Promise.resolve('myMockToken')),
      };
    }),
    notifications: jest.fn(() => {
      return {
        onNotification: jest.fn(),
        onNotificationDisplayed: jest.fn(),
      };
    }),
    crashlytics: jest.fn(() => {
      return {
        recordError: jest.fn(),
      };
    }),
  };
});
// apparently there were more libraries causing problems with jest
jest.mock('pushy-react-native', () => {
  return {};
});
jest.mock('react-native-localize', () => {
  return {};
});
jest.mock('react-native-sound', () => {
  return {};
});

相关内容

  • 没有找到相关文章

最新更新