Module tauri::test

source ·
Available on crate feature test only.
Expand description

Utilities for unit testing on Tauri applications.

§Stability

This module is unstable.

§Examples

use tauri::test::{mock_builder, mock_context, noop_assets};

#[tauri::command]
fn ping() -> &'static str {
    "pong"
}

fn create_app<R: tauri::Runtime>(builder: tauri::Builder<R>) -> tauri::App<R> {
    builder
        .invoke_handler(tauri::generate_handler![ping])
        // remove the string argument to use your app's config file
        .build(tauri::generate_context!("test/fixture/src-tauri/tauri.conf.json"))
        .expect("failed to build app")
}

fn main() {
    // Use `tauri::Builder::default()` to use the default runtime rather than the `MockRuntime`;
    // let app = create_app(tauri::Builder::default());
    let app = create_app(mock_builder());
    let webview = tauri::WebviewWindowBuilder::new(&app, "main", Default::default()).build().unwrap();

    // run the `ping` command and assert it returns `pong`
    let res = tauri::test::get_ipc_response(
        &webview,
        tauri::webview::InvokeRequest {
            cmd: "ping".into(),
            callback: tauri::ipc::CallbackFn(0),
            error: tauri::ipc::CallbackFn(1),
            body: tauri::ipc::InvokeBody::default(),
            headers: Default::default(),
        },
    ).map(|b| b.deserialize::<String>().unwrap());
}

Structs§

Functions§