Merged
Conversation
2142951 to
408ee46
Compare
5dc02a0 to
852021e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This adds support for doctests to the wasm-bindgen-test-runner.
But wait
Don't they work already? ...and they have for many years?
I certainly thought so, github issues suggest so, our docs say so, and you can try it at home:
But they were, all of them, deceived, for another doctest was made...
... ok
It took me some quite some time to trace through the layers here in cargo, rustdoc, changes to doctest compilation in the 2024 edition, webassembly instructions, layers of configuration options that are difficult to trace, and the incorrect documentation I met along the way.
But here's the answer:
cargo test --docdoes this:which is a wasm file with a main function that runs that one doctest.
And our runner doesn't run it, instead it helpfully chirps
which no one ever sees, because it also exits with code 0, which tells the layers above it "this doctest passed, no need to print its output"
Instead we need to actually run doctests.
Solution overview
The key insight is that doctests are compiled differently from regular #[wasm_bindgen_test] tests:
The fix detects doctests by checking for a main export when no _wbgt* tests are found, then executes them appropriately based on the configured test mode.
Changes
- Browser main thread mode (run_in_browser)
- Dedicated worker mode (run_in_dedicated_worker)
- Shared worker mode (run_in_shared_worker)
- Service worker mode (run_in_service_worker) - forced to no_modules for Firefox < 147 compatibility
Test plan
Tests are in crates/cli/tests/wasm-bindgen-test-runner/doctests.rs. They build doctests from source using cargo +nightly test --doc with --persist-doctests to capture the generated wasm files, then run wasm-bindgen-test-runner on them.
test_doctest_nodetest_doctest_node_experimentalwasm_bindgen_test_configure!(run_in_node_experimental)test_doctest_browserwasm_bindgen_test_configure!(run_in_browser)test_doctest_dedicated_workerwasm_bindgen_test_configure!(run_in_dedicated_worker)test_doctest_shared_workerwasm_bindgen_test_configure!(run_in_shared_worker)test_doctest_service_workerwasm_bindgen_test_configure!(run_in_service_worker)test_doctest_denoWASM_BINDGEN_USE_DENO=1env varEach test verifies:
notes