From a12d60e6d7bfafe0f3b776776875a9ad40197e3e Mon Sep 17 00:00:00 2001 From: "xiaofeng.mxf" Date: Sun, 25 Jun 2023 18:01:48 +0800 Subject: [PATCH] fix: incorrect actDepth calculation in test-utils --- test-utils/src/index.js | 18 +++++++++++++----- test-utils/test/shared/act.test.js | 12 ++++++++++++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/test-utils/src/index.js b/test-utils/src/index.js index 4bf7a21f77..f8df33393d 100644 --- a/test-utils/src/index.js +++ b/test-utils/src/index.js @@ -32,11 +32,19 @@ export function act(cb) { // been set up. // // If an exception occurs, the outermost `act` will handle cleanup. - const result = cb(); - if (isThenable(result)) { - return result.then(() => { - --actDepth; - }); + try { + const result = cb(); + if (isThenable(result)) { + return result.then(() => { + --actDepth; + }, (e) => { + --actDepth; + throw e; + }) + } + } catch(e) { + --actDepth; + throw e; } --actDepth; return Promise.resolve(); diff --git a/test-utils/test/shared/act.test.js b/test-utils/test/shared/act.test.js index d1f6ef248f..af9823b906 100644 --- a/test-utils/test/shared/act.test.js +++ b/test-utils/test/shared/act.test.js @@ -385,6 +385,12 @@ describe('act', () => { } catch (e) {} }; + const tryNestedRenderBroken = () => { + act(() => { + tryRenderBroken(); + }); + } + describe('synchronously', () => { it('should rethrow the exception', () => { expect(renderBroken).to.throw('BrokenWidget is broken'); @@ -396,6 +402,12 @@ describe('act', () => { expect(scratch.textContent).to.equal('1'); }); + it('should not affect state updates in future renders when nested `act` throws an exception', () => { + tryNestedRenderBroken(); + renderWorking(); + expect(scratch.textContent).to.equal('1'); + }); + it('should not affect effects in future renders', () => { tryRenderBroken(); renderWorking();