Testing code compiled to WebAssembly

I'm compiling Rust to .wasm, and neither cargo test (with integration tests) nor cargo run --example work as expected. I think the problem is the same in both cases: cargo is trying to execute the .wasm file directly, but it's not an executable as far as my operating system is concerned.

  • Is there a way to change how cargo runs binaries? There are ways to change rustc and rustdoc through configuration and env vars, but I don't see anything similar for the test runner.
  • I suppose I could build the tests and examples with the native target, and explicitly set the target for .wasm when I want that -- or vice versa (which is sort of the case now, as I have set the default target in .cargo/config.toml to wasm32-wasi).
  • Assuming I can't change the test runner, and I don't want to have to remember to set the target appropriately at some point in the process, is there a way to change the target for compiling integration test and example source files? Some invocation of the cfg and/or cfg_attr attributes, for instance?

You can configure a runner like:

runner = "wasmtime"

or in the environment like CARGO_TARGET_WASM32_WASI_RUNNER=wasmtime.

1 Like

Thank you for this! Now that I see it in the documentation, I have no idea how I missed it.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.