PocketIc
Defined in: packages/pic/src/pocket-ic.ts:109
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.
Example
Section titled “Example”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();Methods
Section titled “Methods”addCycles()
Section titled “addCycles()”addCycles(
canisterId,amount):Promise<bigint>
Defined in: packages/pic/src/pocket-ic.ts:1412
Add cycles to the specified canister.
Parameters
Section titled “Parameters”canisterId
Section titled “canisterId”Principal
The Principal of the canister to add cycles to.
amount
Section titled “amount”bigint
The amount of cycles to add.
Returns
Section titled “Returns”Promise<bigint>
The new cycle balance of the canister.
Example
Section titled “Example”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()
Section titled “advanceCertifiedTime()”advanceCertifiedTime(
duration):Promise<void>
Defined in: packages/pic/src/pocket-ic.ts:1156
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.
Parameters
Section titled “Parameters”duration
Section titled “duration”number
The duration to advance the time by.
Returns
Section titled “Returns”Promise<void>
Example
Section titled “Example”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()
Section titled “advanceTime()”advanceTime(
duration):Promise<void>
Defined in: packages/pic/src/pocket-ic.ts:1125
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.
Parameters
Section titled “Parameters”duration
Section titled “duration”number
The duration to advance the time by.
Returns
Section titled “Returns”Promise<void>
Example
Section titled “Example”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()
Section titled “canisterStatus()”canisterStatus(
options):Promise<CanisterStatusResult>
Defined in: packages/pic/src/pocket-ic.ts:642
Returns the status of the given canister.
Parameters
Section titled “Parameters”options
Section titled “options”Options for querying the canister status, see CanisterStatusOptions.
Returns
Section titled “Returns”Promise<CanisterStatusResult>
The canister status, see CanisterStatusResult.
Example
Section titled “Example”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()
Section titled “createActor()”createActor<
T>(interfaceFactory,canisterId):Actor<T>
Defined in: packages/pic/src/pocket-ic.ts:719
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.
Type Parameters
Section titled “Type Parameters”T extends ActorInterface<T> = object
The type of the Actor. Must implement ActorInterface.
Parameters
Section titled “Parameters”interfaceFactory
Section titled “interfaceFactory”InterfaceFactory
The InterfaceFactory to use for the Actor.
canisterId
Section titled “canisterId”Principal
The Principal of the canister to create the Actor for.
Returns
Section titled “Returns”Actor<T>
The Actor instance.
Example
Section titled “Example”import { Principal } from '@icp-sdk/core/principal';import { PocketIc, PocketIcServer } from '@dfinity/pic';import { _SERVICE, idlFactory } from '../declarations';
const canisterId = Principal.fromUint8Array(new Uint8Array([0]));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 });
await pic.tearDown();await picServer.stop();createCanister()
Section titled “createCanister()”createCanister(
options?):Promise<Principal>
Defined in: packages/pic/src/pocket-ic.ts:231
Creates a new canister. For a more convenient way of creating a PocketIC instance, creating a canister and installing code, see setupCanister.
Parameters
Section titled “Parameters”options?
Section titled “options?”Options for creating the canister, see CreateCanisterOptions.
Returns
Section titled “Returns”Promise<Principal>
The Principal of the newly created canister.
Example
Section titled “Example”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()
Section titled “createDeferredActor()”createDeferredActor<
T>(interfaceFactory,canisterId):DeferredActor<T>
Defined in: packages/pic/src/pocket-ic.ts:752
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.
Type Parameters
Section titled “Type Parameters”T extends ActorInterface<T> = object
The type of the DeferredActor. Must implement ActorInterface.
Parameters
Section titled “Parameters”interfaceFactory
Section titled “interfaceFactory”InterfaceFactory
The InterfaceFactory to use for the DeferredActor.
canisterId
Section titled “canisterId”Principal
The Principal of the canister to create the DeferredActor for.
Returns
Section titled “Returns”The DeferredActor instance.
getApplicationSubnets()
Section titled “getApplicationSubnets()”getApplicationSubnets():
Promise<SubnetTopology[]>
Defined in: packages/pic/src/pocket-ic.ts:1338
Get all application subnet topologies for this instance’s network. The instance network topology is configured via the create method.
Returns
Section titled “Returns”Promise<SubnetTopology[]>
An array of subnet topologies for each application subnet that exists on this instance’s network.
getBitcoinSubnet()
Section titled “getBitcoinSubnet()”getBitcoinSubnet():
Promise<SubnetTopology|undefined>
Defined in: packages/pic/src/pocket-ic.ts:1271
Get the Bitcoin subnet topology for this instance’s network. The instance network topology is configured via the create method.
Returns
Section titled “Returns”Promise<SubnetTopology | undefined>
The subnet topology for the Bitcoin subnet, if it exists on this instance’s network.
getCanisterSubnetId()
Section titled “getCanisterSubnetId()”getCanisterSubnetId(
canisterId):Promise<Principal|null>
Defined in: packages/pic/src/pocket-ic.ts:1210
Gets the subnet Id of the provided canister Id.
Parameters
Section titled “Parameters”canisterId
Section titled “canisterId”Principal
The Principal of the canister to get the subnet Id of.
Returns
Section titled “Returns”Promise<Principal | null>
The canister’s subnet Id if the canister exists, null otherwise.
Example
Section titled “Example”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()
Section titled “getControllers()”getControllers(
canisterId):Promise<Principal[]>
Defined in: packages/pic/src/pocket-ic.ts:939
Get the controllers of the specified canister.
Parameters
Section titled “Parameters”canisterId
Section titled “canisterId”Principal
The Principal of the canister to get the controllers of.
Returns
Section titled “Returns”Promise<Principal[]>
The controllers of the specified canister.
Example
Section titled “Example”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()
Section titled “getCyclesBalance()”getCyclesBalance(
canisterId):Promise<bigint>
Defined in: packages/pic/src/pocket-ic.ts:1381
Gets the current cycle balance of the specified canister.
Parameters
Section titled “Parameters”canisterId
Section titled “canisterId”Principal
The Principal of the canister to check.
Returns
Section titled “Returns”Promise<bigint>
The current cycles balance of the canister.
Example
Section titled “Example”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()
Section titled “getDefaultEffectiveCanisterId()”getDefaultEffectiveCanisterId():
Promise<Principal>
Defined in: packages/pic/src/pocket-ic.ts:1260
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.
Returns
Section titled “Returns”Promise<Principal>
The default effective canister id.
Example
Section titled “Example”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()
Section titled “getFiduciarySubnet()”getFiduciarySubnet():
Promise<SubnetTopology|undefined>
Defined in: packages/pic/src/pocket-ic.ts:1284
Get the Fiduciary subnet topology for this instance’s network. The instance network topology is configured via the create method.
Returns
Section titled “Returns”Promise<SubnetTopology | undefined>
The subnet topology for the Fiduciary subnet, if it exists on this instance’s network.
getInternetIdentitySubnet()
Section titled “getInternetIdentitySubnet()”getInternetIdentitySubnet():
Promise<SubnetTopology|undefined>
Defined in: packages/pic/src/pocket-ic.ts:1297
Get the Internet Identity subnet topology for this instance’s network. The instance network topology is configured via the create method.
Returns
Section titled “Returns”Promise<SubnetTopology | undefined>
The subnet topology for the Internet Identity subnet, if it exists on this instance’s network.
getNnsSubnet()
Section titled “getNnsSubnet()”getNnsSubnet():
Promise<SubnetTopology|undefined>
Defined in: packages/pic/src/pocket-ic.ts:1312
Get the NNS subnet topology for this instance’s network. The instance network topology is configured via the create method.
Returns
Section titled “Returns”Promise<SubnetTopology | undefined>
The subnet topology for the NNS subnet, if it exists on this instance’s network.
getPendingHttpsOutcalls()
Section titled “getPendingHttpsOutcalls()”getPendingHttpsOutcalls():
Promise<PendingHttpsOutcall[]>
Defined in: packages/pic/src/pocket-ic.ts:1535
Get all pending HTTPS Outcalls across all subnets on this PocketIC instance.
Returns
Section titled “Returns”Promise<PendingHttpsOutcall[]>
An array of pending HTTPS Outcalls.
Example
Section titled “Example”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 Outcallconst executeGoogleSearch = await deferredActor.google_search();
// tick for two rounds to allow the canister message to be processed// and for the HTTPS Outcall to be queuedawait pic.tick(2);
// get all queued HTTPS Outcallsconst pendingHttpsOutcalls = await pic.getPendingHttpsOutcalls();
// get the first pending HTTPS Outcallconst pendingGoogleSearchOutcall = pendingHttpsOutcalls[0];
// mock the HTTPS Outcallawait 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 Outcallconst result = await executeGoogleSearch();
await pic.tearDown();await picServer.stop();getPubKey()
Section titled “getPubKey()”getPubKey(
subnetId):Promise<Uint8Array<ArrayBufferLike>>
Defined in: packages/pic/src/pocket-ic.ts:1183
Fetch the public key of the specified subnet.
Parameters
Section titled “Parameters”subnetId
Section titled “subnetId”Principal
The Principal of the subnet to fetch the public key of.
Returns
Section titled “Returns”Promise<Uint8Array<ArrayBufferLike>>
The public key of the specified subnet.
Example
Section titled “Example”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()
Section titled “getSnsSubnet()”getSnsSubnet():
Promise<SubnetTopology|undefined>
Defined in: packages/pic/src/pocket-ic.ts:1325
Get the SNS subnet topology for this instance’s network. The instance network topology is configured via the create method.
Returns
Section titled “Returns”Promise<SubnetTopology | undefined>
The subnet topology for the SNS subnet, if it exists on this instance’s network.
getStableMemory()
Section titled “getStableMemory()”getStableMemory(
canisterId):Promise<Uint8Array<ArrayBufferLike>>
Defined in: packages/pic/src/pocket-ic.ts:1481
Get the stable memory of a given canister.
Parameters
Section titled “Parameters”canisterId
Section titled “canisterId”Principal
The Principal of the canister to get the stable memory of.
Returns
Section titled “Returns”Promise<Uint8Array<ArrayBufferLike>>
A blob containing the canister’s stable memory.
Example
Section titled “Example”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()
Section titled “getSystemSubnets()”getSystemSubnets():
Promise<SubnetTopology[]>
Defined in: packages/pic/src/pocket-ic.ts:1351
Get all system subnet topologies for this instance’s network. The instance network topology is configured via the create method.
Returns
Section titled “Returns”Promise<SubnetTopology[]>
An array of subnet topologies for each system subnet that exists on this instance’s network.
getTime()
Section titled “getTime()”getTime():
Promise<number>
Defined in: packages/pic/src/pocket-ic.ts:961
Get the current time of the IC in milliseconds since the Unix epoch.
Returns
Section titled “Returns”Promise<number>
The current time in milliseconds since the UNIX epoch.
Example
Section titled “Example”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()
Section titled “getTopology()”getTopology():
Promise<SubnetTopology[]>
Defined in: packages/pic/src/pocket-ic.ts:1226
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.
Returns
Section titled “Returns”Promise<SubnetTopology[]>
An array of subnet topologies, see SubnetTopology.
installCode()
Section titled “installCode()”installCode(
options):Promise<void>
Defined in: packages/pic/src/pocket-ic.ts:389
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.
Parameters
Section titled “Parameters”options
Section titled “options”Options for installing the code, see InstallCodeOptions.
Returns
Section titled “Returns”Promise<void>
Example
Section titled “Example”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()
Section titled “makeLive()”makeLive():
Promise<number>
Defined in: packages/pic/src/pocket-ic.ts:1638
Make the PocketIC instance live by enabling auto progress and starting the HTTP Gateway.
If the server is already live, this method will return the HTTP Gateway URL.
The PocketIC instance must be created with at least an NNS subnet in
order for fetchRootKey to work correctly.
Returns
Section titled “Returns”Promise<number>
The HTTP Gateway port.
Example
Section titled “Example”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 httpGatewayUrl = await pic.makeLive();const agent = await HttpAgent.create({ host: httpGatewayUrl, shouldFetchRootKey: true,});const actor = Actor.createActor(idlFactory, { agent, canisterId });
await pic.stopLive();await pic.tearDown();await picServer.stop();mockPendingHttpsOutcall()
Section titled “mockPendingHttpsOutcall()”mockPendingHttpsOutcall(
options):Promise<void>
Defined in: packages/pic/src/pocket-ic.ts:1586
Mock a pending HTTPS Outcall.
Parameters
Section titled “Parameters”options
Section titled “options”MockPendingHttpsOutcallOptions
Options for mocking the pending HTTPS Outcall, see MockPendingHttpsOutcallOptions.
Returns
Section titled “Returns”Promise<void>
Example
Section titled “Example”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 Outcallconst executeGoogleSearch = await deferredActor.google_search();
// tick for two rounds to allow the canister message to be processed// and for the HTTPS Outcall to be queuedawait pic.tick(2);
// get all queued HTTPS Outcallsconst pendingHttpsOutcalls = await pic.getPendingHttpsOutcalls();
// get the first pending HTTPS Outcallconst pendingGoogleSearchOutcall = pendingHttpsOutcalls[0];
// mock the HTTPS Outcallawait 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 Outcallconst result = await executeGoogleSearch();
await pic.tearDown();await picServer.stop();queryCall()
Section titled “queryCall()”queryCall(
options):Promise<Uint8Array<ArrayBufferLike>>
Defined in: packages/pic/src/pocket-ic.ts:796
Makes a query call to the given canister.
Parameters
Section titled “Parameters”options
Section titled “options”Options for making the query call, see QueryCallOptions.
Returns
Section titled “Returns”Promise<Uint8Array<ArrayBufferLike>>
The Candid-encoded response of the query call.
Example
Section titled “Example”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()
Section titled “reinstallCode()”reinstallCode(
options):Promise<void>
Defined in: packages/pic/src/pocket-ic.ts:461
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.
Parameters
Section titled “Parameters”options
Section titled “options”Options for reinstalling the code, see ReinstallCodeOptions.
Returns
Section titled “Returns”Promise<void>
Example
Section titled “Example”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()
Section titled “resetCertifiedTime()”resetCertifiedTime():
Promise<void>
Defined in: packages/pic/src/pocket-ic.ts:1016
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.
Returns
Section titled “Returns”Promise<void>
Example
Section titled “Example”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()
Section titled “resetTime()”resetTime():
Promise<void>
Defined in: packages/pic/src/pocket-ic.ts:991
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.
Returns
Section titled “Returns”Promise<void>
Example
Section titled “Example”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()
Section titled “setCertifiedTime()”setCertifiedTime(
time):Promise<void>
Defined in: packages/pic/src/pocket-ic.ts:1090
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.
Parameters
Section titled “Parameters”The time to set in milliseconds since the Unix epoch.
number | Date
Returns
Section titled “Returns”Promise<void>
Example
Section titled “Example”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);// orawait pic.setCertifiedTime(date.getTime());
const time = await pic.getTime();
await pic.tearDown();await picServer.stop();setStableMemory()
Section titled “setStableMemory()”setStableMemory(
canisterId,stableMemory):Promise<void>
Defined in: packages/pic/src/pocket-ic.ts:1446
Set the stable memory of a given canister.
Parameters
Section titled “Parameters”canisterId
Section titled “canisterId”Principal
The Principal of the canister to set the stable memory of.
stableMemory
Section titled “stableMemory”Uint8Array
A blob containing the stable memory to set.
Returns
Section titled “Returns”Promise<void>
Example
Section titled “Example”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()
Section titled “setTime()”setTime(
time):Promise<void>
Defined in: packages/pic/src/pocket-ic.ts:1053
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.
Parameters
Section titled “Parameters”The time to set in milliseconds since the Unix epoch.
number | Date
Returns
Section titled “Returns”Promise<void>
Example
Section titled “Example”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);// orawait pic.setTime(date.getTime());
await pic.tick();
const time = await pic.getTime();
await pic.tearDown();await picServer.stop();setupCanister()
Section titled “setupCanister()”setupCanister<
T>(options):Promise<CanisterFixture<T>>
Defined in: packages/pic/src/pocket-ic.ts:175
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.
Type Parameters
Section titled “Type Parameters”T extends ActorInterface<T> = object
Parameters
Section titled “Parameters”options
Section titled “options”Options for setting up the canister, see SetupCanisterOptions.
Returns
Section titled “Returns”Promise<CanisterFixture<T>>
The Actor instance.
Example
Section titled “Example”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()
Section titled “startCanister()”startCanister(
options):Promise<void>
Defined in: packages/pic/src/pocket-ic.ts:294
Starts the given canister.
Parameters
Section titled “Parameters”options
Section titled “options”Options for starting the canister, see StartCanisterOptions.
Returns
Section titled “Returns”Promise<void>
Example
Section titled “Example”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()
Section titled “stopCanister()”stopCanister(
options):Promise<void>
Defined in: packages/pic/src/pocket-ic.ts:339
Stops the given canister.
Parameters
Section titled “Parameters”options
Section titled “options”Options for stopping the canister, see StopCanisterOptions.
Returns
Section titled “Returns”Promise<void>
Example
Section titled “Example”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()
Section titled “stopLive()”stopLive():
Promise<void>
Defined in: packages/pic/src/pocket-ic.ts:1690
Disables auto progress and stops the HTTP Gateway for the PocketIC instance.
Returns
Section titled “Returns”Promise<void>
Example
Section titled “Example”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 httpGatewayUrl = await pic.makeLive();const agent = await HttpAgent.create({ host: httpGatewayUrl, shouldFetchRootKey: true,});const actor = Actor.createActor(idlFactory, { agent, canisterId });
await pic.stopLive();await pic.tearDown();await picServer.stop();tearDown()
Section titled “tearDown()”tearDown():
Promise<void>
Defined in: packages/pic/src/pocket-ic.ts:885
Deletes the PocketIC instance.
Returns
Section titled “Returns”Promise<void>
Example
Section titled “Example”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()
Section titled “tick()”tick(
times?):Promise<void>
Defined in: packages/pic/src/pocket-ic.ts:910
Make the IC produce and progress by one block. Accepts a parameter times to tick multiple times,
the default is 1.
Parameters
Section titled “Parameters”times?
Section titled “times?”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 timesawait pic.tick(3);
await pic.tearDown();await picServer.stop();Returns
Section titled “Returns”Promise<void>
updateCall()
Section titled “updateCall()”updateCall(
options):Promise<Uint8Array<ArrayBufferLike>>
Defined in: packages/pic/src/pocket-ic.ts:849
Makes an update call to the given canister.
Parameters
Section titled “Parameters”options
Section titled “options”Options for making the update call, see UpdateCallOptions.
Returns
Section titled “Returns”Promise<Uint8Array<ArrayBufferLike>>
The Candid-encoded response of the update call.
Example
Section titled “Example”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()
Section titled “updateCanisterSettings()”updateCanisterSettings(
options):Promise<void>
Defined in: packages/pic/src/pocket-ic.ts:590
Updates the settings of the given canister.
Parameters
Section titled “Parameters”options
Section titled “options”Options for updating the canister settings, see UpdateCanisterSettingsOptions.
Returns
Section titled “Returns”Promise<void>
Example
Section titled “Example”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()
Section titled “upgradeCanister()”upgradeCanister(
options):Promise<void>
Defined in: packages/pic/src/pocket-ic.ts:526
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.
Parameters
Section titled “Parameters”options
Section titled “options”Options for upgrading the canister, see UpgradeCanisterOptions.
Returns
Section titled “Returns”Promise<void>
Example
Section titled “Example”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();create()
Section titled “create()”
staticcreate(url,options?):Promise<PocketIc>
Defined in: packages/pic/src/pocket-ic.ts:135
Creates a PocketIC instance.
Parameters
Section titled “Parameters”string
The URL of an existing PocketIC server to connect to.
options?
Section titled “options?”Options for creating the PocketIC instance see CreateInstanceOptions.
Returns
Section titled “Returns”Promise<PocketIc>
A new PocketIC instance.
Example
Section titled “Example”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();