f8d336d711
* Add a test harness to directly execute wasm tests This commits adds a few new crates and infrastructure to enable comands like: cargo test --target wasm32-unknown-unknown The intention here is to make it as low-friction as possible to write wasm tests and also have them execute in a reasonable amount of time. Eventually this is also hopefully enough support to do things like headless testing! For now though this is defintely MVP status rather than fully fleshed out. There's some more information at `crates/test/README.md` about how it works and how to use it, but for now this is mainly intended to play around with locally in this repository for our own tests. * Port a numbe of `js-sys` tests to the new test framework This commit ports a number of existing tests for the `js-sys` crate over to the new test framework created in the previous commit, showing off how they can be executed as well as drastictlly simplifying the tests themselves! This is intended to be a proof of concept for now which we can refine over time. This should also show off that it's possible to incrementally move over to the new test framework. |
||
---|---|---|
.. | ||
src | ||
Cargo.toml | ||
out.sh | ||
README.md |
wasm-bindgen-test
This crate is an experimental test harness for wasm32-unknown-unknown
, with
the goal of allowing you to write tests as you normally do in Rust and then
simply:
cargo test --target wasm32-unknown-unknown
This project is still in the early stages of its development so there's not a ton of documentation just yet, but a taste of how it works is:
-
First, install the test runner.
cargo install --path crates/test-runner
-
Next, add this to your
.cargo/config
:[target.wasm32-unknown-unknown] runner = 'wasm-bindgen-test-runner'
-
Next, configure your project's dev-dependencies:
[dev-dependencies] # or [target.'cfg(target_arch = "wasm32")'.dev-dependencies] wasm-bindgen-test = { git = 'https://github.com/rustwasm/wasm-bindgen' }
-
Next, write some tests!
// in tests/wasm.rs #![feature(use_extern_macros)] extern crate wasm_bindgen_test; use wasm_bindgen_test::*; #[wasm_bindgen_test] fn pass() { assert_eq!(1, 1); } #[wasm_bindgen_test] fn fail() { assert_eq!(1, 2); }
-
And finally, execute your tests:
$ cargo test --target wasm32-unknown-unknown Finished dev [unoptimized + debuginfo] target(s) in 0.11s Running /home/.../target/wasm32-unknown-unknown/debug/deps/wasm-4a309ffe6ad80503.wasm running 2 tests test wasm::pass ... ok test wasm::fail ... FAILED failures: ---- wasm::fail output ---- error output: panicked at 'assertion failed: `(left == right)` left: `1`, right: `2`', crates/test/tests/wasm.rs:14:5 JS exception that was thrown: RuntimeError: unreachable at __rust_start_panic (wasm-function[1362]:33) at rust_panic (wasm-function[1357]:30) at std::panicking::rust_panic_with_hook::h56e5e464b0e7fc22 (wasm-function[1352]:444) at std::panicking::continue_panic_fmt::had70ba48785b9a8f (wasm-function[1350]:122) at std::panicking::begin_panic_fmt::h991e7d1ca9bf9c0c (wasm-function[1351]:95) at wasm::fail::ha4c23c69dfa0eea9 (wasm-function[88]:477) at core::ops::function::FnOnce::call_once::h633718dad359559a (wasm-function[21]:22) at wasm_bindgen_test::__rt::Context::execute::h2f669104986475eb (wasm-function[13]:291) at __wbg_test_fail_1 (wasm-function[87]:57) at module.exports.__wbg_apply_2ba774592c5223a7 (/home/alex/code/wasm-bindgen/target/wasm32-unknown-unknown/wbg-tmp/wasm-4a309ffe6ad80503.js:61:66) failures: wasm::fail test result: FAILED. 1 passed; 1 failed; 0 ignored error: test failed, to rerun pass '--test wasm'
And that's it! You've now got a test harness executing native wasm code inside
of Node.js and you can use cargo test
as you normally would for workflows.
Components
The test harness is made of three separate components, but you typically don't have to worry about most of them. They're documented here for documentation purposes!
wasm-bindgen-test-macro
This crate, living at crates/test-macro
, is a procedural macro that defines
the #[wasm_bindgen_test]
macro. The normal #[test]
cannot be used and will
not work. Eventually it's intended that the #[wasm_bindgen_test]
attribute
could gain arguments like "run in a browser" or something like a minimum Node
version.
For now though the macro is pretty simple and reexported from the next crate,
wasm-bindgen-test
.
wasm-bindgen-test
This is the runtime support needed to execute tests. This is basically the same
thing as the test
crate in the Rust repository, and one day it will likely use
the test
crate itself! For now though it's a minimal reimplementation that
provides the support for:
- Printing what test cases are running
- Collecting
console.log
andconsole.error
output of each test case for printing later - Rendering the failure output of each test case
- Catching JS exceptions so tests can continue to run after a test fails
- Driving execution of all tests
This is the crate which you actually link to in your wasm test and through which
you import the #[wasm_bindgen_test]
macro. Otherwise this crate provides a
console_log!
macro that's a utility like println!
only using console.log
.
This crate may grow more functionality in the future, but for now it's somewhat bare bones!
wasm-bindgen-test-runner
This is where the secret sauce comes into play. We configured Cargo to execute
this binary instead of directly executing the *.wasm
file (which Cargo would
otherwise try to do). This means that whenever a test is executed it executes
this binary with the wasm file as an argument, allowing it to take full control
over the test process!
The test runner is currently pretty simple, executing a few steps:
- First, it runs the equivalent of
wasm-bindgen
. This'll generate wasm-bindgen output in a temoprary directory. - Next, it generates a small shim JS file which imports these wasm-bindgen-generated files and executes the test harness.
- Finally, it executes
node
over the generated JS file, executing all of your tests.
In essence what happens is that this test runner automatically executes
wasm-bindgen
and then uses Node to actually execute the wasm file, meaning
that your wasm code currently runs in a Node environment.
Future Work
Things that'd be awesome to support in the future:
- Arguments to
wasm-bindgen-test-runner
which are the same aswasm-bindgen
, for example--debug
to affect the generated output. - Built-in webserver to
wasm-bindgen-test-runner
. This would be handy for running tests in a browser while developing. - Headless browser testing to allow for testing in a browser on CI.
- Running each test in its own wasm instance to avoid poisoning the environment on panic