Stream: rust-toolchain

Topic: wit-bindgen & unit tests


view this post on Zulip Alex Casalboni (Aug 11 2025 at 10:18):

Hi all :wave: I am struggling to understand how to create unit tests for the wasm32-wasip2 target with wit-bindgen-generated code.

My current understanding is that the generated code only runs with the wasm32 target/arch, so you can't just use the generated code in regular unit tests (when you cargo testyou hit unreachable!()).

So the question is: how do I create unit tests for Rust components? So far we've tried to unit-test the core business logic that doesn't depend on WASI stuff, but that doesn't feel great. We'd like to reach good test coverage. Is there a way to run unit tests with compiled code?

view this post on Zulip Pat Hickey (Aug 11 2025 at 16:47):

in .cargo/config.toml, add

[target.wasm32-wasip2]
runner = "wasmtime run"

and then cargo test --target wasm32-wasip2 will execute the tests in wasmtime.

view this post on Zulip Alex Casalboni (Aug 12 2025 at 09:42):

thank you @Pat Hickey :pray: because I need the http WASI implementation, I managed to get it to work with:

[target.wasm32-wasip2]
runner = "wasmtime run --wasi http=y"

Unfortunately, it doesn't seem possible to generate coverage data via cargo llvm-cov :sad: https://github.com/taiki-e/cargo-llvm-cov/issues/221

Related: https://github.com/rust-lang/rust/issues/81684

I also looked into https://github.com/hknio/wasmcov but that seems out of scope for WASI :thinking:

Any ideas?

I'd really like to be able to run my Web Assembly tests and get coverage reports for them. First issue, can I specify a custom binary to run e.g. instead of cargo test, my test command is wasm-pack...
I'm trying to add support for code coverage to wasm-bindgen tests with the new -Zinstrument-coverage flag, and hitting an error that I only partly understand. I'm building this with anp/moxie@20920...
Automated coverage analysis of WASM executables on embedded and other constrained environments - hknio/wasmcov

view this post on Zulip Pat Hickey (Aug 12 2025 at 15:52):

shorthand there would be wasmtime run -Shttp, glad it worked.

view this post on Zulip Pat Hickey (Aug 12 2025 at 16:16):

and no, I don't have any recommendations for measuring code coverage.


Last updated: Dec 06 2025 at 05:03 UTC