From b82773cef7fc69349a55faf3b79eabc1e4ba4eeb Mon Sep 17 00:00:00 2001 From: Sebastian Silbermann Date: Wed, 15 Jul 2020 14:49:28 +0200 Subject: [PATCH] fix(unmount): Flush useEffect cleanup functions syncronously (#746) --- src/__tests__/render.js | 14 ++++++++++++++ src/pure.js | 6 +++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/__tests__/render.js b/src/__tests__/render.js index 54916ba4..4e78afa6 100644 --- a/src/__tests__/render.js +++ b/src/__tests__/render.js @@ -87,3 +87,17 @@ test('renders options.wrapper around node', () => { `) }) + +test('flushes useEffect cleanup functions sync on unmount()', () => { + const spy = jest.fn() + function Component() { + React.useEffect(() => spy, []) + return null + } + const {unmount} = render() + expect(spy).toHaveBeenCalledTimes(0) + + unmount() + + expect(spy).toHaveBeenCalledTimes(1) +}) diff --git a/src/pure.js b/src/pure.js index be0fa3f1..f2f3438f 100644 --- a/src/pure.js +++ b/src/pure.js @@ -74,7 +74,11 @@ function render( el.forEach(e => console.log(prettyDOM(e, maxLength, options))) : // eslint-disable-next-line no-console, console.log(prettyDOM(el, maxLength, options)), - unmount: () => ReactDOM.unmountComponentAtNode(container), + unmount: () => { + act(() => { + ReactDOM.unmountComponentAtNode(container) + }) + }, rerender: rerenderUi => { render(wrapUiIfNeeded(rerenderUi), {container, baseElement}) // Intentionally do not return anything to avoid unnecessarily complicating the API.