标签: testing-libraryreact-native

测试时,导致 React 状态更新的代码应该包装到 act(...) 中 - 使用简单的 React-native 嵌套屏幕/组件和 jest axios

我是单元测试/玩笑的新手,但我了解一些有关 React Native 的知识。我想为我的主屏幕编写一个测试,其中包含一个发出简单请求的组件。代码运行没有任何问题,但当我用 Jest 运行它时失败。

主屏.js

import { View } from 'react-native'
import APIExample from '@components/Examples/APIExample'
const HomeScreen = () => {
    return (<View> <APIExample /> </View>)
}
export default HomeScreen
Run Code Online (Sandbox Code Playgroud)

HomeScreen.test.js

import { render } from '@testing-library/react-native'
import HomeScreen from '@screens/HomeScreen'

it('should run', async () => {
    const { getByText } = await render(<HomeScreen />)
})
Run Code Online (Sandbox Code Playgroud)

APIExample.js

import { useState, useEffect } from 'react'
import { Text, View } from 'react-native'
import API from '../../API'

const APIExample = …
Run Code Online (Sandbox Code Playgroud)

reactjs jestjs react-native testing-libraryreact-native

2
推荐指数
1
解决办法
2049
查看次数