1- """Test that the frontend dev server starts on an install without Node ."""
1+ """Test that the frontend starts on installs where bun, not node, is the runtime ."""
22
3+ import contextlib
34import os
45import shutil
5- from collections .abc import Generator
6+ from collections .abc import Iterator
7+ from pathlib import Path
8+ from typing import NoReturn
9+ from urllib .parse import urljoin
610
711import httpx
812import pytest
13+ from packaging import version
14+ from reflex_base import constants
915
1016import reflex .testing as reflex_testing
11- from reflex .testing import AppHarness
17+ from reflex .testing import AppHarness , AppHarnessProd
1218from reflex .utils import js_runtimes , path_ops
1319from reflex .utils .exec import _DEV_CONDITION_FLAG
1420
21+ _APP_TEXT = "nodeless frontend is up"
22+
1523
1624def NodelessApp ():
17- """App whose only requirement is that the dev server serves its page."""
25+ """App whose only requirement is that the frontend serves its page."""
1826 import reflex as rx
1927
2028 def index ():
@@ -37,47 +45,87 @@ def _path_without_node() -> str:
3745 )
3846
3947
40- @pytest .fixture (scope = "module" )
41- def nodeless_app (tmp_path_factory ) -> Generator [AppHarness , None , None ]:
42- """Start NodelessApp with `node` unavailable, as on a bun-only install.
48+ def _require (reason : str ) -> NoReturn :
49+ """Skip a scenario whose runtime is missing, or fail the job that requires it.
4350
4451 Args:
45- tmp_path_factory: pytest tmp_path_factory fixture.
52+ reason: Why the scenario cannot run.
53+ """
54+ if os .environ .get ("REQUIRE_BUN_NODELESS_TEST" ) == "true" :
55+ pytest .fail (reason )
56+ pytest .skip (reason )
4657
47- Yields:
48- The running AppHarness.
58+
59+ @pytest .fixture (params = ["nodeless" , "outdated_node" ])
60+ def bun_runtime (
61+ request : pytest .FixtureRequest , monkeypatch : pytest .MonkeyPatch
62+ ) -> Path :
63+ """Set up an environment where bun is the frontend runtime.
64+
65+ `nodeless` drops node from PATH, as on a bun-only install. `outdated_node`
66+ keeps an unsupported node on PATH, which bun has to take over from.
67+
68+ Args:
69+ request: The pytest request carrying the scenario name.
70+ monkeypatch: The pytest monkeypatch fixture.
71+
72+ Returns:
73+ The bun executable Reflex runs the frontend with.
4974 """
50- with pytest .MonkeyPatch .context () as monkeypatch :
75+ monkeypatch .delenv ("REFLEX_USE_NPM" , raising = False )
76+ node_version = None
77+ if request .param == "nodeless" :
5178 monkeypatch .setenv ("PATH" , _path_without_node ())
5279 if shutil .which ("node" ) is not None :
53- pytest .skip ("node is still resolvable after filtering it off PATH" )
54- if path_ops .get_bun_path () is None :
55- pytest .skip ("bun is unavailable once node is off PATH" )
56- # The node probe is cached per process, so an earlier test may have
57- # found node; it is off PATH now.
58- monkeypatch .setattr (js_runtimes , "get_node_version" , lambda : None )
59- # Drop the `development` condition Reflex puts in the dev server's
60- # environment. Bun applies neither NODE_OPTIONS nor BUN_OPTIONS to the
61- # process it spawns for a package script, so node-less installs never
62- # had that assist -- the condition has to reach bun as a flag on the
63- # dev server command.
64- monkeypatch .setattr (reflex_testing , "_with_development_condition" , dict )
65- harness = AppHarness .create (
66- root = tmp_path_factory .mktemp ("nodeless_app" ), app_source = NodelessApp
67- )
68- # Started outside the `with` form on purpose: `__enter__` *is* `start()`,
69- # so a failure to bring the frontend up skips `__exit__` and leaks the
70- # backend thread, which then keeps the interpreter alive. This test
71- # fails precisely that way, so it has to stop the harness itself.
72- try :
73- harness .start ()
74- yield harness
75- finally :
76- harness .stop ()
80+ _require ("node is still resolvable after filtering it off PATH" )
81+ else :
82+ node_version = js_runtimes ._get_version_of_executable (path_ops .get_node_path ())
83+ if node_version is None :
84+ _require ("node is not installed" )
85+ if node_version >= version .parse (constants .Node .MIN_VERSION ):
86+ _require (f"node { node_version } is supported, not outdated" )
87+ bun_path = path_ops .get_bun_path ()
88+ if bun_path is None :
89+ _require ("bun is not installed" )
90+ # The node probe is cached once per process, possibly under another PATH.
91+ monkeypatch .setattr (js_runtimes , "get_node_version" , lambda : node_version )
92+ # Reflex picks bun, and forces it over an outdated node with `--bun`.
93+ assert js_runtimes .get_js_package_executor ()[0 ] == [
94+ str (bun_path ),
95+ * (["--bun" ] if node_version else []),
96+ ]
97+ return bun_path
98+
99+
100+ @contextlib .contextmanager
101+ def _started (harness_cls : type [AppHarness ], root : Path ) -> Iterator [AppHarness ]:
102+ """Run NodelessApp in a harness that is stopped even when it fails to start.
103+
104+ `__enter__` *is* `start()`, so a failure to bring the frontend up skips
105+ `__exit__` and leaks the backend thread, which then keeps the interpreter
106+ alive.
77107
108+ Args:
109+ harness_cls: The harness class that runs the app.
110+ root: The directory to create the app in.
111+
112+ Yields:
113+ The running harness.
114+ """
115+ harness = harness_cls .create (root = root , app_source = NodelessApp )
116+ try :
117+ harness .start ()
118+ yield harness
119+ finally :
120+ harness .stop ()
78121
79- def test_nodeless_frontend_serves (nodeless_app : AppHarness ):
80- """The dev server starts and serves a page with no Node installed.
122+
123+ def test_dev_frontend_serves (
124+ bun_runtime : Path ,
125+ monkeypatch : pytest .MonkeyPatch ,
126+ tmp_path_factory : pytest .TempPathFactory ,
127+ ):
128+ """The dev server starts and compiles the page with bun as the runtime.
81129
82130 `react-router dev` needs the `development` export condition and relaunches
83131 itself to enable it. Bun runs the CLI here and ignores the condition in the
@@ -87,15 +135,42 @@ def test_nodeless_frontend_serves(nodeless_app: AppHarness):
87135 flag up front.
88136
89137 Args:
90- nodeless_app: The harness running without node on PATH.
138+ bun_runtime: The bun executable the scenario runs the frontend with.
139+ monkeypatch: The pytest monkeypatch fixture.
140+ tmp_path_factory: The pytest temporary path factory.
141+ """
142+ # Drop the `development` condition Reflex puts in the dev server's
143+ # environment. Bun applies neither NODE_OPTIONS nor BUN_OPTIONS to the
144+ # process it spawns for a package script, so the condition has to reach
145+ # bun as a flag on the dev server command.
146+ monkeypatch .setattr (reflex_testing , "_with_development_condition" , dict )
147+ with _started (AppHarness , tmp_path_factory .mktemp ("nodeless_app" )) as harness :
148+ assert harness .frontend_process is not None
149+ args = harness .frontend_process .args
150+ assert isinstance (args , list )
151+ assert args [:2 ] == [str (bun_runtime ), _DEV_CONDITION_FLAG ]
152+ assert harness .frontend_url is not None
153+ document = httpx .get (harness .frontend_url , timeout = 60 )
154+ assert document .status_code == 200
155+ assert "window.__reactRouterContext" in document .text
156+ # The page renders client-side, so check the route module the dev
157+ # server compiles for it.
158+ route = httpx .get (
159+ urljoin (harness .frontend_url , "/app/routes/_index.jsx" ), timeout = 60
160+ )
161+ assert route .status_code == 200
162+ assert _APP_TEXT in route .text
163+
164+
165+ @pytest .mark .usefixtures ("bun_runtime" )
166+ def test_prod_frontend_serves (tmp_path_factory : pytest .TempPathFactory ):
167+ """The production build runs and serves the page with bun as the runtime.
168+
169+ Args:
170+ tmp_path_factory: The pytest temporary path factory.
91171 """
92- assert nodeless_app .frontend_process is not None
93- args = nodeless_app .frontend_process .args
94- assert isinstance (args , list )
95- assert _DEV_CONDITION_FLAG in args
96- assert nodeless_app .frontend_url is not None
97- response = httpx .get (nodeless_app .frontend_url , timeout = 60 )
98- assert response .status_code == 200
99- # The page is hydrated client-side, so assert on the react-router document
100- # the dev server rendered rather than on the app's own text.
101- assert "__reactRouterContext" in response .text
172+ with _started (AppHarnessProd , tmp_path_factory .mktemp ("nodeless_app" )) as harness :
173+ assert harness .frontend_url is not None
174+ response = httpx .get (harness .frontend_url , timeout = 60 )
175+ assert response .status_code == 200
176+ assert _APP_TEXT in response .text
0 commit comments