Skip to content

Commit ef89f7c

Browse files
committed
fix test lifespan on prod
1 parent fad67cf commit ef89f7c

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

reflex/testing.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from http.server import SimpleHTTPRequestHandler
2424
from importlib.util import find_spec
2525
from pathlib import Path
26-
from typing import TYPE_CHECKING, Any, Literal, TypeVar
26+
from typing import TYPE_CHECKING, Any, Literal, Self, TypeVar
2727

2828
import uvicorn
2929

@@ -131,7 +131,7 @@ def create(
131131
Callable[[], None] | types.ModuleType | str | functools.partial[Any] | None
132132
) = None,
133133
app_name: str | None = None,
134-
) -> AppHarness:
134+
) -> Self:
135135
"""Create an AppHarness instance at root.
136136
137137
Args:
@@ -453,7 +453,7 @@ def consume_frontend_output():
453453
self.frontend_output_thread = threading.Thread(target=consume_frontend_output)
454454
self.frontend_output_thread.start()
455455

456-
def start(self) -> AppHarness:
456+
def start(self) -> Self:
457457
"""Start the backend in a new thread and dev frontend as a separate process.
458458
459459
Returns:
@@ -482,7 +482,7 @@ def get_app_global_source(key: str, value: Any):
482482
return f"{key} = {value!r}"
483483
return inspect.getsource(value)
484484

485-
def __enter__(self) -> AppHarness:
485+
def __enter__(self) -> Self:
486486
"""Contextmanager protocol for `start()`.
487487
488488
Returns:

tests/integration/test_lifespan.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ def index():
9999

100100

101101
@pytest.fixture(
102-
params=[False, True], ids=["no_api_transformer", "mount_api_transformer"]
102+
scope="session",
103+
params=[False, True],
104+
ids=["no_api_transformer", "mount_api_transformer"],
103105
)
104106
def mount_api_transformer(request: pytest.FixtureRequest) -> bool:
105107
"""Whether to use api_transformer in the app.
@@ -113,7 +115,9 @@ def mount_api_transformer(request: pytest.FixtureRequest) -> bool:
113115
return request.param
114116

115117

116-
@pytest.fixture(params=[False, True], ids=["no_fastapi", "mount_cached_fastapi"])
118+
@pytest.fixture(
119+
scope="session", params=[False, True], ids=["no_fastapi", "mount_cached_fastapi"]
120+
)
117121
def mount_cached_fastapi(request: pytest.FixtureRequest) -> bool:
118122
"""Whether to use cached FastAPI in the app (app.api).
119123
@@ -126,22 +130,26 @@ def mount_cached_fastapi(request: pytest.FixtureRequest) -> bool:
126130
return request.param
127131

128132

129-
@pytest.fixture
133+
@pytest.fixture(scope="session")
130134
def lifespan_app(
131-
tmp_path, mount_api_transformer: bool, mount_cached_fastapi: bool
135+
tmp_path_factory: pytest.TempPathFactory,
136+
app_harness_env: type[AppHarness],
137+
mount_api_transformer: bool,
138+
mount_cached_fastapi: bool,
132139
) -> Generator[AppHarness, None, None]:
133140
"""Start LifespanApp app at tmp_path via AppHarness.
134141
135142
Args:
136-
tmp_path: pytest tmp_path fixture
143+
tmp_path_factory: pytest tmp_path_factory fixture
144+
app_harness_env: AppHarness environment
137145
mount_api_transformer: Whether to mount the API transformer.
138146
mount_cached_fastapi: Whether to mount the cached FastAPI app.
139147
140148
Yields:
141149
running AppHarness instance
142150
"""
143-
with AppHarness.create(
144-
root=tmp_path,
151+
with app_harness_env.create(
152+
root=tmp_path_factory.mktemp("lifespan_app"),
145153
app_source=functools.partial(
146154
LifespanApp,
147155
mount_cached_fastapi=mount_cached_fastapi,

0 commit comments

Comments
 (0)