Rust + Wasm Testing

I am trying to follow the "Rust and WebAssembly" Tutorial. I am at the point where we are adding tests, Testing Life - Rust and WebAssembly. The problem is that I keep getting an error that it cannot find the wasm_game_of_life crate even through that is the crate the test is in.

tests\web.rs:
//! Test suite for the Web and headless browsers.

#![cfg(target_arch = "wasm32")]

extern crate wasm_bindgen_test;
use wasm_bindgen_test::*;

extern crate wasm_game_of_life;
use wasm_game_of_life::Universe;

wasm_bindgen_test_configure!(run_in_browser);
...

I am getting the following error when I run wasm-pack test --chrome --headless and I cannot figure out why.

PS C:\Users\<user>\Rust\wasm-game-of-life> wasm-pack test --chrome --headless
[INFO]: Checking for the Wasm target...
   Compiling wasm-game-of-life v0.1.0 (C:\Users\mrmal\Rust\wasm-game-of-life)
error[E0463]: can't find crate for `wasm_game_of_life`
 --> tests\web.rs:8:1
  |
8 | extern crate wasm_game_of_life;
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate

For more information about this error, try `rustc --explain E0463`.           
error: could not compile `wasm-game-of-life` (test "web") due to 1 previous error

When you get a “can't find crate” message from rustc, it means rustc can't find the right kind of compiled crate file usable as a dependency. Cargo is supposed to always build those files and provide them to rustc, but it won’t if you instruct it to do something else. In particular, I bet your Cargo.toml contains this:

[lib]
crate-type = ["cdylib"]

cdylib outputs cannot be used as Rust dependencies, so your test cannot depend on your library (as it normally does by default). You need to change this to:

[lib]
crate-type = ["lib", "cdylib"]

You may also remove all of the extern crate declarations; they are not necessary (except in special cases that do not apply here) since the 2018 edition, as external crates (that are known to Cargo) are implicitly declared instead.

3 Likes

Wow, thanks! You hit the nail on the head! I think I'll actually be able to finish this tutorial! I wish the documentation went into a little more detail about these types of things. Most of it has been: copy, paste, and cross my fingers.

running 2 tests
test test_tick ... ok
test pass ... ok

test result: ok. 2 passed; 0 failed; 0 ignored; 0 filtered out; finished in 0.01s