javascript - “测试运行完成后一秒开玩笑没有退出”反应本机中的消息
问题描述
我尝试做一个简单的测试
import 'react-native';
import React from 'react';
import App from '../src/App';
import {render} from '@testing-library/react-native';
// Silence the warning https://github.com/facebook/react-native/issues/11094#issuecomment-263240420
jest.mock('react-native/Libraries/Animated/src/NativeAnimatedHelper');
it('renders correctly', async () => {
const {findByText, findAllByText} = render(<App />);
const homeText = await findByText(/home/i);
expect(homeText).toBeTruthy();
});
但是当它运行时,我得到以下消息
Jest did not exit one second after the test run has completed.
This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.
在网络环境中,我看到可以解决停止某些服务的问题mongoose.close()
,例如,但是在 RN idk 中,应该停止解决该消息是什么。请问你能帮帮我吗?
编辑:此图片显示控制台使用--detectOpenHandles
标志
运行
代码如下
HomeScreen.js
import React from 'react';
import {Button, FlatList, Text, TouchableOpacity, View} from 'react-native';
import {useQuery} from 'react-query';
import {routes} from '../utils/routes';
import {getBirras} from '../utils/services';
const HomeScreen = ({navigation}) => {
const {data, error, isLoading} = useQuery('/beers', getBirras);
const itemStyle = {color: 'black', fontSize: 26};
if (isLoading) {
return <Text>Cargando...</Text>;
}
if (error) {
return <Text>Error al cargas birras</Text>;
}
return (
<React.Fragment>
<Text>Home page {data && data.length}</Text>
{data && (
<FlatList
keyExtractor={(item, index) => item.name}
data={data}
renderItem={({item}) => {
return (
<TouchableOpacity
onPress={() =>
navigation.navigate(routes.detailsPage, {id: item.id})
}>
<Text style={itemStyle}>
{item.name} - {item.id}
</Text>
</TouchableOpacity>
);
}}
/>
)}
<Button
title="Go to detail"
onPress={() => navigation.navigate(routes.detailsPage)}
/>
</React.Fragment>
);
};
export default HomeScreen;
DetailScreen.js
import React from 'react';
import {Text, View} from 'react-native';
import {useQuery} from 'react-query';
import {getABirra} from '../utils/services';
const DetailsScreen = ({route}) => {
const {id} = route.params;
const {data, error, isLoading} = useQuery(['detailBirra', id], getABirra);
return (
<View style={{color: 'black'}}>
{data && (
<>
<Text style={{color: 'black'}}>Slogan: {data[0].tagline}</Text>
<Text>Description: {data[0].description}</Text>
</>
)}
</View>
);
};
export default DetailsScreen;
services.js
import Config from 'react-native-config';
export const getBirras = async () => {
try {
const data = await fetch(`${Config.API_URL}/beers`);
return data.json();
} catch (error) {
throw error.response;
}
};
export const getABirra = async ({queryKey}) => {
try {
const data = await fetch(`${Config.API_URL}beers/${queryKey[1]}`);
return data.json();
} catch (error) {
return error.response;
}
};
解决方案
推荐阅读
- php - CKFinder 模态打开主页并且不显示文件
- for-loop - CMD 关闭而不是循环
- python - 在python中连续添加数字
- javascript - 如何使用 json.parse() 将 json 中的键值视为对象而不是字符串?
- javascript - 简化Jquery脚本,动态——二选
- r - 带有 ggplot2 的双比例图:为添加的变量添加图例条目
- javascript - React - 无法在 Heroku 上渲染图像
- android - Android ble 扫描后台扩展广告
- python - 如何在python中模拟电子邮件机密信息
- python - 我们如何加载基于 json 数据创建的配置单元表以使用 spark.sql 触发数据帧?