首页 > 解决方案 > “测试运行完成后一秒开玩笑没有退出”反应本机中的消息

问题描述

我尝试做一个简单的测试

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;
  }
};

标签: javascriptreact-nativetestingjestjsreact-testing-library

解决方案


推荐阅读