React native Can';不要让Jest expo应用程序与react导航一起使用

React native Can';不要让Jest expo应用程序与react导航一起使用,react-native,jestjs,react-navigation,expo,react-test-renderer,React Native,Jestjs,React Navigation,Expo,React Test Renderer,我试图用Jest、Expo、React导航进行快照测试,我的整个应用程序只使用挂钩。我希望最终将这些应用到e2e测试中,Jest在其中点击并快照测试所有内容,但我甚至无法让react导航进行渲染。expo loader之后的快照总是显示“null”。我遵循了expo init附带的tabs starter中的基本示例,但它概述如何设置模拟的方式对我的应用程序根本不起作用。我试过各种各样的方法,但都不管用 App.tsx 从“@expo/vector icons”导入{Ionicons}; 从“e

我试图用Jest、Expo、React导航进行快照测试,我的整个应用程序只使用挂钩。我希望最终将这些应用到e2e测试中,Jest在其中点击并快照测试所有内容,但我甚至无法让react导航进行渲染。expo loader之后的快照总是显示“null”。我遵循了expo init附带的tabs starter中的基本示例,但它概述如何设置模拟的方式对我的应用程序根本不起作用。我试过各种各样的方法,但都不管用

App.tsx

从“@expo/vector icons”导入{Ionicons};
从“expo”导入{AppLoading};
从“expo资产”导入{Asset};
从“expo字体”导入*作为字体;
从“React”导入React,{useState};
从“react native”导入{YellowBox};
从“react native paper”导入{Provider as PaperProvider};
从“react native screens”导入{UseScreences};
从'reactive react redux'导入{Provider as RxProvider};
从“redux”导入{applyMiddleware,compose,createStore};
从“redux thunk”导入thunk;
从“./components/UI/SnackBar”导入SnackBar;
从“./lib/socketMiddleware”导入{socketMiddleware};
从“./navigation/AppNavigator”导入SwitchNavigator;
从“./reducers/rootReducer”导入rootReducer;
从“/theme”导入{theme};
const middleware=[thunk,socketMiddleware()];
const composeEnhancers=(如有窗口)。uuu REDUX_DEVTOOLS_EXTENSION_uu124;uu124;|COMPOSE;
export const store=createStore(
减根剂,
composeEnhancers(applyMiddleware(…中间件))
);
//必须在导航堆栈渲染之前调用
使用屏幕();
ignoreWarnings(['Require cycle:']);
常量应用=(道具)=>{
const[isLoadingComplete,setLoadingComplete]=useState(false);
如果(!isLoadingComplete&&!props.skipLoadingScreen){
返回(
handleFinishLoading(setLoadingComplete)}
/>
);
}否则{
返回(
);
}
};
const loadResourcesAsync=async()=>{
等待承诺([
Asset.loadAsync([
//没什么
]),
Font.loadAsync({
…Ionicons.font,
TitilliumText250:要求('./assets/font/TitilliumText22L-250wt.otf'),
TitilliumText800:需要('./assets/font/TitilliumText22L-800wt.otf')
})
]);
};
const handleLoadingError=(错误:error)=>{
控制台。警告(错误);
};
const handleFinishLoading=(setLoadingComplete)=>{
设置加载完成(真);
};
导出默认应用程序;
App.test.tsx:

从“React”导入React;
从“react native paper”导入{Provider as PaperProvider};
从“react navigation/NavigationTestUtils”导入NavigationTestUtils;
从“反应测试渲染器”导入渲染器;
从“./App”导入应用程序;
从“/theme”导入{theme};
开玩笑的模仿('expo',()=>({
AppLoading:“AppLoading”
}));
笑话:mock(“react-native-screens”);
笑话:mock(“react-native-paper”);
开玩笑的模仿('redux');
开玩笑的模仿(reactive-react-redux);
jest.mock('./navigation/AppNavigator',()=>SwitchNavigator');
描述('App',()=>{
开玩笑。使用faketimers();
在每个之前(()=>{
NavigationTestUtils.resetInternalState();
});
//成功
它(`呈现加载屏幕',()=>{
const tree=renderer.create().toJSON();
expect(tree.toMatchSnapshot();
});
//此快照始终为空
它(`呈现根目录而不加载屏幕',()=>{
常量树=渲染器
.创造(
)
.toJSON();
expect(tree.toMatchSnapshot();
});
});
/导航/AppNavigator.tsx:

从“反应导航”导入{createAppContainer,createSwitchNavigator};
从“/LoginStack”导入LoginStack;
从“/TabStack”导入TabStack;
/**分配给其他用户的最根导航器*/
const SwitchNavigator=createAppContainer(
createSwitchNavigator(
{
LoginStack:LoginStack,
TabStack:TabStack
},
{
initialRouteName:“登录堆栈”
}
)
);
导出默认的SwitchNavigator;

我遇到了类似的问题,并通过以下途径找到了解决方案:

似乎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,create};
它('在不加载屏幕的情况下呈现根',()=>{
让树;
行动(()=>{
树=创建(
);
});
expect(tree.toJSON()).toMatchSnapshot();
});
注意:当我改变使用“react test renderer”导入的方式时,我的测试无法找到作为函数的act(),只需重新安装npm包就解决了这个问题