Skip to content

Commit

Permalink
avoid false positive from try-catch in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mondaychen committed Apr 1, 2022
1 parent 19cb6e5 commit 79d0f59
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ describe('ReactHooksInspection', () => {
},
};

let didCatch = false;
expect(() => {
// mock the Error constructor to check the internal of the error instance
try {
Expand All @@ -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' +
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,8 @@ describe('ReactHooksInspectionIntegration', () => {
const renderer = ReactTestRenderer.create(<Foo />);
const childFiber = renderer.root._currentFiber();

let didCatch = false;

try {
ReactDebugTools.inspectHooksOfFiber(childFiber, FakeDispatcherRef);
} catch (error) {
Expand All @@ -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);
Expand Down

0 comments on commit 79d0f59

Please sign in to comment.