forked from reactive-python/reactpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
96 lines (71 loc) · 2.23 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
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_TESTING_DEFAULT_TIMEOUT,
)
from reactpy.testing import (
BackendFixture,
DisplayFixture,
capture_reactpy_logs,
clear_reactpy_web_modules_dir,
)
REACTPY_ASYNC_RENDERING.current = True
def pytest_addoption(parser: Parser) -> None:
parser.addoption(
"--headed",
dest="headed",
action="store_true",
help="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_javascript():
subprocess.run(["hatch", "run", "javascript:build"], check=True) # noqa: S607, S603
@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_TESTING_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=not bool(pytestconfig.option.headed))
@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()