yewstack / yew

Rust / Wasm framework for creating reliable and efficient web applications
https://yew.rs
Apache License 2.0
30.5k stars 1.42k forks source link

Unable to use `yew::tests` because it is `#cfg(test)` #3329

Open andybalaam opened 1 year ago

andybalaam commented 1 year ago

Problem

I am unable to use the yew::tests module in my tests. Sorry if I'm being stupid, but I think it's because the cfg(test) does not get passed on to crates you depend on.

Steps To Reproduce Steps to reproduce the behavior:

$ cargo new yew-app
     Created binary (application) `yew-app` package
$ cd yew-app/
$ vim src/main.rs Cargo.toml
$ cat Cargo.toml 
[package]
name = "yew-app"
version = "0.1.0"
edition = "2021"

[dependencies]
yew = { version = "0.20.0", features = ["csr"] }
$ cat src/main.rs 
fn main() {
    println!("Hello, world!");
}

#[cfg(test)]
mod test {
    use yew::tests::layout_tests;

    #[test]
    fn my_test() {
        // Do something with layout_tests
    }
}
$ cargo test
...
error[E0432]: unresolved import `yew::tests`
 --> src/main.rs:7:14
  |
7 |     use yew::tests::layout_tests;
  |              ^^^^^ could not find `tests` in `yew`

Expected behavior

Because of the information here: https://yew.rs/docs/more/testing I expected to be able to use this module.

Environment:

Questionnaire

rollo-b2c2 commented 11 months ago

I'm having problems with this too and haven't had the time to figure out what magic cargo settings I need to get this to work.

Wouldn't it make sense to just expose regardless of if you're testing or not? The compiler should just remove this dead code from the executable on build anyway (if you don't import it certainly won't be included).