Skip to content

Testing apps

calimero-sdk’s testing feature is the harness: your app built as the real calimero:app component, run on the real wasmtime engine with an in-memory host (crates/sdk/src/testing). A passing test means what it means on a node. The harness plays the node’s executor: it applies a call’s changes only when the call ends Ok, seeds each logged run so tests repeat, and resolves event audiences against the org you give it.

[dependencies]
calimero-sdk = { .. }
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
calimero-sdk = { .., features = ["testing"] }
calimero-app-build = { .. }
serde_json = "1"

calimero-app-build builds the crate into a component (wasm32-wasip2) for the test. The component apps in core share a small helper for that (apps/kv-store/tests/common).

apps/kv-store/tests/harness.rs:

use calimero_sdk::testing::{Failure, Org, TestApp};
use serde_json::json;
#[test]
fn set_get_len_remove() {
let mut t = common::app(Org::new().members(["bob"]));
t.as_("owner").call("set", json!({ "key": "k", "value": "v" })).unwrap();
assert_eq!(t.as_("owner").view("get", json!({ "key": "k" })), Ok(json!("v")));
assert_eq!(t.as_("owner").view("len", json!({})), Ok(json!("1")));
}
  • TestApp::new(component, org, now_ms) creates the app; init runs as the owner.
  • Org::new().admins([..]).members([..]).guests([..]).teams([..]) sets the org the app sees through env::org_role and env::in_team.
  • t.as_(account).call(method, args) / .view(..) run a method as account, with camelCase JSON arguments, the same as a client sends. The result is Result<Value, Failure>: Failure::App { code, fields } for an app error.
  • t.events_for(account) lists the events account received, in order.
  • t.anonymous() and t.system() call as a caller who is not signed in and as the node; t.api_key(..) as an API key.
  • Time and timers: t.set_time(ms), t.advance(ms), t.tick(timer).
  • The outside world: t.mock_http(..) answers outbound HTTP, t.mock_app(..) stands in for another app, t.take_xcalls() / t.deliver(..) inspect and deliver cross-app messages, t.add_file(..) / t.file(..) seed and read files.

A scenario is a JSON list of steps that the Rust harness, the TypeScript harness and the node’s cluster runner all run, so one file tests the app everywhere. Each scenarios/<name>.json runs in the org its <name>.org.json names.

[
{ "as": "bob", "op": "call", "method": "getOrInsert",
"args": { "key": "k", "value": "first" },
"expect": { "ok": "first" },
"events": { "bob": ["Inserted"] } }
]
  • op: call, view, tick (a timer, always as the system), advance (milliseconds) or removeMember.
  • as: an account, "@anonymous" or "@system"; "confirmed": true adds the passkey step-up.
  • expect: { "ok": value }, { "error": code, "fields": {..} }, { "trap": .. }, { "confirmRequired": true }, or { "refused": "<API code>" } when the platform refuses before the app runs.
  • "save": "id" stores a result; "$id" later in args or expect is replaced by it. "@bob" is the account bob as the app sees it.
  • events lists, per account, every event name it has received so far.

Run them all from one test (apps/kv-store/tests/scenarios.rs):

use calimero_sdk::testing::run_scenario;
#[test]
fn every_scenario_passes() {
for path in common::scenarios() {
let mut t = common::app(common::org_of(&path));
run_scenario(&mut t, &path).unwrap_or_else(|e| panic!("{}: {e:#}", path.display()));
}
}

Plain #[test]s may also call the app’s state directly on the host target: env::mock stands in for the host there. Actions, cross-app calls and outbound HTTP answer an error outside a component; use TestApp for those.