jest test is not exiting after test passes with react-test-renderer - react-native

I have a toMatchSnapshot test on a simple component with React Native 0.68.2/jest 29.0. Here is the component SysError:
import React, {useState, useContext, Component} from 'react';
import {Platform, Button, Text, TouchableOpacity } from 'react-native';
export default function SysError({route}) {
const message = route.params.message;
return (
<Text>{message}. System NOT available right now. Try again later</Text>
)
};
Here is the SysError.test.js:
import React from 'react';
import renderer from 'react-test-renderer'; //<<==v17.0.2
import SysError from './SysError';
it ('describe SysError page view with no message', () => {
const route = {params: {message:"noth"}};
const tree = renderer.create(<SysError route={route} />).toJSON();
expect(tree).toMatchSnapshot();
})
After yarn test --detectOpenHandle, test passes but is not exiting by itself. Here is the error for component SysError:
Jest has detected the following 2 open handles potentially keeping Jest from exiting:
● MESSAGEPORT
1 | import React from 'react';
> 2 | import renderer from 'react-test-renderer';
| ^
3 | import SysError from './SysError';
4 |
5 | it ('describe SysError page view with no message', () => {
at node_modules/scheduler/cjs/scheduler.development.js:178:17
at Object.<anonymous> (node_modules/scheduler/cjs/scheduler.development.js:13:3)
at Object.require (node_modules/scheduler/index.js:6:20)
at require (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:19:19)
at Object.<anonymous> (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:13:3)
at Object.require (node_modules/react-test-renderer/index.js:6:20)
at Object.<anonymous> (src/components/app/SysError.test.js:2:1)
at asyncGeneratorStep (node_modules/#babel/runtime/helpers/asyncToGenerator.js:3:24)
at _next (node_modules/#babel/runtime/helpers/asyncToGenerator.js:25:9)
There is no async call in SysError.js and what is missing here?

This seems to be caused by the scheduler package.
It was supposed to be fixed in React 17.1.0 but that never came around. There was even an issue for it.
What you can do, is any of the following:
Upgrade to React 18+ (Expo SDK 46+) and the corresponding RN version.
Use delete global.MessageChannel; which should do in your file where the issue is occurring.
Install the react-16-node-hanging-test-fix with yarn add react-16-node-hanging-test-fix or npm install react-16-node-hanging-test-fix which was made by a React core team member Dan Abramaov. This package essentially does step 2 with greater safety due to some checks and stuff.

Related

import renderer from `react-test-renderer` causes open handler and jest hung

Start to test view for React Native 0.68.2/jest 29/react-test-renderer 17.0.2 app. The test case is simple:
import renderer from 'react-test-renderer'; //<<==import causes open handler and jest can't exit
import Home from './Home'; //<<== test Home page view
it ('describe Home page view', () => {
const home = renderer.create(<Home />).toJSON();
expect(home).toMatchSnapshot();
})
Here is the output of yarn jest --detectOpenHandles:
Jest has detected the following 1 open handle potentially keeping Jest from exiting:
● MESSAGEPORT
> 1 | import renderer from 'react-test-renderer';
| ^
2 | import Home from './Home';
3 |
4 | it ('describe Home page view', () => {
at node_modules/scheduler/cjs/scheduler.development.js:178:17
at Object.<anonymous> (node_modules/scheduler/cjs/scheduler.development.js:13:3)
at Object.require (node_modules/scheduler/index.js:6:20)
at require (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:19:19)
at Object.<anonymous> (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:13:3)
at Object.require (node_modules/react-test-renderer/index.js:6:20)
at Object.<anonymous> (src/components/home/Home.test.js:1:1)
at asyncGeneratorStep (node_modules/#babel/runtime/helpers/asyncToGenerator.js:3:24)
at _next (node_modules/#babel/runtime/helpers/asyncToGenerator.js:25:9)
The jest can exit by itself and has to be terminated forcefully. What caused the error here?

Why am I getting this error if I am importing Navigator library in react native?

I am using react native under expo and have installed the required dependencies to install navigator. This is my code:
import { StatusBar } from 'expo-status-bar';
import { StyleSheet, Text, View, SafeAreaView } from 'react-native';
import Landing from './components/Landing';
import Beach from './components/Beach';
import { createStackNavigator } from '#react-navigation/stack';
export default function App() {
return (
<Landing></Landing>
);
}
If I compile this, I am getting this error:
Error: [BABEL] G:\IntrotoIT\beach-app\node_modules\#react-navigation\stack\src\index.tsx: You gave us a visitor for the node type TSInstantiationExpression but it's not a valid type
at verify (C:\Users\Lenovo\AppData\Roaming\npm\node_modules\expo-cli\node_modules\#babel\traverse\lib\visitors.js:108:13)
at Function.explode (C:\Users\Lenovo\AppData\Roaming\npm\node_modules\expo-cli\node_modules\#babel\traverse\lib\visitors.js:30:3)
at C:\Users\Lenovo\AppData\Roaming\npm\node_modules\expo-cli\node_modules\#babel\core\lib\config\full.js:269:42
at Generator.next (<anonymous>)
at Function.<anonymous> (C:\Users\Lenovo\AppData\Roaming\npm\node_modules\expo-cli\node_modules\#babel\core\lib\gensync-utils\async.js:24:3)
at Generator.next (<anonymous>)
at evaluateSync (C:\Users\Lenovo\AppData\Roaming\npm\node_modules\expo-cli\node_modules\gensync\index.js:251:28)
at Function.sync (C:\Users\Lenovo\AppData\Roaming\npm\node_modules\expo-cli\node_modules\gensync\index.js:89:14)
at sync (C:\Users\Lenovo\AppData\Roaming\npm\node_modules\expo-cli\node_modules\#babel\core\lib\gensync-utils\async.js:67:25)
I seriously don't have clue on how to fix it. Tried deleting node modules and then npm install but it was of no use.
Thanks

How to resolve a NativeModule.RNLocalize is null error in test?

I'm using the package react-native-localize to provide localization in an app. I've already linked the library and it works fine running on a device.
Issue:
When I test a component that imports react-native-localize. I get the error react-native-localize: NativeModule.RNLocalize is null.
In order to resolve this null error I call jest.mock('react-native-localize'); at the top of the test file. But I still get an error pointing to NativeModule.RNLocalize is null. I've also provided the mock as mentioned in the package README to no avail.
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';
import * as RNLocalize from 'react-native-localize';
// mocking the module here with jest.mock
jest.mock('react-native-localize');
it('renders correctly', () => {
renderer.create(<App />);
});
Question:
How can you resolve a NativeModule.RNLocalize is null error in test?
Test Stack Trace:
FAIL __tests__/App-test.js
● Test suite failed to run
react-native-localize: NativeModule.RNLocalize is null. To fix this issue try these steps:
• Run `react-native link react-native-localize` in the project root.
• Rebuild and re-run the app.
• If you are using CocoaPods on iOS, run `pod install` in the `ios` directory and then rebuild and re-run the app. You may also need to re-open Xcode to get the new pods.
• Check that the library was linked correctly when you used the link command by running through the manual installation instructions in the README.
* If you are getting this error while unit testing you need to mock the native module. Follow the guide in the README.
If none of these fix the issue, please open an issue on the Github repository: https://github.com/react-native-community/react-native-localize
16 |
17 | import {NavigationContainer} from '#react-navigation/native';
> 18 | import * as RNLocalize from 'react-native-localize';
| ^
19 | import {Icon} from 'native-base';
20 | import {createStackNavigator} from '#react-navigation/stack';
21 | const Stack = createStackNavigator();
at Object.<anonymous> (node_modules/react-native-localize/lib/commonjs/module.js:17:9)
at Object.<anonymous> (node_modules/react-native-localize/lib/commonjs/index.js:3:1)
at Object.<anonymous> (src/modules/AppView.js:18:1)
at Object.<anonymous> (src/modules/AppViewContainer.js:2:1)
at Object.<anonymous> (App.js:23:1)
at Object.<anonymous> (__tests__/App-test.js:7:1)
I fixed this issue in my tests by adding these lines in my jest configuration file
jest.mock("react-native-localize", () => {
return {
getLocales: jest.fn(),
// you can add other functions mock here that you are using
};
});
I found the solution :- paste this code in your jest configuration file (setup.js)
jest.mock("react-native-localize", () => {
getLocales: jest.fn(), // use getLocales if you have used this, else use the one that you have used
// you can add other functions mock here that you are using
};
});

Unable to resolve module 'react-redux'...module 'react-redux' does not exist in haste module map

The code in app.js file with import statements for react redux is created to display a header with a text called "Tech stack".
App.js
import React from 'react';
import { View } from 'react-native';
import { Header } from './components/common';
import { Provider } from 'react-redux';
import { createStore } from 'redux';
import reducers from './reducers';
const App = () => {
return(
<Provider store={createStore(reducers)}>
<View>
<Header headerText="Tech Stack" />
</View>
</Provider>
);
};
export default App;
This is the index file
index.js
import { AppRegistry } from 'react-native';
import App from './src/App';
AppRegistry.registerComponent(tech_stack, () => App);
While running this on the terminal, it throws a error saying unable to resolve module 'react-redux'.
Close the JS bundle(a terminal starts when you run app for first time) and rerun it using the command react-native start from the project path. Basically, you need to rerun the JS bundle after every package installation.
Install 'react-redux' by using
npm install --save react-redux
if you not installed it. By seeing your comment as you mentioned that you installed redux only.
Then restart the JS bundle by using
react-native start --reset-cache
I've faced this issue just now. actually, Our (you and I) main issue is naming the global state folder name is redux and the bundler falls in a conflict because there is a folder inside node_modules that name is redux too.
In fact, the main issue is this line:
import { createStore } from 'redux';
I renamed the resux stuffs folder to reduxStore instead of redux and then everything works properly.
This error occurs because you do not have Redux-Thunk middleware installed on your application. To install run:
npm install redux-thunk
Then, restart your application:
react-native start
More information: https://github.com/reduxjs/redux-thunk
I just ran into this problem and solved it by installing redux in addition to react-redux. Apparently, react-redux requires it.

React styleguidist error with create react app

I am trying to add React Styleguidist to my project and am getting an "Unexpected token" compiler error.
I am using the Create react app to create the project. After it didn't work I created a new project and continue to get the same error when returning a component.
Here is the code for the simple component that I created to try to figure it out:
import React from 'react';
import React from 'react';
const Component1 = (props)=><div>Test</div>;
export default Component1;
Based on what I read at https://react-styleguidist.js.org/docs/getting-started.html it looks like I should only need to run npm install --save-dev react-styleguidist and then npx styleguidist server
I am sure that I am missing something, but have not been able to find anything that would explain the error below.
SyntaxError: /Users/seanlynch/Projects/style/src/Components/Component1.js: Unexpected token (3:28)
1 | import React from 'react';
2 |
> 3 | const Component1 = (props)=><div>Test</div>;
| ^
4 |
5 |
6 | export default Component1;
# ./node_modules/react-styleguidist/lib/index.js (./node_modules/react-styleguidist/loaders/styleguide-loader.js!./node_modules/react-styleguidist/lib/index.js) 46:30-101
# ./node_modules/react-styleguidist/lib/index.js
# multi ./node_modules/react-styleguidist/lib/index ./node_modules/react-styleguidist/node_modules/react-dev-utils/webpackHotDevClient.js
add styleguide.config.js with follow lines
module.exports = {
propsParser: require("react-docgen").parse,
webpackConfig: require("react-scripts/config/webpack.config"),
};