diff --git a/packages/react-debug-tools/src/__tests__/ReactHooksInspection-test.js b/packages/react-debug-tools/src/__tests__/ReactHooksInspection-test.js index 7a116f3f42c64..43624ce37affa 100644 --- a/packages/react-debug-tools/src/__tests__/ReactHooksInspection-test.js +++ b/packages/react-debug-tools/src/__tests__/ReactHooksInspection-test.js @@ -276,6 +276,7 @@ describe('ReactHooksInspection', () => { }, }; + let didCatch = false; expect(() => { // mock the Error constructor to check the internal of the error instance try { @@ -288,6 +289,7 @@ describe('ReactHooksInspection', () => { "Cannot read property 'useState' of null", ); } + didCatch = true; }).toErrorDev( 'Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for' + ' one of the following reasons:\n' + @@ -297,6 +299,8 @@ describe('ReactHooksInspection', () => { 'See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.', {withoutStack: true}, ); + // avoid false positive if no error was thrown at all + expect(didCatch).toBe(true); expect(getterCalls).toBe(1); expect(setterCalls).toHaveLength(2); diff --git a/packages/react-debug-tools/src/__tests__/ReactHooksInspectionIntegration-test.js b/packages/react-debug-tools/src/__tests__/ReactHooksInspectionIntegration-test.js index 3edfa32e39185..e57f084975d63 100644 --- a/packages/react-debug-tools/src/__tests__/ReactHooksInspectionIntegration-test.js +++ b/packages/react-debug-tools/src/__tests__/ReactHooksInspectionIntegration-test.js @@ -921,6 +921,8 @@ describe('ReactHooksInspectionIntegration', () => { const renderer = ReactTestRenderer.create(); const childFiber = renderer.root._currentFiber(); + let didCatch = false; + try { ReactDebugTools.inspectHooksOfFiber(childFiber, FakeDispatcherRef); } catch (error) { @@ -934,7 +936,11 @@ describe('ReactHooksInspectionIntegration', () => { '3. You might have more than one copy of React in the same app\n' + 'See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.', ); + didCatch = true; } + // avoid false positive if no error was thrown at all + expect(didCatch).toBe(true); + expect(getterCalls).toBe(1); expect(setterCalls).toHaveLength(2); expect(setterCalls[0]).not.toBe(initial);