forked from reactive-python/reactpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
118 lines (88 loc) · 2.96 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from __future__ import annotations
import asyncio
import os
import subprocess
import pytest
from _pytest.config import Config
from _pytest.config.argparsing import Parser
from reactpy.config import (
REACTPY_ASYNC_RENDERING,
REACTPY_DEBUG,
REACTPY_TESTS_DEFAULT_TIMEOUT,
)
from reactpy.testing import (
BackendFixture,
DisplayFixture,
capture_reactpy_logs,
clear_reactpy_web_modules_dir,
)
from reactpy.testing.common import GITHUB_ACTIONS
REACTPY_ASYNC_RENDERING.set_current(True)
REACTPY_DEBUG.set_current(True)
def pytest_addoption(parser: Parser) -> None:
parser.addoption(
"--headless",
dest="headless",
action="store_true",
help="Don't open a browser window when running web-based tests",
)
@pytest.fixture(autouse=True, scope="session")
def install_playwright():
subprocess.run(["playwright", "install", "chromium"], check=True) # noqa: S607, S603
@pytest.fixture(autouse=True, scope="session")
def rebuild():
subprocess.run(["hatch", "build", "-t", "wheel"], check=True) # noqa: S607, S603
@pytest.fixture(autouse=True, scope="function")
def create_hook_state():
"""This fixture is a bug fix related to `pytest_asyncio`.
Usually the hook stack is created automatically within the display fixture, but context
variables aren't retained within `pytest_asyncio` async fixtures. As a workaround,
this fixture ensures that the hook stack is created before each test is run.
Ref: https://github.com/pytest-dev/pytest-asyncio/issues/127
"""
from reactpy.core._life_cycle_hook import HOOK_STACK
token = HOOK_STACK.initialize()
yield token
HOOK_STACK.reset(token)
@pytest.fixture
async def display(server, page):
async with DisplayFixture(server, page) as display:
yield display
@pytest.fixture
async def server():
async with BackendFixture() as server:
yield server
@pytest.fixture
async def page(browser):
pg = await browser.new_page()
pg.set_default_timeout(REACTPY_TESTS_DEFAULT_TIMEOUT.current * 1000)
try:
yield pg
finally:
await pg.close()
@pytest.fixture
async def browser(pytestconfig: Config):
from playwright.async_api import async_playwright
async with async_playwright() as pw:
yield await pw.chromium.launch(
headless=bool(pytestconfig.option.headless) or GITHUB_ACTIONS
)
@pytest.fixture(scope="session")
def event_loop_policy():
if os.name == "nt": # nocov
return asyncio.WindowsProactorEventLoopPolicy()
else:
return asyncio.DefaultEventLoopPolicy()
@pytest.fixture(autouse=True)
def clear_web_modules_dir_after_test():
clear_reactpy_web_modules_dir()
@pytest.fixture(autouse=True)
def assert_no_logged_exceptions():
with capture_reactpy_logs() as records:
yield
try:
for r in records:
if r.exc_info is not None:
raise r.exc_info[1]
finally:
records.clear()