Skip to content

Cannot run simple unit test with default Rust template #672

Closed
@radu-matei

Description

@radu-matei

The default Rust template for a Spin application sets the default build target in .cargo/config.toml as wasm32-wasi:

[build]
target = "wasm32-wasi"

This make sense in order to ensure cargo build creates a module that Spin can run, but this means that a simple unit test that purely wants to test some logic in the handler cannot be run:

  • with a regular cargo test, this fails because it would try to instantiate a Wasm binary and run the test:
.../target/wasm32-wasi/debug/deps/rust_api-a5d8eced053c53aa.wasm: cannot execute binary file
  • with cargo wasi test, this fails because of host imports that are not available in Wasmtime directly (but are in Spin):
wasm32-wasi/debug/deps/rust_api-a5d8eced053c53aa.wasm`
Error: failed to run main module `.../rust-api/target/wasm32-wasi/debug/deps/rust_api-a5d8eced053c53aa.wasm`

Caused by:
    0: failed to instantiate ".../target/wasm32-wasi/debug/deps/rust_api-a5d8eced053c53aa.wasm"
    1: unknown import: `wasi_experimental_http::req` has not been defined

The workaround for this is to remove the default target in .cargo/config.toml and run cargo test — assuming this only tests logic that does not have anything to do with Wasm or Spin functionality.

Because in the default Rust template we have a build command that sets the target to wasm32-wasi, do we want to remove the default target in the Cargo configuration file so testing with cargo test works for simple unit tests?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions