pocket-ic-harness 0.2.0

A test harness for Internet Computer canisters using PocketIC.
Documentation

pocket-ic-harness

license-mit repo-stars downloads latest-version conventional-commits

ci docs

A test harness for Internet Computer canisters using PocketIC.

Overview

pocket-ic-harness provides reusable utilities for integration testing IC canisters with PocketIC:

  • Canister trait - define your canisters and their WASM paths
  • CanisterSetup trait - define how canisters are installed before each test
  • PocketIcTestEnv<S> - generic test environment with canister installation and registry
  • PocketIcClient - typed query/update calls with Candid encoding
  • init_new_agent() - create IC agents against PocketIC endpoints
  • #[pocket_ic_harness::test] - proc-macro for automatic setup/teardown

Quick Start

Define your canisters and setup:

use std::path::Path;
use candid::Encode;
use pocket_ic_harness::{Canister, CanisterSetup, PocketIcTestEnv};

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
enum MyCanister {
    Backend,
}

impl Canister for MyCanister {
    fn as_path(&self) -> &Path {
        match self {
            MyCanister::Backend => Path::new("path/to/backend.wasm.gz"),
        }
    }
}

struct MySetup;

impl CanisterSetup for MySetup {
    type Canister = MyCanister;

    async fn setup(env: &mut PocketIcTestEnv<Self>) {
        let init_arg = Encode!(&()).unwrap();
        env.install_canister(MyCanister::Backend, init_arg).await;
    }
}

Write tests with the proc-macro — canisters are already installed:

#[pocket_ic_harness::test]
async fn test_my_canister(ctx: PocketIcTestEnv<MySetup>) {
    let canister_id = ctx.canister_id(&MyCanister::Backend);
    // test your canister...
}

Get Started

Add to your Cargo.toml:

[dev-dependencies]
pocket-ic-harness = "0.1"

License

Licensed under the MIT license. See LICENSE for details.