[React] Ensure all React useEffect Effects Run Synchronously in Tests with react-testing-library
Thanks to react-testing-library
our tests are free of implementation details, so when we refactor components to hooks we generally don't need to make any changes to our tests. However, useEffect
is slightly different from componentDidMount
in that it's actually executed asynchronously after the render has taken place. So all of our query tests which relied on the HTTP requests being sent immediately after render are failing. Let's use the flushEffects
utility from react-testing-library
to ensure that the pending effect callbacks are run before we make assertions.
Component code:
import {useContext, useReducer, useEffect} from 'react' import * as GitHub from '../../../github-client' function Query ({query, variables, children, normalize = data => data}) { const client = useContext(GitHub.Context) const defaultState = {loaded: false, fetching: false, data: null, error: null} const [state, setState] = useReducer( (state, newState) => ({...state, ...newState}), defaultState) useEffect(() => { setState({fetching: true}) client .request(query, variables) .then(res => setState({ data: normalize(res), error: null, loaded: true, fetching: false, }), ) .catch(error => setState({ error, data: null, loaded: false, fetching: false, }), ) }, [query, variables]) // trigger the effects when 'query' or 'variables' changes return children(state) } export default Query
Test Code:
import {render as rtlRender, wait, flushEffects} from 'react-testing-library'
import React from 'react' import {render as rtlRender, wait, flushEffects} from 'react-testing-library' import * as GitHubClient from '../../../../github-client' import Query from '../query' const fakeResponse = {fakeData: {}} const fakeClient = {request: jest.fn(() => Promise.resolve(fakeResponse))} beforeEach(() => { fakeClient.request.mockClear() }) function renderQuery({ client = fakeClient, children = jest.fn(() => null), query = '', variables = {}, normalize, ...options } = {}) { const props = {query, variables, children, normalize} const utils = rtlRender( <GitHubClient.Provider client={client}> <Query {...props} /> </GitHubClient.Provider>, options, ) return { ...utils, rerender: options => renderQuery({ container: utils.container, children, query, variables, normalize, ...options, }), client, query, variables, children, } } test('query makes requests to the client on mount', async () => { const {children, client, variables, query} = renderQuery() flushEffects(); expect(children).toHaveBeenCalledTimes(2) expect(children).toHaveBeenCalledWith({ data: null, error: null, fetching: true, loaded: false, }) expect(client.request).toHaveBeenCalledTimes(1) expect(client.request).toHaveBeenCalledWith(query, variables) children.mockClear() await wait() expect(children).toHaveBeenCalledTimes(1) expect(children).toHaveBeenCalledWith({ data: fakeResponse, error: null, fetching: false, loaded: true, }) }) test('does not request if rerendered and nothing changed', async () => { const {children, client, rerender} = renderQuery() flushEffects(); await wait() children.mockClear() client.request.mockClear() rerender() flushEffects(); await wait() expect(client.request).toHaveBeenCalledTimes(0) expect(children).toHaveBeenCalledTimes(1) // does still re-render children. }) test('makes request if rerendered with new variables', async () => { const {client, query, rerender} = renderQuery({ variables: {username: 'fred'}, }) flushEffects(); await wait() client.request.mockClear() const newVariables = {username: 'george'} rerender({variables: newVariables}) flushEffects(); await wait() expect(client.request).toHaveBeenCalledTimes(1) expect(client.request).toHaveBeenCalledWith(query, newVariables) }) test('makes request if rerendered with new query', async () => { const {client, variables, rerender} = renderQuery({ query: `query neat() {}`, }) flushEffects(); await wait() client.request.mockClear() const newQuery = `query nice() {}` rerender({query: newQuery}) flushEffects(); await wait() expect(client.request).toHaveBeenCalledTimes(1) expect(client.request).toHaveBeenCalledWith(newQuery, variables) }) test('normalize allows modifying data', async () => { const normalize = data => ({normalizedData: data}) const {children} = renderQuery({normalize}) flushEffects(); await wait() expect(children).toHaveBeenCalledWith({ data: {normalizedData: fakeResponse}, error: null, fetching: false, loaded: true, }) })
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2017-02-19 [Angular] Difference between ngAfterViewInit and ngAfterContentInit
2017-02-19 [Angular] Difference between ViewChild and ContentChild
2017-02-19 [Angular] @ContentChildren and QueryList
2017-02-19 [Angular] @ContentChild and ngAfterContentInit
2017-02-19 [Angular] Content Projection with ng-content
2016-02-19 [Immutable + AngularJS] Use Immutable .List() for Angular array
2016-02-19 [Protractor] Running tests on multiple browsers