下面的测试通过了,但我收到以下警告两次,我不知道为什么。有人可以帮我弄清楚吗?
console.error
Warning: You called act(async () => ...) without await. This could lead to unexpected testing behaviour, interleaving multiple act calls and mixing their scopes. You should - await act(async () => ...);
at printWarning (../../node_modules/react-test-renderer/cjs/react-test-renderer.development.js:120:30)
at error (../../node_modules/react-test-renderer/cjs/react-test-renderer.development.js:92:5)
at ../../node_modules/react-test-renderer/cjs/react-test-renderer.development.js:14953:13
at tryCallOne (../../node_modules/react-native/node_modules/promise/lib/core.js:37:12)
at ../../node_modules/react-native/node_modules/promise/lib/core.js:123:15
at flush (../../node_modules/asap/raw.js:50:29)
import { fireEvent } from '@testing-library/react-native'
import { renderScreen } from 'test/render'
describe('screens/home', () => {
it('should render and redirect to the EventScreen', async () => {
const {
getByA11yLabel,
findByA11yLabel,
findAllByA11yLabel,
toJSON
} = renderScreen('Main')
expect(toJSON()).toMatchSnapshot('Default render')
const title = 'New event'
const titleInput = getByA11yLabel('event.title')
// Change title - sync fn
fireEvent.changeText(titleInput, title)
// Create button should be visible
const createButton = await findByA11yLabel('event.create')
expect(titleInput.props.value).toBe(title)
expect(createButton).toBeTruthy()
expect(toJSON()).toMatchSnapshot('Change title')
// Create event - async fn
fireEvent.press(createButton)
// The app should be redirected to the EventScreen
const titleInputs = await findAllByA11yLabel('event.title')
const upsertButton = await findByA11yLabel('event.upsert')
expect(toJSON()).toMatchSnapshot('Create event')
expect(titleInputs).toHaveLength(2)
expect(titleInputs[0].props.value).toBe('') // @MainScreen
expect(titleInputs[1].props.value).toBe(title) // @EventScreen
expect(upsertButton).toBeTruthy()
})
})
- 据我所知,不需要包装
fireEvent
与act
- link https://callstack.github.io/react-native-testing-library/docs/api/#act
-
findBy*
也自动包裹着act
- link https://callstack.github.io/react-native-testing-library/docs/api-queries#findby
- Related GitHub 中的问题 https://github.com/callstack/react-native-testing-library/issues/379仍然开放
依赖项:
- 反应:16.13.1
- 世博会:39.0.4
- 笑话:26.6.3
- ts-笑话:26.4.4
- 玩笑博览会:39.0.0
- @testing-library/jest-native:3.4.3
- @测试库/反应:11.2.2
- @testing-library/react-native:7.1.0
- 反应测试渲染器:16.13.1
- 打字稿:4.1.2
如果您已用尽所有其他调试工作并且非常确定您的代码编写正确,那么它可能与react-native/jest-preset
替换global.Promise
带有模拟(参见issue https://github.com/facebook/react-native/issues/29303).
在这种情况下,问题的解决方案是覆盖/修补 jest 预设,以首先保存原始的全局 Promise,应用react-native/jest-preset
然后恢复原来的Promise(覆盖模拟版本)。这让我可以使用await
在与渲染无关的测试中,不会触发可怕的
console.error
Warning: You called act(async () => ...) without await. This could lead to unexpected testing behaviour, interleaving multiple act calls and mixing their scopes. You should - await act(async () => ...);
此代码片段显示了执行此补丁的一种方法:https://github.com/sbalay/without_await/commit/64a76486f31bdc41f5c240d28263285683755938 https://github.com/sbalay/without_await/commit/64a76486f31bdc41f5c240d28263285683755938
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)