Skip to content
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

[bug] assert_ipc_response (Rust tests for icp commands) are broken on 1.6.7 #9960

Closed
ospfranco opened this issue Jun 4, 2024 · 5 comments
Closed
Assignees
Labels
priority: 1 high type: documentation Need to update the API documentation

Comments

@ospfranco
Copy link

ospfranco commented Jun 4, 2024

Hi, I'm trying to write tests for some of the Rust commands that are made available to JS. I was using the function provided by tauri::test::assert_ipc_response according to this SO answer:

fn create_app<R: tauri::Runtime>(mut builder: tauri::Builder<R>) -> tauri::App<R> {
    builder
        .setup(|app| {Ok(())})
        .invoke_handler(tauri::generate_handler![greet])
        .build(tauri::generate_context!())
        .expect("failed to build app")
}


#[cfg(test)]
mod tests {
    use tauri::Manager;
    #[test]
    fn something() {
        let data = r#"{"name": "the test"}"#;
        let app = super::create_app(tauri::test::mock_builder());
        let window = app.get_window("main").unwrap();
        tauri::test::assert_ipc_response(
            &window,
            tauri::InvokePayload {
                cmd: "greet".into(),
                tauri_module: None,
                callback: tauri::api::ipc::CallbackFn(0),
                error: tauri::api::ipc::CallbackFn(1),
                inner: serde_json::from_str(data).unwrap(),
            },
            Ok("Hello, the test! You've been greeted from Rust!"),
        );
    }
}

However, it seems there has been a recent update to the Tauri runtime where an invoke_key is now required. So now the tauri::InvokePayload now requires it and it fails if a matching value to the internal key is not provided:

received ipc message without a __TAURI_INVOKE_KEY__
thread 'nebula::api::tests::should_return_0_with_no_loaded_models' panicked at /Users/osp/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tauri-1.6.7/src/test/mod.rs:318:38:
called `Result::unwrap()` on an `Err` value: InvokeKey
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

How is one supposed to write tests for the exposed Rust code? There is no documentation on how to achieve this.

Edit: This PR added reference to invoke_key everywhere, but there is no way to retrieve it in a test context.

@ospfranco ospfranco added the type: documentation Need to update the API documentation label Jun 4, 2024
@ospfranco ospfranco changed the title [docs] How to write Rust tests for commands? [bug] assert_ipc_response (Rust tests for icp commands) are broken on 1.6.7 Jun 4, 2024
@lucasfernog lucasfernog self-assigned this Jun 4, 2024
@lucasfernog
Copy link
Member

We fixed this issue on v2 but didn't apply the same fix for v1, and it wasn't catch by CI because we applied this patch on a GitHub private fork from an advisory, which doesn't run CI tests by default. A fix will be deployed today.

@lucasfernog
Copy link
Member

fix available on 1.6.8, you now must add invoke_key: Some(tauri::test::INVOKE_KEY.into()), to your payload

@ospfranco
Copy link
Author

Thank you!

@ospfranco
Copy link
Author

ospfranco commented Jun 5, 2024

Also, what about docs, is this the correct way to write tests for the JS commands?

@ospfranco
Copy link
Author

The way issues are closed and then never looked at again is frustrating. In any case, somebody stumbles upon this issue I've done a small write-up on how to write tests for JS commands:

https://ospfranco.com/writting-tests-for-tauri-rust-commands/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: 1 high type: documentation Need to update the API documentation
Projects
None yet
Development

No branches or pull requests

3 participants