Skip to content

Testing with quoin-testkit

How-to Guide1 min read
Edit page

You will test reactive business logic without a window manager or browser. quoin-testkit provides concrete, dependency-light implementations of the core traits designed for tests, so a hook written once runs in a plain cargo test.

Prerequisites: the testkit feature (or a direct dependency on quoin-testkit).

use quoin_core::prelude::*;
use quoin_testkit::{ImmediateExecutor, TestContext};
fn use_counter<C: ReactiveContext>(cx: &C) -> C::Signal<u32> {
cx.create_signal(0u32)
}
let mut cx = TestContext::with_executor(ImmediateExecutor);
let count = use_counter(&cx);
count.set(41);
count.update(|v| *v += 1);
assert_eq!(count.get(), 42);
// Update plumbing is observable:
assert_eq!(cx.updates().count(), 2, "set + update each requested a re-render");

TestContext is a full ReactiveContext with update counting and a shared global store. TestSignal mutates bump the context’s update counter, mirroring the pull-based adapters.

Executor Behavior Use it for
ImmediateExecutor Runs futures inline to completion; fully deterministic, blocking Pure state machines, reducers, derived pipelines
ThreadedExecutor One dedicated thread per future; real concurrency and abort semantics Timers, channels, abort/detach tests
TokioExecutor (feature tokio) Real tokio runtime #[tokio::test] suites; supports paused time via tokio::time::advance

cx.updates() records how many times the UI was asked to re-render. Pair it with cx.batch(|| …) to prove that batches collapse N writes into one update request:

cx.batch(|| {
a.set(1);
b.set(2);
});
assert_eq!(cx.updates().count(), 1);

The repository ships two headless example suites you can read as templates:

  • todo-core — a framework-agnostic todo store: cargo test -p todo-core
  • fetch-core — resource-driven async search: cargo test -p fetch-core

Run the whole headless example suite (including the devtools demo) with just test-headless-examples.