gpt4 book ai didi

react-native - 带有 jest 的 React Native 平台特定测试

转载 作者:行者123 更新时间:2023-12-04 05:14:37 28 4
gpt4 key购买 nike

我正在处理的 RN 项目旨在作为“仅限 android”,因此无需使用 cocoapods 添加诸如“react-native-firebase”之类的库。我必须事先说明,我无法使用任何 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',
},
],
],
};

用于 Jest 测试的虚拟函数:

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

问题是,有没有办法让 jest 跳过 ios 相关的检查,只关注 android 的东西?

更新:显然有一种方法可以覆盖 jest 预设,所以我将其添加到 package.json 中:

"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'
...
}

最佳答案

显然这是一个众所周知的问题。我找到了解决方案 here .基本上我将 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 {};
});

关于react-native - 带有 jest 的 React Native 平台特定测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55023067/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com