diff --git a/shared/sx/tests/test_sx_js.py b/shared/sx/tests/test_sx_js.py index dcae45f..135adae 100644 --- a/shared/sx/tests/test_sx_js.py +++ b/shared/sx/tests/test_sx_js.py @@ -20,19 +20,39 @@ SX_TEST_JS = Path(__file__).resolve().parents[2] / "static" / "scripts" / "sx-te def _js_render(sx_text: str, components_text: str = "") -> str: """Run sx.js + sx-test.js in Node and return the renderToString result.""" - # Build a small Node script + import tempfile, os + # Build a small Node script that requires the source files script = f""" - global.document = undefined; // no DOM needed for string render - {SX_JS.read_text()} + globalThis.document = undefined; // no DOM needed for string render + // sx.js IIFE uses (typeof window !== "undefined" ? window : this). + // In Node file mode, `this` is module.exports, not globalThis. + // Patch: make the IIFE target globalThis so Sx is accessible. + var _origThis = this; + Object.defineProperty(globalThis, 'document', {{ value: undefined, writable: true }}); + (function() {{ + var _savedThis = globalThis; + {SX_JS.read_text()} + // Hoist Sx from module.exports to globalThis if needed + if (typeof Sx === 'undefined' && typeof module !== 'undefined' && module.exports && module.exports.Sx) {{ + globalThis.Sx = module.exports.Sx; + }} + }}).call(globalThis); {SX_TEST_JS.read_text()} if ({json.dumps(components_text)}) Sx.loadComponents({json.dumps(components_text)}); var result = Sx.renderToString({json.dumps(sx_text)}); process.stdout.write(result); """ - result = subprocess.run( - ["node", "-e", script], - capture_output=True, text=True, timeout=5, - ) + # Write to temp file to avoid OS arg length limits + fd, tmp = tempfile.mkstemp(suffix=".js") + try: + with os.fdopen(fd, "w") as f: + f.write(script) + result = subprocess.run( + ["node", tmp], + capture_output=True, text=True, timeout=5, + ) + finally: + os.unlink(tmp) if result.returncode != 0: pytest.fail(f"Node.js error:\n{result.stderr}") return result.stdout