tauri-apps / tauri-docs

The source for all Tauri project documentation.
https://tauri.app
MIT License
749 stars 512 forks source link

docs improvements for test module #2094

Open ospfranco opened 2 months ago

ospfranco commented 2 months ago

Describe the bug

I'm trying to write some tests for the Rust backend. In those tests I'm trying to create instances of a mock api so I can tests the bindings to JS are properly setup:

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use std::path::Path;

    fn setup<R: tauri::Runtime>(
        builder: tauri::Builder<R>,
    ) -> Result<tauri::App<R>, std::io::Error> {
        let app = builder
            .plugin(super::init())
            .build(tauri::generate_context!())
            .unwrap();

        ...

        Ok(app)
    }

    #[test]
    fn should_drop_model() {
        let app = setup(tauri::test::mock_builder()).unwrap();
        let app_data_dir = app.handle().path_resolver().app_data_dir().unwrap();
        ...
    }

    #[test]
    fn should_drop_model2() {
        let app = setup(tauri::test::mock_builder()).unwrap();
        let app_data_dir = app.handle().path_resolver().app_data_dir().unwrap();
        ...
    }
}

However, whenever I try to run my tests I get the following error:

error: symbol `_EMBED_INFO_PLIST` is already defined
   --> src/nebula/nebula.rs:352:20
    |
352 |             .build(tauri::generate_context!())
    |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: this error originates in the macro `$crate::embed_info_plist_bytes` which comes from the expansion of the macro `tauri::generate_context` (in Nightly builds, run with -Z macro-backtrace for more info)

There is not a lot of documentation on how to achieve this but I can assume that creating multiple instances of a tauri app is not supported?

Reproduction

No response

Expected behavior

No response

Full tauri info output

~/Developer/pulsar (osp/vss*) » yarn tauri info                                                                                                                                                                                           130 ↵ osp@Oscars-MBP

[✔] Environment
    - OS: Mac OS 14.4.1 X64
    ✔ Xcode Command Line Tools: installed
    ✔ rustc: 1.77.2 (25ef9e3d8 2024-04-09)
    ✔ cargo: 1.77.2 (e52e36006 2024-03-26)
    ✔ rustup: 1.27.0 (bbb9276d2 2024-03-08)
    ✔ Rust toolchain: stable-aarch64-apple-darwin (default)
    - node: 18.20.2
    - yarn: 4.1.0
    - npm: 10.5.0

[-] Packages
    - tauri [RUST]: 1.6.2
    - tauri-build [RUST]: 1.5.1
    - wry [RUST]: 0.24.7
    - tao [RUST]: 0.16.8
    - @tauri-apps/api [NPM]: 1.5.3 (outdated, latest: 1.5.4)
    - @tauri-apps/cli [NPM]: 1.5.11 (outdated, latest: 1.5.12)

[-] App
    - build-type: bundle
    - CSP: asset:
    - distDir: ../dist
    - devPath: http://localhost:1420/
    - framework: React
    - bundler: Vite

Stack trace

No response

Additional context

No response

ospfranco commented 2 months ago

I just stumbled upon mock_app():

let app = tauri::test::mock_app();

That seems to work. Would be great to have this documented better.

FabianLars commented 2 months ago

Moved this to the docs repo because i think the whole testing guide needs improvements, however this also needs to be fixed in the rust code docs so it's really an issue in both repos.

ospfranco commented 2 months ago

In order to create an app with the JS bindings I needed to test I had to do it this way, not sure if it is the correct way but it works for me:

let app = tauri::test::mock_builder()
            .plugin(super::init())
            .build(mock_context(noop_assets()))
            .unwrap();