I am testing the app navigation stack, and when rendering a very basic component it times out with the below error though there is no heavy loading in the component.
Error:
Ran all test suites.
error Command failed with exit code 1. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command. nitinpiparava@Nitins-MacBook-Pro chronextapp % yarn run test yarn run v1.22.10 $ jest FAIL tests/AppStack-test.js (11.447 s) AppStack ✕ renders the correct screen (10007 ms)
● AppStack › renders the correct screen
thrown: "Exceeded timeout of 5000 ms for a test.
Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."
21 |
22 | describe('AppStack', () => {
> 23 | it('renders the correct screen', async () => {
| ^
24 | const { getByText } = render(
25 | <NavigationContainer>
26 | <AppStack1 />
at __tests__/AppStack-test.js:23:3
at Object.<anonymous> (__tests__/AppStack-test.js:22:1)
Component
import React from 'react';
import { StyleSheet, Text, View, Button } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
export const Screen1 = ({ navigation }) => (
<View style={styles.container}>
<Text>Screen 1</Text>
<Button title='Go to Screen 2' onPress={() => navigation.push('Screen2')} />
</View>
);
export const Screen2 = () => (
<View style={styles.container}>
<Text>Screen 2</Text>
</View>
);
const Stack = createStackNavigator();
export const AppStack1 = () => (
<Stack.Navigator>
<Stack.Screen name='Screen1' component={Screen1} />
<Stack.Screen name='Screen2' component={Screen2} />
</Stack.Navigator>
);
const SignIn = () => (
<View style={styles.container}>
<Text>Sign In</Text>
</View>
);
const AuthStack = () => (
<Stack.Navigator>
<Stack.Screen name='SignIn' component={SignIn} />
</Stack.Navigator>
);
export default ({ isLoggedIn = true }) => (
<NavigationContainer>
{isLoggedIn ? <AppStack1 /> : <AuthStack />}
</NavigationContainer>
);
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
Ject mock setup file
import 'react-native-gesture-handler/jestSetup';
jest.mock('react-native-reanimated', () => {
const Reanimated = require('react-native-reanimated/mock');
// The mock for `call` immediately calls the callback which is incorrect
// So we override it with a no-op
Reanimated.default.call = () => {};
return Reanimated;
});
//Silence the warning: Animated: `useNativeDriver` is not supported because the native animated module is missing
jest.mock('react-native/Libraries/Animated/NativeAnimatedHelper');
jest.mock('rn-fetch-blob', () => {
return {
__esModule: true,
default: {
fs: {
unlink: jest.fn(),
},
},
};
});
Jest Config
"jest": {
"preset": "react-native",
"moduleNameMapper": {
".+\\.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$": "identity-obj-proxy"
},
"setupFiles": [
"./src/__mocks__/@react-native-async-storage/setup.js"
],
"transform": {
"^.+\\.jsx$": "babel-jest",
"^.+\\.tsx$": "babel-jest"
},
"transformIgnorePatterns": [
"node_modules/(?!@react-native|react-native)"
]
},
"resolutions": {
"react-native/@jest/create-cache-key-function": "^27.0.2"
}