Skip to main content

Module test

Module 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),
            // alternatively use "tauri://localhost"
            url: "http://tauri.localhost".parse().unwrap(),
            body: tauri::ipc::InvokeBody::default(),
            headers: Default::default(),
            invoke_key: tauri::test::INVOKE_KEY.to_string(),
        },
    ).map(|b| b.deserialize::<String>().unwrap());
}

Structs§

EventProxy
MockRuntime
MockRuntimeHandle
MockWebviewDispatcher
MockWindowBuilder
MockWindowDispatcher
NoopAsset
An empty Assets implementation.
RuntimeContext

Constants§

INVOKE_KEY
The invoke key used for tests.

Functions§

assert_ipc_response
Executes the given IPC message and assert the response matches the expected value.
get_ipc_response
Executes the given IPC message and get the return value.
mock_app
Creates a new App for testing using the mock_context with a noop_assets.
mock_builder
Creates a new Builder using the MockRuntime.
mock_context
Creates a new crate::Context for testing.
noop_assets
Creates a new empty Assets implementation.