Skip to content

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

Open
tauri-apps/tauri-docs
#3255
@mAAdhaTTah

Description

@mAAdhaTTah

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions