Skip to content

[bug] Testing docs example doesn't compile when command accepts AppHandle #12077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mAAdhaTTah opened this issue Dec 31, 2024 · 3 comments · May be fixed by tauri-apps/tauri-docs#3255
Open
Labels
status: needs triage This issue needs to triage, applied to new issues type: bug

Comments

@mAAdhaTTah
Copy link

mAAdhaTTah commented Dec 31, 2024

Describe the bug

I am attempting to write tests for my command code, based on the example in the docs. However, when attempting to pass an AppHandle to the command function, I get an error "the trait bound AppHandle: CommandArg<'_, R> is not satisfied".

Reproduction

Slightly modified from the docs:

#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

// Note accessing the AppHandle here!
// Adding this is what's causing the problem.
#[tauri::command]
fn greet(_app: tauri::AppHandle, name: &str) -> String {
    format!("Hello, {}! You've been greeted from Rust!", name)
}

fn create_app<R: tauri::Runtime>(mut builder: tauri::Builder<R>) -> tauri::App<R> {
    builder
        .setup(|app| {
            // do something
            Ok(())
        })
        .invoke_handler(tauri::generate_handler![greet])
        // remove the string argument on your app
        .build(tauri::generate_context!())
        .expect("failed to build app")
}

fn main() {
    tauri::Builder::default()
        .invoke_handler(tauri::generate_handler![greet])
        .run(tauri::generate_context!())
        .unwrap();
}

#[cfg(test)]
mod tests {

    #[test]
    fn something() {
        let data = r#"{"name": "the test"}"#;

        let app = super::create_app(tauri::test::mock_builder());
        let window = tauri::WebviewWindowBuilder::new(&app, "main", Default::default())
            .build()
            .unwrap();
        // do something with the app and window
        // in this case we'll run the my_cmd command with no arguments
        tauri::test::assert_ipc_response(
            &window,
            tauri::webview::InvokeRequest {
                cmd: "greet".into(),
                callback: tauri::ipc::CallbackFn(0),
                error: tauri::ipc::CallbackFn(1),
                url: "http://tauri.localhost".parse().unwrap(),
                body: tauri::ipc::InvokeBody::default(),
                headers: Default::default(),
                invoke_key: tauri::test::INVOKE_KEY.to_string(),
            },
            Ok("Hello, the test! You've been greeted from Rust!"),
        );
    }
}

Expected behavior

Code should compile and successfully run tests.

Full tauri info output

[✔] Environment
    - OS: Mac OS 15.1.1 arm64 (X64)
    ✔ Xcode Command Line Tools: installed
    ✔ rustc: 1.83.0 (90b35a623 2024-11-26)
    ✔ cargo: 1.83.0 (5ffbef321 2024-10-29)
    ✔ rustup: 1.27.1 (54dd3d00f 2024-04-24)
    ✔ Rust toolchain: stable-aarch64-apple-darwin (default)
    - node: 20.17.0
    - pnpm: 9.15.0
    - npm: 10.8.2

[-] Packages
    - tauri 🦀: 2.0.6
    - tauri-build 🦀: 2.0.3
    - wry 🦀: 0.46.3
    - tao 🦀: 0.30.8
    - @tauri-apps/api : 2.1.1
    - @tauri-apps/cli : 2.1.0

[-] Plugins
    - tauri-plugin-store 🦀: 2.2.0
    - @tauri-apps/plugin-store : 2.2.0
    - tauri-plugin-shell 🦀: 2.2.0
    - @tauri-apps/plugin-shell : 2.2.0
    - tauri-plugin-dialog 🦀: 2.2.0
    - @tauri-apps/plugin-dialog : 2.2.0
    - tauri-plugin-fs 🦀: 2.2.0
    - @tauri-apps/plugin-fs : 2.2.0

[-] App
    - build-type: bundle
    - CSP: unset
    - frontendDist: ../dist
    - devUrl: http://localhost:1420/
    - framework: React
    - bundler: Vite```

Stack trace

error[E0277]: the trait bound `AppHandle: CommandArg<'_, R>` is not satisfied
  --> src/welcome/mod.rs:20:1
   |
20 |   #[tauri::command]
   |   ^^^^^^^^^^^^^^^^^ the trait `serde::Deserialize<'_>` is not implemented for `AppHandle`, which is required by `AppHandle: CommandArg<'_, R>`
   |
  ::: src/startup.rs:9:25
   |
9  |           .invoke_handler(tauri::generate_handler![
   |  _________________________-
10 | |             welcome::get_saw_welcome_screen,
11 | |             welcome::set_saw_welcome_screen
12 | |         ])
   | |_________- in this macro invocation
   |
   = help: the following other types implement trait `serde::Deserialize<'de>`:
             `&'a [u8]` implements `serde::Deserialize<'de>`
             `&'a serde_json::value::RawValue` implements `serde::Deserialize<'de>`
             `&'a std::path::Path` implements `serde::Deserialize<'de>`
             `&'a str` implements `serde::Deserialize<'de>`
             `&'p jsonptr::pointer::Pointer` implements `serde::Deserialize<'de>`
             `()` implements `serde::Deserialize<'de>`
             `(T,)` implements `serde::Deserialize<'de>`
             `(T0, T1)` implements `serde::Deserialize<'de>`
           and 352 others
   = note: required for `AppHandle` to implement `CommandArg<'_, R>`
   = note: this error originates in the macro `welcome::__cmd__set_saw_welcome_screen` which comes from the expansion of the macro `tauri::generate_handler` (in Nightly builds, run with -Z macro-backtrace for more info)

Additional context

No response

@mAAdhaTTah mAAdhaTTah added status: needs triage This issue needs to triage, applied to new issues type: bug labels Dec 31, 2024
@mAAdhaTTah mAAdhaTTah changed the title [bug] Testing docs example doesn't when command accepts AppHandle [bug] Testing docs example doesn't compile when command accepts AppHandle Dec 31, 2024
@marclove
Copy link

I'm getting this error as well. Did you ever figure out how to deal with it? @mAAdhaTTah

@marclove
Copy link

marclove commented Apr 13, 2025

Of course I'd figure it out shortly after asking 😆

The trick is a generic Runtime for the AppHandle. Change from:

#[tauri::command]
pub async fn send_message(
    app: AppHandle,

to:

#[tauri::command]
pub async fn send_message<R: Runtime>(
    app: AppHandle<R>,

@mAAdhaTTah
Copy link
Author

I actually didn't figure it out and have mostly just kept Tauri-specific functionality away from what I'm testing but that's very helpful, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs triage This issue needs to triage, applied to new issues type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants