fix playwright tests, which insist on running an asyncio loop

This commit is contained in:
Masen Furer 2025-01-29 19:00:03 -08:00
parent 8f6dfdef9c
commit 534d38c830
No known key found for this signature in database
GPG Key ID: 2AE2BD5531FF94F4
2 changed files with 26 additions and 5 deletions

View File

@ -3,12 +3,14 @@
from __future__ import annotations from __future__ import annotations
import asyncio import asyncio
import concurrent.futures
import traceback import traceback
from datetime import datetime from datetime import datetime
from pathlib import Path from pathlib import Path
from typing import Any, Callable, Dict, Optional, Type, Union from typing import Any, Callable, Dict, Optional, Type, Union
from urllib.parse import urlparse from urllib.parse import urlparse
from reflex.utils.exec import is_in_app_harness
from reflex.utils.prerequisites import get_web_dir from reflex.utils.prerequisites import get_web_dir
from reflex.vars.base import Var from reflex.vars.base import Var
@ -178,6 +180,23 @@ def compile_state(state: Type[BaseState]) -> dict:
initial_state = state(_reflex_internal_init=True).dict( initial_state = state(_reflex_internal_init=True).dict(
initial=True, include_computed=False initial=True, include_computed=False
) )
try:
_ = asyncio.get_running_loop()
except RuntimeError:
pass
else:
if is_in_app_harness():
# Playwright tests already have an event loop running, so we can't use asyncio.run.
with concurrent.futures.ThreadPoolExecutor() as pool:
resolved_initial_state = pool.submit(
asyncio.run, _resolve_delta(initial_state)
).result()
console.warn(
f"Had to get initial state in a thread 🤮 {resolved_initial_state}",
)
return resolved_initial_state
# Normally the compile runs before any event loop starts, we asyncio.run is available for calling.
return asyncio.run(_resolve_delta(initial_state)) return asyncio.run(_resolve_delta(initial_state))

View File

@ -3,7 +3,7 @@
from typing import Generator from typing import Generator
import pytest import pytest
from playwright.sync_api import Page from playwright.sync_api import Page, expect
from reflex.testing import AppHarness from reflex.testing import AppHarness
@ -87,12 +87,14 @@ def test_table(page: Page, table_app: AppHarness):
table = page.get_by_role("table") table = page.get_by_role("table")
# Check column headers # Check column headers
headers = table.get_by_role("columnheader").all_inner_texts() headers = table.get_by_role("columnheader")
assert headers == expected_col_headers for header, exp_value in zip(headers.all(), expected_col_headers, strict=True):
expect(header).to_have_text(exp_value)
# Check rows headers # Check rows headers
rows = table.get_by_role("rowheader").all_inner_texts() rows = table.get_by_role("rowheader")
assert rows == expected_row_headers for row, expected_row in zip(rows.all(), expected_row_headers, strict=True):
expect(row).to_have_text(expected_row)
# Check cells # Check cells
rows = table.get_by_role("cell").all_inner_texts() rows = table.get_by_role("cell").all_inner_texts()