do not clear all decorated pages during AppHarness tests (#3093)

This commit is contained in:
benedikt-bartscher 2024-04-16 18:17:43 +02:00 committed by GitHub
parent aff0eb664e
commit 3014db1c1c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -124,6 +124,7 @@ class AppHarness:
backend: Optional[uvicorn.Server] = None
state_manager: Optional[StateManagerMemory | StateManagerRedis] = None
_frontends: list["WebDriver"] = dataclasses.field(default_factory=list)
_decorated_pages: list = dataclasses.field(default_factory=list)
@classmethod
def create(
@ -230,11 +231,15 @@ class AppHarness:
with chdir(self.app_path):
# ensure config and app are reloaded when testing different app
reflex.config.get_config(reload=True)
# Clean out any `rx.page` decorators from other tests.
reflex.app.DECORATED_PAGES.clear()
# Save decorated pages before importing the test app module
before_decorated_pages = reflex.app.DECORATED_PAGES.copy()
# Ensure the AppHarness test does not skip State assignment due to running via pytest
os.environ.pop(reflex.constants.PYTEST_CURRENT_TEST, None)
self.app_module = reflex.utils.prerequisites.get_compiled_app(reload=True)
# Save the pages that were added during testing
self._decorated_pages = [
p for p in reflex.app.DECORATED_PAGES if p not in before_decorated_pages
]
self.app_instance = self.app_module.app
if isinstance(self.app_instance._state_manager, StateManagerRedis):
# Create our own redis connection for testing.
@ -396,6 +401,10 @@ class AppHarness:
for driver in self._frontends:
driver.quit()
# Cleanup decorated pages added during testing
for page in self._decorated_pages:
reflex.app.DECORATED_PAGES.remove(page)
def __exit__(self, *excinfo) -> None:
"""Contextmanager protocol for `stop()`.