rustwasm / wasm-bindgen

Facilitating high-level interactions between Wasm modules and JavaScript
https://rustwasm.github.io/docs/wasm-bindgen/
Apache License 2.0
7.82k stars 1.08k forks source link

docs(wasm-bindgen-test): testing using private module still works? #3975

Open afifurrohman-id opened 5 months ago

afifurrohman-id commented 5 months ago

Summary

From the docs:

One other difference is that the tests must be in the root of the crate, or within a pub mod. Putting them inside a private module will not work.

But seem this code is just works now:

use web_sys::js_sys::Math;

fn rand() -> usize {
    Math::floor(Math::random() * 10.0) as usize
}

#[cfg(test)]
mod tests {
    use wasm_bindgen_test::wasm_bindgen_test;
    use web_sys::console;
    use wasm_bindgen_test::console_log;

    #[wasm_bindgen_test]
    fn it_works() {
        console::info_1(&"THIS IS WORKS".into());
        console_log!("This is works too");

        let rand = super::rand();

        assert!(rand < 0)
    }
}

Output:

failures:

---- wasm_test_bug::tests::it_works output ----
    log output:
        This is works too

    info output:
        THIS IS WORKS

    error output:
        panicked at src/lib.rs:20:9:
        assertion failed: rand < 0

Additional Details

daxpedda commented 5 months ago

I'm not aware of the historic context here, but this has been working as long as I have known wasm-bindgen.

You can continue relying on it as it won't stop working unless Rust itself changes something.

The documentation has to be updated though, so I will leave this issue open for that.