【发布时间】:2020-10-11 23:18:54
【问题描述】:
React 告诉我使用 act 测试助手,但我收到以下警告:
Cannot log after tests are done. Did you forget to wait for something async in your test? Attempted to log "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 () => ...);".
这是我的测试:
it('navigates to MainScreen on submit', () => {
const props = createMockNav()
const setUser = jest.fn()
const result = renderer.create(
<MockedProvider mocks={[]}>
<UserContext.Provider value={{ setUser: setUser }}>
<LoginScreen {...props} />
</UserContext.Provider>
</MockedProvider>
)
const testInstance = result.root
const button = testInstance.findByType(Button)
expect(button.props.title).toBe('Login!')
act(async () => {
button.props.onPress()
await wait(0);
expect(props.navigation.navigate).toHaveBeenCalledTimes(1)
})
})
我尝试在 act 调用内部或外部移动东西,但我要么破坏它,要么仍然收到警告。任何帮助表示赞赏。
【问题讨论】:
标签: reactjs react-native jestjs