Skip to content

Commit

Permalink
fix: incorrect actDepth calculation in test-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaofeng.mxf committed Jun 25, 2023
1 parent 0ef740b commit a12d60e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
18 changes: 13 additions & 5 deletions test-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
12 changes: 12 additions & 0 deletions test-utils/test/shared/act.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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();
Expand Down

0 comments on commit a12d60e

Please sign in to comment.