#testing #yew #unit-testing #test-runner

app wasm-test-runner

a binary used to create unit-tests in your wasm files

3 releases

0.1.2 Jul 17, 2023
0.1.1 Jul 15, 2023
0.1.0 Jul 10, 2023

#880 in WebAssembly

40 downloads per month

MIT license

12KB
187 lines

Goal

This crate offer a way to do unit-tests in wasm-based projects. It is a feature that many people want, but it is quite hard to do it right because of the way wasm-bindgen is build.

Usage

First, add wasm-test as a dev-dependency in your Cargo.toml

[dev-dependencies]
wasm-test = {git="https://github.com/rambip/wasm-test"}

In your rust project, you can replace the #[test] invocation by #[wasm_test] like so:

#[cfg(test)]
mod tests {
    use wasm_test::*;

    #[wasm_test]
    fn test_should_pass(){
        assert!(true);
    }

    #[wasm_test]
    #[should_panic]
    fn test_should_fail(){
        assert!(false);
    }
}

Install the special runner with cargo:

cargo install wasm-test-runner

(if it doesn't work, you can add the --target <your target> to cargo install, see here for why)

Then, add this line to the .cargo/config.toml of your project

[build]
target = "wasm32-unknown-unknown"

[target.wasm32-unknown-unknown]
runner = "wasm-test-runner"

Now, you should be able to run your unit-tests as usual !

cargo test

Notes

  • The goal of this crate is to test the logic all the wasm part of your code. That means you can write unit-tests, bun you cannot test javascript code. If you want integration tests with javascript instead, please use wasm-bindgen-test

Todo

  • Show backtrace, at least get the line of the error

Dependencies

~3–15MB
~162K SLoC