Browse Source

properly reset app and Client

Falko Schindler 1 year ago
parent
commit
ff1813c601
2 changed files with 11 additions and 1 deletions
  1. 9 0
      nicegui/app.py
  2. 2 1
      tests/conftest.py

+ 9 - 0
nicegui/app.py

@@ -232,3 +232,12 @@ class App(FastAPI):
     def remove_route(self, path: str) -> None:
         """Remove routes with the given path."""
         self.routes[:] = [r for r in self.routes if getattr(r, 'path', None) != path]
+
+    def reset(self) -> None:
+        """Reset app to its initial state. (Useful for testing.)"""
+        self.storage.clear()
+        self._startup_handlers.clear()
+        self._shutdown_handlers.clear()
+        self._connect_handlers.clear()
+        self._disconnect_handlers.clear()
+        self._exception_handlers.clear()

+ 2 - 1
tests/conftest.py

@@ -59,8 +59,9 @@ def reset_globals() -> Generator[None, None, None]:
     importlib.reload(plotly)
     importlib.reload(pyplot)
     Client.instances.clear()
-    globals.app.storage.clear()
+    Client.page_routes.clear()
     Client.index_client = Client(page('/'), shared=True).__enter__()
+    globals.app.reset()
     globals.app.get('/')(Client.index_client.build_response)
     binding.reset()