【发布时间】:2020-04-04 22:39:56
【问题描述】:
在 Android 上运行
所有包都已成功链接。
我试过了
$ ./gradlew clean #in android folder
$ react-native start
$ react-native run-android
然后开玩笑显示错误
不变违规:本机模块不能为空
$ jest
FAIL __tests__/SignInScreen-test.js
● Test suite failed to run
Invariant Violation: Native module cannot be null.
at invariant (node_modules/invariant/invariant.js:40:15)
at new NativeEventEmitter (node_modules/react-native/Libraries/EventEmitter/NativeEventEmitter.js:37:7)
at Object.<anonymous> (node_modules/react-native-reanimated/src/ReanimatedEventEmitter.js:4:16)
at Object.<anonymous> (node_modules/react-native-reanimated/src/core/AnimatedCall.js:2:1)
console.group node_modules/redux-logger/dist/redux-logger.js:1
action persist/PERSIST @ 05:17:04.459
console.log node_modules/redux-logger/dist/redux-logger.js:1
prev state {
authReducer: {
...
我该如何解决这个错误或者有解决方案来测试像 Jest 这样的 React 原生应用程序?
jestSetupFile.js
import mockAsyncStorage from '@react-native-community/async-storage/jest/async-storage-mock';
jest.mock('@react-native-community/async-storage', () => mockAsyncStorage);
jest.mock('react-native-keychain', () => ({
SECURITY_LEVEL_ANY: 'MOCK_SECURITY_LEVEL_ANY',
SECURITY_LEVEL_SECURE_SOFTWARE: 'MOCK_SECURITY_LEVEL_SECURE_SOFTWARE',
SECURITY_LEVEL_SECURE_HARDWARE: 'MOCK_SECURITY_LEVEL_SECURE_HARDWARE',
setGenericPassword: jest.fn().mockResolvedValue(),
getGenericPassword: jest.fn().mockResolvedValue(),
resetGenericPassword: jest.fn().mockResolvedValue(),
}));
package.json
"jest": {
"preset": "react-native",
"setupFiles": [
"./jestSetupFile.js"
]
},
SignInScreen-test.js
import {SignInScreen} from '../screens/SignInScreen';
describe('Test Component using hooks', () => {
test('SignInScreen', () => {
expect(<SignInScreen />).toMatchSnapshot();
});
});
【问题讨论】:
标签: javascript react-native unit-testing jestjs react-native-android