gpt4 book ai didi

react-native - 无法让 Jest expo 应用程序与 react-navigation 一起使用

转载 作者:行者123 更新时间:2023-12-05 07:16:41 33 4
gpt4 key购买 nike

我正在尝试使用 Jest、Expo、React Navigation 进行快照测试,而我的整个应用程序仅使用 Hook 。我想最终将这些变成 e2e 测试,其中 Jest 单击并快照测试所有内容,但我什至无法 react 导航以进行渲染。我在 expo loader 之后的快照总是显示“null”。我遵循了 expo init 附带的选项卡启动器中的基本示例,但它概述了如何设置模拟的方式根本不适用于我的应用程序。我已经尝试了各种方法,但没有任何效果。

应用.tsx

import { Ionicons } from '@expo/vector-icons';
import { AppLoading } from 'expo';
import { Asset } from 'expo-asset';
import * as Font from 'expo-font';
import React, { useState } from 'react';
import { YellowBox } from 'react-native';
import { Provider as PaperProvider } from 'react-native-paper';
import { useScreens } from 'react-native-screens';
import { Provider as RxProvider } from 'reactive-react-redux';
import { applyMiddleware, compose, createStore } from 'redux';
import thunk from 'redux-thunk';
import SnackBar from './components/UI/Snackbar';
import { socketMiddleware } from './lib/socketMiddleware';
import SwitchNavigator from './navigation/AppNavigator';
import rootReducer from './reducers/rootReducer';
import { theme } from './Theme';

const middleware = [thunk, socketMiddleware()];
const composeEnhancers = (window as any).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
export const store = createStore<iAppState, any, any, any>(
rootReducer,
composeEnhancers(applyMiddleware(...middleware))
);

// Must be called prior to navigation stack rendering
useScreens();

YellowBox.ignoreWarnings(['Require cycle:']);

const App = (props) => {
const [isLoadingComplete, setLoadingComplete] = useState(false);

if (!isLoadingComplete && !props.skipLoadingScreen) {
return (
<AppLoading
startAsync={loadResourcesAsync}
onError={handleLoadingError}
onFinish={() => handleFinishLoading(setLoadingComplete)}
/>
);
} else {
return (
<RxProvider store={store}>
<PaperProvider theme={theme}>
<SwitchNavigator />
<SnackBar />
</PaperProvider>
</RxProvider>
);
}
};

const loadResourcesAsync = async () => {
await Promise.all([
Asset.loadAsync([
//nothing
]),
Font.loadAsync({
...Ionicons.font,
TitilliumText250: require('./assets/fonts/TitilliumText22L-250wt.otf'),
TitilliumText800: require('./assets/fonts/TitilliumText22L-800wt.otf')
})
]);
};

const handleLoadingError = (error: Error) => {
console.warn(error);
};

const handleFinishLoading = (setLoadingComplete) => {
setLoadingComplete(true);
};

export default App;

应用程序测试.tsx:

import React from 'react';
import { Provider as PaperProvider } from 'react-native-paper';
import NavigationTestUtils from 'react-navigation/NavigationTestUtils';
import renderer from 'react-test-renderer';
import App from './App';
import { theme } from './Theme';

jest.mock('expo', () => ({
AppLoading: 'AppLoading'
}));
jest.mock('react-native-screens');
jest.mock('react-native-paper');
jest.mock('redux');
jest.mock('reactive-react-redux');
jest.mock('./navigation/AppNavigator', () => 'SwitchNavigator');

describe('App', () => {
jest.useFakeTimers();

beforeEach(() => {
NavigationTestUtils.resetInternalState();
});

// success
it(`renders the loading screen`, () => {
const tree = renderer.create(<App />).toJSON();
expect(tree).toMatchSnapshot();
});

// this snapshot is always null
it(`renders the root without loading screen`, () => {
const tree = renderer
.create(
<PaperProvider theme={theme}>
<App skipLoadingScreen></App>
</PaperProvider>
)
.toJSON();
expect(tree).toMatchSnapshot();
});
});

/navigation/AppNavigator.tsx:

import { createAppContainer, createSwitchNavigator } from 'react-navigation';
import LoginStack from './LoginStack';
import TabStack from './TabStack';

/** The most root navigator which allocates to the others. */
const SwitchNavigator = createAppContainer(
createSwitchNavigator(
{
LoginStack: LoginStack,
TabStack: TabStack
},
{
initialRouteName: 'LoginStack'
}
)
);

export default SwitchNavigator;

最佳答案

我遇到了类似的问题并通过以下方式找到了解决方案:https://github.com/expo/expo/issues/7155#issuecomment-592681861

似乎 act() 对我来说神奇地工作以阻止它返回 null(不确定如何)

更新您的测试以像这样使用它:

import { act, create } from 'react-test-renderer';

it('renders the root without loading screen', () => {
let tree;

act(() => {
tree = create(
<PaperProvider theme={theme}>
<App skipLoadingScreen></App>
</PaperProvider>
);
});

expect(tree.toJSON()).toMatchSnapshot();
});

注意:当我更改使用“react-test-renderer”导入的方式时,我的测试无法找到 act() 作为函数,只需重新安装 npm 包即可解决此问题!

关于react-native - 无法让 Jest expo 应用程序与 react-navigation 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59144867/

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