1111from selenium .webdriver .support import expected_conditions as EC
1212from selenium .webdriver .support .ui import WebDriverWait
1313
14- from reflex .testing import AppHarness
14+ from reflex .testing import AppHarness , AppHarnessProd
1515
1616
1717def TestApp ():
@@ -26,6 +26,8 @@ class TestAppConfig(rx.Config):
2626 class TestAppState (rx .State ):
2727 """State for the TestApp app."""
2828
29+ react_error : bool = False
30+
2931 def divide_by_number (self , number : int ):
3032 """Divide by number and print the result.
3133
@@ -50,6 +52,18 @@ def index():
5052 on_click = lambda : TestAppState .divide_by_number (0 ), # type: ignore
5153 id = "induce-backend-error-btn" ,
5254 ),
55+ rx .button (
56+ "induce_react_error" ,
57+ on_click = TestAppState .set_react_error (True ), # type: ignore
58+ id = "induce-react-error-btn" ,
59+ ),
60+ rx .box (
61+ rx .cond (
62+ TestAppState .react_error ,
63+ rx .Var .create ({"invalid" : "cannot have object as child" }),
64+ "" ,
65+ ),
66+ ),
5367 )
5468
5569
@@ -152,3 +166,37 @@ def test_backend_exception_handler_during_runtime(
152166 "divide_by_number" in captured_default_handler_output .out
153167 and "ZeroDivisionError" in captured_default_handler_output .out
154168 )
169+
170+
171+ def test_frontend_exception_handler_with_react (
172+ test_app : AppHarness ,
173+ driver : WebDriver ,
174+ capsys ,
175+ ):
176+ """Test calling frontend exception handler during runtime.
177+
178+ Render an object as a react child, which is invalid.
179+
180+ Args:
181+ test_app: harness for TestApp app
182+ driver: WebDriver instance.
183+ capsys: pytest fixture for capturing stdout and stderr.
184+
185+ """
186+ reset_button = WebDriverWait (driver , 20 ).until (
187+ EC .element_to_be_clickable ((By .ID , "induce-react-error-btn" ))
188+ )
189+
190+ reset_button .click ()
191+
192+ # Wait for the error to be logged
193+ time .sleep (2 )
194+
195+ captured_default_handler_output = capsys .readouterr ()
196+ if isinstance (test_app , AppHarnessProd ):
197+ assert "Error: Minified React error #31" in captured_default_handler_output .out
198+ else :
199+ assert (
200+ "Error: Objects are not valid as a React child (found: object with keys \n {invalid})"
201+ in captured_default_handler_output .out
202+ )
0 commit comments