我正在使用本机0.59,我想为我的应用编写首次测试。因此,阅读react docs我只需要运行以下命令yart test
即可执行以下文件:
import 'react-native';
import React from 'react';
import App from '../App';
// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';
it('renders correctly', () => {
renderer.create(<App />);
});
这是使用react-native启动应用程序时要测试的默认文件。但是,当我运行yarn test
命令时,测试失败。追溯如下:
FAIL __tests__/App-test.js
● Test suite failed to run
TypeError: (0 , _reactNavigation.createBottomTabNavigator) is not a function
43 |
> 44 | const MainStack = createBottomTabNavigator({
| ^
45 | Home: { screen: Home },
46 | Pets: { screen: Pets },
47 | Notifications: { screen: UserNotifications },
at Object.<anonymous> (src/app-navigator.js:44:63)
at Object.<anonymous> (src/Reducers/navigation.js:2:44)
这是我的app-navigator.js
文件的一部分:
const PetStack = createStackNavigator({
UpdatePet: { screen: UpdatePet },
}, {
mode: 'card',
defaultNavigationOptions: {
headerStyle: { backgroundColor: 'transparent' }
}
});
const MainStack = createBottomTabNavigator({
Home: { screen: Home },
Pets: { screen: Pets },
Notifications: { screen: UserNotifications },
UpdateUser: { screen: UpdateUser },
});
这些是失败的行。太奇怪了,因为当我console.log
createStackNavigator
时它记录了一个模拟函数,但是当我使用createBottomTabNavigator
时它却记录了undefined
。
这是我开玩笑的配置:
"jest": {
"preset": "react-native",
"verbose": true,
"transform": {
"^.+\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js"
},
"globals": {
"NODE_ENV": "test"
},
"moduleFileExtensions": [
"js"
],
"moduleDirectories": [
"src",
"node_modules"
],
"transformIgnorePatterns": [
"/node_modules/(?!native-base|react-native-localize|react-navigation|react-navigation-redux-helpers)/"
],
"setupFiles": [
"./jest-setup.js"
]
}
我已经阅读了一些相关问题,刚刚发现this。他们建议将inlineRequires
中的密钥false
设置为node_modules/react-native/jest/preprocessor.js
,但对我不起作用
答案 0 :(得分:0)
在放置jest.config.js
文件的项目的根目录下创建文件package.json
。
// jest.config.js
const {defaults} = require('jest-config');
module.exports = {
moduleFileExtensions: [...defaults.moduleFileExtensions, 'ts', 'tsx'],
};
有关更多详细信息,请参见this