Skip to content

Commit 1d99f8a

Browse files
authored
feat(core): expose test utils (#4752)
1 parent d33672c commit 1d99f8a

File tree

12 files changed

+412
-72
lines changed

12 files changed

+412
-72
lines changed

.changes/expose-test.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Expose the `test` module behind the `test` Cargo feature.

.github/workflows/lint-core.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ jobs:
5050
clippy:
5151
- { args: '', key: 'empty' }
5252
- {
53-
args: '--features compression,wry,linux-protocol-headers,isolation,custom-protocol,api-all,cli,updater,system-tray,windows7-compat,http-multipart',
53+
args: '--features compression,wry,linux-protocol-headers,isolation,custom-protocol,api-all,cli,updater,system-tray,windows7-compat,http-multipart,test',
5454
key: 'all'
5555
}
5656
- { args: '--features custom-protocol', key: 'custom-protocol' }

.github/workflows/test-core.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ jobs:
5656
key: api-all
5757
}
5858
- {
59-
args: --features compression,wry,linux-protocol-headers,isolation,custom-protocol,api-all,cli,updater,system-tray,windows7-compat,http-multipart,
59+
args: --features compression,wry,linux-protocol-headers,isolation,custom-protocol,api-all,cli,updater,system-tray,windows7-compat,http-multipart,test,
6060
key: all
6161
}
6262

core/tauri/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ features = [
2626
"devtools",
2727
"http-multipart",
2828
"icon-png",
29+
"test",
2930
"dox"
3031
]
3132
rustdoc-args = [ "--cfg", "doc_cfg" ]
@@ -126,6 +127,7 @@ cargo_toml = "0.11"
126127

127128
[features]
128129
default = [ "wry", "compression", "objc-exception" ]
130+
test = []
129131
compression = [ "tauri-macros/compression", "tauri-utils/compression" ]
130132
wry = [ "tauri-runtime-wry" ]
131133
objc-exception = [ "tauri-runtime-wry/objc-exception" ]

core/tauri/src/app.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ pub struct Builder<R: Runtime> {
990990
invoke_handler: Box<InvokeHandler<R>>,
991991

992992
/// The JS message responder.
993-
invoke_responder: Arc<InvokeResponder<R>>,
993+
pub(crate) invoke_responder: Arc<InvokeResponder<R>>,
994994

995995
/// The script that initializes the `window.__TAURI_POST_MESSAGE__` function.
996996
invoke_initialization_script: String,

core/tauri/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//! The following are a list of [Cargo features](https://doc.rust-lang.org/stable/cargo/reference/manifest.html#the-features-section) that can be enabled or disabled:
1212
//!
1313
//! - **wry** *(enabled by default)*: Enables the [wry](https://github.com/tauri-apps/wry) runtime. Only disable it if you want a custom runtime.
14+
//! - **test**: Enables the [`test`] module exposing unit test helpers.
1415
//! - **dox**: Internal feature to generate Rust documentation without linking on Linux.
1516
//! - **objc-exception**: Wrap each msg_send! in a @try/@catch and panics if an exception is caught, preventing Objective-C from unwinding into Rust.
1617
//! - **linux-protocol-headers**: Enables headers support for custom protocol requests on Linux. Requires webkit2gtk v2.36 or above.
@@ -839,8 +840,8 @@ pub(crate) mod sealed {
839840
}
840841
}
841842

842-
/// Utilities for unit testing on Tauri applications.
843-
#[cfg(test)]
843+
#[cfg(any(test, feature = "test"))]
844+
#[cfg_attr(doc_cfg, doc(cfg(feature = "test")))]
844845
pub mod test;
845846

846847
#[cfg(test)]

core/tauri/src/scope/ipc.rs

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,16 @@ impl Scope {
171171
#[cfg(test)]
172172
mod tests {
173173
use super::RemoteDomainAccessScope;
174-
use crate::{api::ipc::CallbackFn, test::MockRuntime, App, InvokePayload, Manager, Window};
174+
use crate::{
175+
api::ipc::CallbackFn,
176+
test::{assert_ipc_response, mock_app, MockRuntime},
177+
App, InvokePayload, Manager, Window,
178+
};
175179

176180
const PLUGIN_NAME: &str = "test";
177181

178182
fn test_context(scopes: Vec<RemoteDomainAccessScope>) -> (App<MockRuntime>, Window<MockRuntime>) {
179-
let app = crate::test::mock_app();
183+
let app = mock_app();
180184
let window = app.get_window("main").unwrap();
181185

182186
for scope in scopes {
@@ -186,44 +190,6 @@ mod tests {
186190
(app, window)
187191
}
188192

189-
fn assert_ipc_response(
190-
window: &Window<MockRuntime>,
191-
payload: InvokePayload,
192-
expected: Result<&str, &str>,
193-
) {
194-
let callback = payload.callback;
195-
let error = payload.error;
196-
window.clone().on_message(payload).unwrap();
197-
198-
let mut num_tries = 0;
199-
let evaluated_script = loop {
200-
std::thread::sleep(std::time::Duration::from_millis(50));
201-
let evaluated_script = window.dispatcher().last_evaluated_script();
202-
if let Some(s) = evaluated_script {
203-
break s;
204-
}
205-
num_tries += 1;
206-
if num_tries == 20 {
207-
panic!("Response script not evaluated");
208-
}
209-
};
210-
let (expected_response, fn_name) = match expected {
211-
Ok(payload) => (payload, callback),
212-
Err(payload) => (payload, error),
213-
};
214-
let expected = format!(
215-
"window[\"_{}\"]({})",
216-
fn_name.0,
217-
crate::api::ipc::serialize_js(&expected_response).unwrap()
218-
);
219-
220-
println!("Last evaluated script:");
221-
println!("{evaluated_script}");
222-
println!("Expected:");
223-
println!("{expected}");
224-
assert!(evaluated_script.contains(&expected));
225-
}
226-
227193
fn app_version_payload() -> InvokePayload {
228194
let callback = CallbackFn(0);
229195
let error = CallbackFn(1);

0 commit comments

Comments
 (0)