Skip to content

Commit 79d0f59

Browse files
committed
avoid false positive from try-catch in tests
1 parent 19cb6e5 commit 79d0f59

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

packages/react-debug-tools/src/__tests__/ReactHooksInspection-test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ describe('ReactHooksInspection', () => {
276276
},
277277
};
278278

279+
let didCatch = false;
279280
expect(() => {
280281
// mock the Error constructor to check the internal of the error instance
281282
try {
@@ -288,6 +289,7 @@ describe('ReactHooksInspection', () => {
288289
"Cannot read property 'useState' of null",
289290
);
290291
}
292+
didCatch = true;
291293
}).toErrorDev(
292294
'Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for' +
293295
' one of the following reasons:\n' +
@@ -297,6 +299,8 @@ describe('ReactHooksInspection', () => {
297299
'See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.',
298300
{withoutStack: true},
299301
);
302+
// avoid false positive if no error was thrown at all
303+
expect(didCatch).toBe(true);
300304

301305
expect(getterCalls).toBe(1);
302306
expect(setterCalls).toHaveLength(2);

packages/react-debug-tools/src/__tests__/ReactHooksInspectionIntegration-test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,8 @@ describe('ReactHooksInspectionIntegration', () => {
921921
const renderer = ReactTestRenderer.create(<Foo />);
922922
const childFiber = renderer.root._currentFiber();
923923

924+
let didCatch = false;
925+
924926
try {
925927
ReactDebugTools.inspectHooksOfFiber(childFiber, FakeDispatcherRef);
926928
} catch (error) {
@@ -934,7 +936,11 @@ describe('ReactHooksInspectionIntegration', () => {
934936
'3. You might have more than one copy of React in the same app\n' +
935937
'See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.',
936938
);
939+
didCatch = true;
937940
}
941+
// avoid false positive if no error was thrown at all
942+
expect(didCatch).toBe(true);
943+
938944
expect(getterCalls).toBe(1);
939945
expect(setterCalls).toHaveLength(2);
940946
expect(setterCalls[0]).not.toBe(initial);

0 commit comments

Comments
 (0)