Skip to content

PocketIc

Defined in: packages/pic/src/pocket-ic.ts:118

This class represents the main PocketIC client. It is responsible for interacting with the PocketIC server via the REST API. See PocketIcServer for details on the server to use with this client.

The easist way to use PocketIC is to use setupCanister convenience method:

import { PocketIc, PocketIcServer } from '@dfinity/pic';
import { _SERVICE, idlFactory } from '../declarations';
const wasmPath = resolve('..', '..', 'canister.wasm');
const picServer = await PocketIcServer.start();
const pic = await PocketIc.create(picServer.getUrl());
const fixture = await pic.setupCanister<_SERVICE>({ idlFactory, wasmPath });
const { actor } = fixture;
// perform tests...
await pic.tearDown();
await picServer.stop();

If more control is needed, then the createCanister, installCode and createActor methods can be used directly:

import { PocketIc, PocketIcServer } from '@dfinity/pic';
import { _SERVICE, idlFactory } from '../declarations';
const wasm = resolve('..', '..', 'canister.wasm');
const picServer = await PocketIcServer.start();
const pic = await PocketIc.create(picServer.getUrl());
const canisterId = await pic.createCanister();
await pic.installCode({ canisterId, wasm });
const actor = pic.createActor<_SERVICE>({ idlFactory, canisterId });
// perform tests...
await pic.tearDown();
await picServer.stop();

addCycles(canisterId, amount): Promise<bigint>

Defined in: packages/pic/src/pocket-ic.ts:1558

Add cycles to the specified canister.

Principal

The Principal of the canister to add cycles to.

bigint

The amount of cycles to add.

Promise<bigint>

The new cycle balance of the canister.

Principal

import { Principal } from '@icp-sdk/core/principal';
import { PocketIc, PocketIcServer } from '@dfinity/pic';
const canisterId = Principal.fromUint8Array(new Uint8Array([0]));
const picServer = await PocketIcServer.start();
const pic = await PocketIc.create(picServer.getUrl());
const newCyclesBalance = await pic.addCycles(canisterId, BigInt(10_000_000));
await pic.tearDown();
await picServer.stop();

advanceCertifiedTime(duration): Promise<void>

Defined in: packages/pic/src/pocket-ic.ts:1285

Advance the time of the IC by the given duration in milliseconds and immediately have query calls and read state requests reflect the new time.

Use advanceTime to advance time without immediately reflecting the new time.

number

The duration to advance the time by.

Promise<void>

import { PocketIc, PocketIcServer } from '@dfinity/pic';
const picServer = await PocketIcServer.start();
const pic = await PocketIc.create(picServer.getUrl());
const initialTime = await pic.getTime();
await pic.advanceCertifiedTime(1_000);
const newTime = await pic.getTime();
await pic.tearDown();
await picServer.stop();

advanceTime(duration): Promise<void>

Defined in: packages/pic/src/pocket-ic.ts:1254

Advance the time of the IC by the given duration in milliseconds. tick should be called after calling this method in order for query calls and read state requests to reflect the new time.

Use advanceCertifiedTime to advance time and immediately have query calls and read state requests reflect the new time.

number

The duration to advance the time by.

Promise<void>

import { PocketIc, PocketIcServer } from '@dfinity/pic';
const picServer = await PocketIcServer.start();
const pic = await PocketIc.create(picServer.getUrl());
const initialTime = await pic.getTime();
await pic.advanceTime(1_000);
await pic.tick();
const newTime = await pic.getTime();
await pic.tearDown();
await picServer.stop();

canisterStatus(options): Promise<CanisterStatusResult>

Defined in: packages/pic/src/pocket-ic.ts:687

Returns the status of the given canister.

CanisterStatusOptions

Options for querying the canister status, see CanisterStatusOptions.

Promise<CanisterStatusResult>

The canister status, see CanisterStatusResult.

Principal

import { Principal } from '@icp-sdk/core/principal';
import { PocketIc, PocketIcServer } from '@dfinity/pic';
const canisterId = Principal.fromUint8Array(new Uint8Array([0]));
const picServer = await PocketIcServer.start();
const pic = await PocketIc.create(picServer.getUrl());
const status = await pic.canisterStatus({ canisterId });
await pic.tearDown();
await picServer.stop();

createActor<T>(options): Actor<T>

Defined in: packages/pic/src/pocket-ic.ts:768

Creates an Actor for the given canister. An Actor is a typesafe class that implements the Candid interface of a canister. To create a canister for the Actor, see createCanister. For a more convenient way of creating a PocketIC instance, creating a canister and installing code, see setupCanister.

T extends ActorInterface<T> = object

The type of the Actor. Must implement ActorInterface.

CreateActorOptions

Options for creating the Actor, see CreateActorOptions.

Actor<T>

The Actor instance.

import { resolve } from 'node:path';
import { PocketIc, PocketIcServer, generateRandomIdentity } from '@dfinity/pic';
import { _SERVICE, idlFactory } from '../declarations/backend.did';
const wasm = resolve('..', '..', 'canister.wasm');
const alice = generateRandomIdentity();
const picServer = await PocketIcServer.start();
const pic = await PocketIc.create(picServer.getUrl());
const canisterId = await pic.createCanister();
await pic.installCode({ canisterId, wasm });
const actor = pic.createActor<_SERVICE>({
idlFactory,
canisterId,
sender: alice.getPrincipal(),
});
await pic.tearDown();
await picServer.stop();

createCanister(options?): Promise<Principal>

Defined in: packages/pic/src/pocket-ic.ts:252

Creates a new canister. For a more convenient way of creating a PocketIC instance, creating a canister and installing code, see setupCanister.

CreateCanisterOptions = {}

Options for creating the canister, see CreateCanisterOptions.

Promise<Principal>

The Principal of the newly created canister.

Principal

import { PocketIc, PocketIcServer } from '@dfinity/pic';
const picServer = await PocketIcServer.start();
const pic = await PocketIc.create(picServer.getUrl());
const canisterId = await pic.createCanister();
await pic.tearDown();
await picServer.stop();

createDeferredActor<T>(options): DeferredActor<T>

Defined in: packages/pic/src/pocket-ic.ts:811

Creates a DeferredActor for the given canister. A DeferredActor is a typesafe class that implements the Candid interface of a canister.

A DeferredActor in contrast to a normal Actor will submit the call to the PocketIc replica, but the call will not be executed immediately. Instead, the calls are queued and a Promise is returned by the DeferredActor that can be awaited to process the pending canister call.

To create a canister for the DeferredActor, see createCanister. For a more convenient way of creating a PocketIC instance, creating a canister and installing code, see setupCanister.

T extends ActorInterface<T> = object

The type of the DeferredActor. Must implement ActorInterface.

CreateActorOptions

Options for creating the DeferredActor, see CreateActorOptions.

DeferredActor<T>

The DeferredActor instance.

const deferredActor = pic.createDeferredActor<_SERVICE>({
idlFactory,
canisterId,
});
const executeCall = await deferredActor.greet('PicJS');
// other calls or ticks...
const response = await executeCall();

fetchCanisterLogs(options): Promise<CanisterLogRecord[]>

Defined in: packages/pic/src/pocket-ic.ts:918

Fetches the log records of the given canister, e.g. to inspect traps in timers or heartbeats. Only controllers can read a canister’s logs unless its log visibility allows otherwise, see FetchCanisterLogsOptions.sender.

FetchCanisterLogsOptions

Options for fetching canister logs, see FetchCanisterLogsOptions.

Promise<CanisterLogRecord[]>

The canister’s log records, see CanisterLogRecord.

import { PocketIc, PocketIcServer, generateRandomIdentity } from '@dfinity/pic';
const controller = generateRandomIdentity();
const picServer = await PocketIcServer.start();
const pic = await PocketIc.create(picServer.getUrl());
const canisterId = await pic.createCanister({
sender: controller.getPrincipal(),
controllers: [controller.getPrincipal()],
});
// install and call the canister...
const logs = await pic.fetchCanisterLogs({
canisterId,
sender: controller.getPrincipal(),
});
const messages = logs.map(log => new TextDecoder().decode(log.content));
await pic.tearDown();
await picServer.stop();

getApplicationSubnets(): Promise<SubnetTopology[]>

Defined in: packages/pic/src/pocket-ic.ts:1484

Get all application subnet topologies for this instance’s network. The instance network topology is configured via the create method.

Promise<SubnetTopology[]>

An array of subnet topologies for each application subnet that exists on this instance’s network.


getBitcoinSubnet(): Promise<SubnetTopology | undefined>

Defined in: packages/pic/src/pocket-ic.ts:1400

Get the Bitcoin subnet topology for this instance’s network. The instance network topology is configured via the create method.

Promise<SubnetTopology | undefined>

The subnet topology for the Bitcoin subnet, if it exists on this instance’s network.


getCanisterSubnetId(canisterId): Promise<Principal | null>

Defined in: packages/pic/src/pocket-ic.ts:1339

Gets the subnet Id of the provided canister Id.

Principal

The Principal of the canister to get the subnet Id of.

Promise<Principal | null>

The canister’s subnet Id if the canister exists, null otherwise.

Principal

import { PocketIc, PocketIcServer } from '@dfinity/pic';
const canisterId = Principal.fromUint8Array(new Uint8Array([0]));
const picServer = await PocketIcServer.start();
const pic = await PocketIc.create(picServer.getUrl());
const subnetId = await pic.getCanisterSubnetId(canisterId);
await pic.tearDown();
await picServer.stop();

getControllers(canisterId): Promise<Principal[]>

Defined in: packages/pic/src/pocket-ic.ts:1068

Get the controllers of the specified canister.

Principal

The Principal of the canister to get the controllers of.

Promise<Principal[]>

The controllers of the specified canister.

Principal

import { Principal } from '@icp-sdk/core/principal';
const canisterId = Principal.fromUint8Array(new Uint8Array([0]));
const picServer = await PocketIcServer.start();
const pic = await PocketIc.create(picServer.getUrl());
const controllers = await pic.getControllers(canisterId);
await pic.tearDown();
await picServer.stop();

getCyclesBalance(canisterId): Promise<bigint>

Defined in: packages/pic/src/pocket-ic.ts:1527

Gets the current cycle balance of the specified canister.

Principal

The Principal of the canister to check.

Promise<bigint>

The current cycles balance of the canister.

Principal

import { Principal } from '@icp-sdk/core/principal';
import { PocketIc, PocketIcServer } from '@dfinity/pic';
const canisterId = Principal.fromUint8Array(new Uint8Array([0]));
const picServer = await PocketIcServer.start();
const pic = await PocketIc.create(picServer.getUrl());
const cyclesBalance = await pic.getCyclesBalance(canisterId);
await pic.tearDown();
await picServer.stop();

getDefaultEffectiveCanisterId(): Promise<Principal>

Defined in: packages/pic/src/pocket-ic.ts:1389

Get the default effective canister id for this PocketIC instance. This is useful when calling IcManagementCanister.provisionalCreateCanisterWithCycles on the management canister from @icp-sdk/canisters/ic-management.

Promise<Principal>

The default effective canister id.

Principal

import { PocketIc, PocketIcServer } from '@dfinity/pic';
import { IcManagementCanister } from '@icp-sdk/canisters/ic-management';
const picServer = await PocketIcServer.start();
const pic = await PocketIc.create(picServer.getUrl());
const defaultEffectiveCanisterId = await pic.getDefaultEffectiveCanisterId();
const managementCanister = IcManagementCanister.create({ agent });
const canisterId = await managementCanister.provisionalCreateCanisterWithCycles({
canisterId: defaultEffectiveCanisterId,
});
await pic.tearDown();
await picServer.stop();

getFiduciarySubnet(): Promise<SubnetTopology | undefined>

Defined in: packages/pic/src/pocket-ic.ts:1413

Get the Fiduciary subnet topology for this instance’s network. The instance network topology is configured via the create method.

Promise<SubnetTopology | undefined>

The subnet topology for the Fiduciary subnet, if it exists on this instance’s network.


getInternetIdentitySubnet(): Promise<SubnetTopology | undefined>

Defined in: packages/pic/src/pocket-ic.ts:1443

Get the Internet Identity subnet topology for this instance’s network. The instance network topology is configured via the create method.

Promise<SubnetTopology | undefined>

The subnet topology for the Internet Identity subnet, if it exists on this instance’s network.


getNnsSubnet(): Promise<SubnetTopology | undefined>

Defined in: packages/pic/src/pocket-ic.ts:1458

Get the NNS subnet topology for this instance’s network. The instance network topology is configured via the create method.

Promise<SubnetTopology | undefined>

The subnet topology for the NNS subnet, if it exists on this instance’s network.


getPendingHttpsOutcalls(): Promise<PendingHttpsOutcall[]>

Defined in: packages/pic/src/pocket-ic.ts:1681

Get all pending HTTPS Outcalls across all subnets on this PocketIC instance.

Promise<PendingHttpsOutcall[]>

An array of pending HTTPS Outcalls.

import { Principal } from '@icp-sdk/core/principal';
import { PocketIc, PocketIcServer } from '@dfinity/pic';
const canisterId = Principal.fromUint8Array(new Uint8Array([0]));
const picServer = await PocketIcServer.start();
const pic = await PocketIc.create(picServer.getUrl());
// queue the canister message that will send the HTTPS Outcall
const executeGoogleSearch = await deferredActor.google_search();
// tick for two rounds to allow the canister message to be processed
// and for the HTTPS Outcall to be queued
await pic.tick(2);
// get all queued HTTPS Outcalls
const pendingHttpsOutcalls = await pic.getPendingHttpsOutcalls();
// get the first pending HTTPS Outcall
const pendingGoogleSearchOutcall = pendingHttpsOutcalls[0];
// mock the HTTPS Outcall
await pic.mockPendingHttpsOutcall({
requestId: pendingGoogleSearchOutcall.requestId,
subnetId: pendingGoogleSearchOutcall.subnetId,
response: {
type: 'success',
body: new TextEncoder().encode('Google search result'),
statusCode: 200,
headers: [],
},
});
// finish executing the message, including the HTTPS Outcall
const result = await executeGoogleSearch();
await pic.tearDown();
await picServer.stop();

getPubKey(subnetId): Promise<Uint8Array<ArrayBufferLike>>

Defined in: packages/pic/src/pocket-ic.ts:1312

Fetch the public key of the specified subnet.

Principal

The Principal of the subnet to fetch the public key of.

Promise<Uint8Array<ArrayBufferLike>>

The public key of the specified subnet.

import { PocketIc, PocketIcServer } from '@dfinity/pic';
const picServer = await PocketIcServer.start();
const pic = await PocketIc.create(picServer.getUrl());
const subnets = pic.getApplicationSubnets();
const pubKey = await pic.getPubKey(subnets[0].id);
await pic.tearDown();
await picServer.stop();

getSnsSubnet(): Promise<SubnetTopology | undefined>

Defined in: packages/pic/src/pocket-ic.ts:1471

Get the SNS subnet topology for this instance’s network. The instance network topology is configured via the create method.

Promise<SubnetTopology | undefined>

The subnet topology for the SNS subnet, if it exists on this instance’s network.


getStableMemory(canisterId): Promise<Uint8Array<ArrayBufferLike>>

Defined in: packages/pic/src/pocket-ic.ts:1627

Get the stable memory of a given canister.

Principal

The Principal of the canister to get the stable memory of.

Promise<Uint8Array<ArrayBufferLike>>

A blob containing the canister’s stable memory.

Principal

import { Principal } from '@icp-sdk/core/principal';
import { PocketIc, PocketIcServer } from '@dfinity/pic';
const canisterId = Principal.fromUint8Array(new Uint8Array([0]));
const picServer = await PocketIcServer.start();
const pic = await PocketIc.create(picServer.getUrl());
const stableMemory = await pic.getStableMemory(canisterId);
await pic.tearDown();
await picServer.stop();

getSystemSubnets(): Promise<SubnetTopology[]>

Defined in: packages/pic/src/pocket-ic.ts:1497

Get all system subnet topologies for this instance’s network. The instance network topology is configured via the create method.

Promise<SubnetTopology[]>

An array of subnet topologies for each system subnet that exists on this instance’s network.


getTestThresholdKeysSubnet(): Promise<SubnetTopology | undefined>

Defined in: packages/pic/src/pocket-ic.ts:1426

Get the test threshold keys subnet topology for this instance’s network. The instance network topology is configured via the create method.

Promise<SubnetTopology | undefined>

The subnet topology for the test threshold keys subnet, if it exists on this instance’s network.


getTime(): Promise<number>

Defined in: packages/pic/src/pocket-ic.ts:1090

Get the current time of the IC in milliseconds since the Unix epoch.

Promise<number>

The current time in milliseconds since the UNIX epoch.

import { PocketIc, PocketIcServer } from '@dfinity/pic';
const picServer = await PocketIcServer.start();
const pic = await PocketIc.create(picServer.getUrl());
const time = await pic.getTime();
await pic.tearDown();
await picServer.stop();

getTopology(): Promise<SubnetTopology[]>

Defined in: packages/pic/src/pocket-ic.ts:1355

Get the topology of this instance’s network. The topology is a list of subnets, each with a type and a list of canister ID ranges that can be deployed to that subnet. The instance network topology is configured via the create method.

Promise<SubnetTopology[]>

An array of subnet topologies, see SubnetTopology.


installCode(options): Promise<void>

Defined in: packages/pic/src/pocket-ic.ts:422

Installs the given WASM module to the provided canister. To create a canister to install code to, see createCanister. For a more convenient way of creating a PocketIC instance, creating a canister and installing code, see setupCanister.

InstallCodeOptions

Options for installing the code, see InstallCodeOptions.

Promise<void>

Principal

import { Principal } from '@icp-sdk/core/principal';
import { PocketIc, PocketIcServer } from '@dfinity/pic';
import { resolve } from 'node:path';
const canisterId = Principal.fromUint8Array(new Uint8Array([0]));
const wasm = resolve('..', '..', 'canister.wasm');
const picServer = await PocketIcServer.start();
const pic = await PocketIc.create(picServer.getUrl());
await pic.installCode({ canisterId, wasm });
await pic.tearDown();
await picServer.stop();

makeLive(): Promise<number>

Defined in: packages/pic/src/pocket-ic.ts:1785

Make the PocketIC instance live by enabling auto progress and starting an HTTP gateway. If the instance was created with CreateInstanceOptions.httpGateway, that gateway is used instead. If the instance is already live, this method returns the port of its HTTP gateway. The PocketIC instance must be created with at least an NNS subnet in order for fetchRootKey to work correctly.

Promise<number>

The HTTP Gateway port.

import { Principal } from '@icp-sdk/core/principal';
import { PocketIc, PocketIcServer } from '@dfinity/pic';
import { resolve } from 'node:path';
const canisterId = Principal.fromUint8Array(new Uint8Array([0]));
const wasm = resolve('..', '..', 'canister.wasm');
const picServer = await PocketIcServer.start();
const pic = await PocketIc.create(picServer.getUrl(), {
nns: { state: { type: SubnetStateType.New } },
application: [{ state: { type: SubnetStateType.New } }],
});
const canister = await pic.installCode({ canisterId, wasm });
await pic.installCode({ canisterId, wasm });
const httpGatewayPort = await pic.makeLive();
const agent = await HttpAgent.create({
host: `http://localhost:${httpGatewayPort}`,
shouldFetchRootKey: true,
});
const actor = Actor.createActor(idlFactory, { agent, canisterId });
await pic.stopLive();
await pic.tearDown();
await picServer.stop();

mockPendingHttpsOutcall(options): Promise<void>

Defined in: packages/pic/src/pocket-ic.ts:1732

Mock a pending HTTPS Outcall.

MockPendingHttpsOutcallOptions

Options for mocking the pending HTTPS Outcall, see MockPendingHttpsOutcallOptions.

Promise<void>

import { Principal } from '@icp-sdk/core/principal';
import { PocketIc, PocketIcServer } from '@dfinity/pic';
const canisterId = Principal.fromUint8Array(new Uint8Array([0]));
const picServer = await PocketIcServer.start();
const pic = await PocketIc.create(picServer.getUrl());
// queue the canister message that will send the HTTPS Outcall
const executeGoogleSearch = await deferredActor.google_search();
// tick for two rounds to allow the canister message to be processed
// and for the HTTPS Outcall to be queued
await pic.tick(2);
// get all queued HTTPS Outcalls
const pendingHttpsOutcalls = await pic.getPendingHttpsOutcalls();
// get the first pending HTTPS Outcall
const pendingGoogleSearchOutcall = pendingHttpsOutcalls[0];
// mock the HTTPS Outcall
await pic.mockPendingHttpsOutcall({
requestId: pendingGoogleSearchOutcall.requestId,
subnetId: pendingGoogleSearchOutcall.subnetId,
response: {
type: 'success',
body: new TextEncoder().encode('Google search result'),
statusCode: 200,
headers: [],
},
});
// finish executing the message, including the HTTPS Outcall
const result = await executeGoogleSearch();
await pic.tearDown();
await picServer.stop();

queryCall(options): Promise<Uint8Array<ArrayBufferLike>>

Defined in: packages/pic/src/pocket-ic.ts:861

Makes a query call to the given canister.

QueryCallOptions

Options for making the query call, see QueryCallOptions.

Promise<Uint8Array<ArrayBufferLike>>

The Candid-encoded response of the query call.

import { Principal } from '@icp-sdk/core/principal';
import { PocketIc, PocketIcServer } from '@dfinity/pic';
import { _SERVICE, idlFactory } from '../declarations';
const wasm = resolve('..', '..', 'canister.wasm');
const picServer = await PocketIcServer.start();
const pic = await PocketIc.create(picServer.getUrl());
canisterId = await pic.createCanister({
sender: controllerIdentity.getPrincipal(),
});
await pic.installCode({ canisterId, wasm });
const res = await pic.queryCall({
canisterId,
method: 'greet',
});
await pic.tearDown();
await picServer.stop();

reinstallCode(options): Promise<void>

Defined in: packages/pic/src/pocket-ic.ts:494

Reinstalls the given WASM module to the provided canister. This will reset both the canister’s heap and its stable memory. To create a canister to upgrade, see createCanister. To install the initial WASM module to a new canister, see installCode.

ReinstallCodeOptions

Options for reinstalling the code, see ReinstallCodeOptions.

Promise<void>

Principal

import { Principal } from '@icp-sdk/core/principal';
import { PocketIc, PocketIcServer } from '@dfinity/pic';
import { resolve } from 'node:path';
const canisterId = Principal.fromUint8Array(new Uint8Array([0]));
const wasm = resolve('..', '..', 'canister.wasm');
const picServer = await PocketIcServer.start();
const pic = await PocketIc.create(picServer.getUrl());
await pic.reinstallCode({ canisterId, wasm });
await pic.tearDown();
await picServer.stop();

resetCertifiedTime(): Promise<void>

Defined in: packages/pic/src/pocket-ic.ts:1145

Reset the time of the IC to the current time and immediately have query calls and read state requests reflect the new time.

Use resetTime to reset time without immediately reflecting the new time.

Promise<void>

import { PocketIc, PocketIcServer } from '@dfinity/pic';
const picServer = await PocketIcServer.start();
const pic = await PocketIc.create(picServer.getUrl());
await pic.resetCertifiedTime();
const time = await pic.getTime();
await pic.tearDown();
await picServer.stop();

resetTime(): Promise<void>

Defined in: packages/pic/src/pocket-ic.ts:1120

Reset the time of the IC to the current time. tick should be called after calling this method in order for query calls and read state request to reflect the new time.

Use resetCertifiedTime to set time and immediately have query calls and read state requests reflect the new time.

Promise<void>

import { PocketIc, PocketIcServer } from '@dfinity/pic';
const picServer = await PocketIcServer.start();
const pic = await PocketIc.create(picServer.getUrl());
await pic.resetTime();
await pic.tick();
const time = await pic.getTime();
await pic.tearDown();
await picServer.stop();

setCertifiedTime(time): Promise<void>

Defined in: packages/pic/src/pocket-ic.ts:1219

Set the current time of the IC and immediately have query calls and read state requests reflect the new time.

Use setTime to set time without immediately reflecting the new time.

The time to set in milliseconds since the Unix epoch.

number | Date

Promise<void>

import { PocketIc, PocketIcServer } from '@dfinity/pic';
const pic = await PocketIc.create();
const date = new Date();
const picServer = await PocketIcServer.start();
const pic = await PocketIc.create(picServer.getUrl());
await pic.setCertifiedTime(date);
// or
await pic.setCertifiedTime(date.getTime());
const time = await pic.getTime();
await pic.tearDown();
await picServer.stop();

setStableMemory(canisterId, stableMemory): Promise<void>

Defined in: packages/pic/src/pocket-ic.ts:1592

Set the stable memory of a given canister.

Principal

The Principal of the canister to set the stable memory of.

Uint8Array

A blob containing the stable memory to set.

Promise<void>

Principal

import { Principal } from '@icp-sdk/core/principal';
import { PocketIc, PocketIcServer } from '@dfinity/pic';
const canisterId = Principal.fromUint8Array(new Uint8Array([0]));
const stableMemory = new Uint8Array([0, 1, 2, 3, 4]);
const picServer = await PocketIcServer.start();
const pic = await PocketIc.create(picServer.getUrl());
await pic.setStableMemory(canisterId, stableMemory);
await pic.tearDown();
await picServer.stop();

setTime(time): Promise<void>

Defined in: packages/pic/src/pocket-ic.ts:1182

Set the current time of the IC. tick should be called after calling this method in order for query calls and read state request to reflect the new time.

Use setCertifiedTime to set time and immediately have query calls and read state requests reflect the new time.

The time to set in milliseconds since the Unix epoch.

number | Date

Promise<void>

import { PocketIc, PocketIcServer } from '@dfinity/pic';
const pic = await PocketIc.create();
const date = new Date();
const picServer = await PocketIcServer.start();
const pic = await PocketIc.create(picServer.getUrl());
await pic.setTime(date);
// or
await pic.setTime(date.getTime());
await pic.tick();
const time = await pic.getTime();
await pic.tearDown();
await picServer.stop();

setupCanister<T>(options): Promise<CanisterFixture<T>>

Defined in: packages/pic/src/pocket-ic.ts:184

A convenience method that creates a new canister, installs the given WASM module to it and returns a typesafe Actor that implements the Candid interface of the canister. To just create a canister, see createCanister. To just install code to an existing canister, see installCode. To just create an Actor for an existing canister, see createActor.

T extends ActorInterface<T> = object

SetupCanisterOptions

Options for setting up the canister, see SetupCanisterOptions.

Promise<CanisterFixture<T>>

The Actor instance.

import { PocketIc, PocketIcServer } from '@dfinity/pic';
import { _SERVICE, idlFactory } from '../declarations';
const wasmPath = resolve('..', '..', 'canister.wasm');
const picServer = await PocketIcServer.start();
const pic = await PocketIc.create(picServer.getUrl());
const fixture = await pic.setupCanister<_SERVICE>({ idlFactory, wasmPath });
const { actor } = fixture;
await pic.tearDown();
await picServer.stop();

startCanister(options): Promise<void>

Defined in: packages/pic/src/pocket-ic.ts:327

Starts the given canister.

StartCanisterOptions

Options for starting the canister, see StartCanisterOptions.

Promise<void>

Principal

import { Principal } from '@icp-sdk/core/principal';
import { PocketIc, PocketIcServer } from '@dfinity/pic';
const canisterId = Principal.fromUint8Array(new Uint8Array([0]));
const picServer = await PocketIcServer.start();
const pic = await PocketIc.create(picServer.getUrl());
await pic.startCanister({ canisterId });
await pic.tearDown();
await picServer.stop();

stopCanister(options): Promise<void>

Defined in: packages/pic/src/pocket-ic.ts:372

Stops the given canister.

StopCanisterOptions

Options for stopping the canister, see StopCanisterOptions.

Promise<void>

Principal

import { Principal } from '@icp-sdk/core/principal';
import { PocketIc, PocketIcServer } from '@dfinity/pic';
const canisterId = Principal.fromUint8Array(new Uint8Array([0]));
const picServer = await PocketIcServer.start();
const pic = await PocketIc.create(picServer.getUrl());
await pic.stopCanister({ canisterId });
await pic.tearDown();
await picServer.stop();

stopLive(): Promise<void>

Defined in: packages/pic/src/pocket-ic.ts:1840

Disables auto progress and stops the HTTP gateway started by makeLive. A gateway created with CreateInstanceOptions.httpGateway keeps running until the instance is torn down.

Promise<void>

import { Principal } from '@icp-sdk/core/principal';
import { PocketIc, PocketIcServer } from '@dfinity/pic';
import { resolve } from 'node:path';
const canisterId = Principal.fromUint8Array(new Uint8Array([0]));
const wasm = resolve('..', '..', 'canister.wasm');
const picServer = await PocketIcServer.start();
const pic = await PocketIc.create(picServer.getUrl(), {
nns: { state: { type: SubnetStateType.New } },
application: [{ state: { type: SubnetStateType.New } }],
});
const canister = await pic.installCode({ canisterId, wasm });
await pic.installCode({ canisterId, wasm });
const httpGatewayPort = await pic.makeLive();
const agent = await HttpAgent.create({
host: `http://localhost:${httpGatewayPort}`,
shouldFetchRootKey: true,
});
const actor = Actor.createActor(idlFactory, { agent, canisterId });
await pic.stopLive();
await pic.tearDown();
await picServer.stop();

tearDown(): Promise<void>

Defined in: packages/pic/src/pocket-ic.ts:1014

Deletes the PocketIC instance.

Promise<void>

import { PocketIc, PocketIcServer } from '@dfinity/pic';
const picServer = await PocketIcServer.start();
const pic = await PocketIc.create(picServer.getUrl());
await pic.tearDown();
await picServer.stop();

tick(times?): Promise<void>

Defined in: packages/pic/src/pocket-ic.ts:1039

Make the IC produce and progress by one block. Accepts a parameter times to tick multiple times, the default is 1.

number = 1

The number of new blocks to produce and progress by. Defaults to 1.

import { PocketIc, PocketIcServer } from '@dfinity/pic';
const picServer = await PocketIcServer.start();
const pic = await PocketIc.create(picServer.getUrl());
await pic.tick();
// or to tick multiple times
await pic.tick(3);
await pic.tearDown();
await picServer.stop();

Promise<void>


updateCall(options): Promise<Uint8Array<ArrayBufferLike>>

Defined in: packages/pic/src/pocket-ic.ts:976

Makes an update call to the given canister.

UpdateCallOptions

Options for making the update call, see UpdateCallOptions.

Promise<Uint8Array<ArrayBufferLike>>

The Candid-encoded response of the update call.

import { Principal } from '@icp-sdk/core/principal';
import { PocketIc, PocketIcServer } from '@dfinity/pic';
import { _SERVICE, idlFactory } from '../declarations';
const wasm = resolve('..', '..', 'canister.wasm');
const picServer = await PocketIcServer.start();
const pic = await PocketIc.create(picServer.getUrl());
canisterId = await pic.createCanister({
sender: controllerIdentity.getPrincipal(),
});
await pic.installCode({ canisterId, wasm });
const res = await pic.updateCall({
canisterId,
method: 'greet',
});
await pic.tearDown();
await picServer.stop();

updateCanisterSettings(options): Promise<void>

Defined in: packages/pic/src/pocket-ic.ts:623

Updates the settings of the given canister.

UpdateCanisterSettingsOptions

Options for updating the canister settings, see UpdateCanisterSettingsOptions.

Promise<void>

Principal

import { Principal } from '@icp-sdk/core/principal';
import { PocketIc, PocketIcServer } from '@dfinity/pic';
const canisterId = Principal.fromUint8Array(new Uint8Array([0]));
const picServer = await PocketIcServer.start();
const pic = await PocketIc.create(picServer.getUrl());
await pic.updateCanisterSettings({
canisterId,
controllers: [Principal.fromUint8Array(new Uint8Array([1]))],
});
await pic.tearDown();
await picServer.stop();

upgradeCanister(options): Promise<void>

Defined in: packages/pic/src/pocket-ic.ts:559

Upgrades the given canister with the given WASM module. This will reset the canister’s heap, but preserve stable memory. To create a canister to upgrade to, see createCanister. To install the initial WASM module to a new canister, see installCode.

UpgradeCanisterOptions

Options for upgrading the canister, see UpgradeCanisterOptions.

Promise<void>

Principal

import { Principal } from '@icp-sdk/core/principal';
import { PocketIc, PocketIcServer } from '@dfinity/pic';
import { resolve } from 'node:path';
const canisterId = Principal.fromUint8Array(new Uint8Array([0]));
const wasm = resolve('..', '..', 'canister.wasm');
const picServer = await PocketIcServer.start();
const pic = await PocketIc.create(picServer.getUrl());
await pic.upgradeCanister({ canisterId, wasm });
await pic.tearDown();
await picServer.stop();

static create(url, options?): Promise<PocketIc>

Defined in: packages/pic/src/pocket-ic.ts:144

Creates a PocketIC instance.

string

The URL of an existing PocketIC server to connect to.

CreateInstanceOptions

Options for creating the PocketIC instance see CreateInstanceOptions.

Promise<PocketIc>

A new PocketIC instance.

import { PocketIc, PocketIcServer } from '@dfinity/pic';
const picServer = await PocketIcServer.start();
const pic = await PocketIc.create(picServer.getUrl());
const fixture = await pic.setupCanister<_SERVICE>({ idlFactory, wasmPath });
const { actor } = fixture;
await pic.tearDown();
await picServer.stop();