wasm-bindgen icon indicating copy to clipboard operation
wasm-bindgen copied to clipboard

Headless test server port cannot be specified

Open TomWyllie opened this issue 2 months ago • 1 comments

Current situation

When using the test runner in headless mode, the test server starts on a randomly assigned available port. This creates difficulties in configuring network access for the tests being run.

Expected situation

It should be possible to specify the port eiher with an environment variable e.g. WASM_BINDGEN_TEST_ADDRESS, or defaulting to a fixed port.

Context

In the wasm_bindgen_test_runner, the below block of code makes it impossible to guarantee the port that the test server starts on, when using headless mode;

&if headless {
    "127.0.0.1:0".parse().unwrap()
} else if let Ok(address) = std::env::var("WASM_BINDGEN_TEST_ADDRESS") {
    address.parse().unwrap()
} else {
    "127.0.0.1:8000".parse().unwrap()
},

I had unit tests that were failing due to CORS restrictions when fetching from a test server, and without being able to fix the port, specifying the correct Access-Control-Allow-Origin value without using * was impossible. There's a comment in cli/src/wasm_bindgen_test_runner/headless.rs suggesting that starting the browser driver on any available port is to support running tests in parallel, so I suppose that's the rationale here also, though I'm not sure if the tests are actually run in parallel yet.

I would suggest that the test server address in headless mode should respect the environment variable WASM_BINDGEN_TEST_ADDRESS. Happy to submit a PR, or any thoughts from folk if there are compelling reasons to keep this on a randomly assigned port.

TomWyllie avatar Nov 05 '25 13:11 TomWyllie

This change doesn't break existing functionality when WASM_BINDGEN_TEST_ADDRESS isn't set, so LGTM. PRs are welcome!

Spxg avatar Nov 23 '25 15:11 Spxg