Skip to main content

Testing contracts

Contracts can be tested in two ways, one by writing rust tests in the contract.rs file itself, and other way is to write a mocha test script that interacts with deployed contract and assert the returned values. There are examples for both in the sample-project created after wasmkit init step.

Rust tests

These tests can be run by going into the contract's directory having Cargo.toml file and running:


cargo test

Client interaction tests

These tests can be run by running:


wasmkit test --network <network-name>

Test scripts

Wasmkit has support for user to write tests on top of js interactions with the deployed contract instance. These scripts are stored in the test/ directory in the project's root directory.

A wasmkit test script has the same structure as a mocha test file with describe and it blocks, a sample test is explained below:


_40
import { use } from "chai";
_40
import { getAccountByName, wasmKitChai } from "@kubiklabs/wasmkit";
_40
_40
import { CounterContract } from "../artifacts/typescript_schema/CounterContract";
_40
_40
use(wasmKitChai);
_40
_40
describe("counter", () => {
_40
async function setup() {
_40
const contract_owner = await getAccountByName("account_0");
_40
const contract = new CounterContract();
_40
await contract.setupClient();
_40
_40
return { contract_owner, contract };
_40
}
_40
_40
it("deploy and init", async () => {
_40
const runTs = String(new Date());
_40
const { contract_owner, contract } = await setup();
_40
const deploy_response = await contract.deploy(
_40
contract_owner,
_40
{ // custom fees
_40
amount: [{ amount: "90000", denom: "untrn" }],
_40
gas: "35000000",
_40
}
_40
);
_40
console.log(deploy_response);
_40
_40
const contract_info = await contract.instantiate(
_40
{
_40
"count": 102,
_40
},
_40
`deploy test ${runTs}`,
_40
contract_owner,
_40
undefined, // tokens to tranfer
_40
// customFees, // custom fess here
_40
);
_40
console.log(contract_info);
_40
});
_40
});

Following is a breakdown of the above script:

  • Import expect and use from chai, Contract, getAccountByName and wasmkitChai from wasmkit and add wasmkit asserts to chai using use(wasmkitChai).

_10
import { use } from "chai";
_10
import { getAccountByName, wasmKitChai } from "@kubiklabs/wasmkit";
_10
_10
import { CounterContract } from "../artifacts/typescript_schema/CounterContract";
_10
_10
use(wasmKitChai);

  • setup() method does the initial common steps for each test, such as creating Account objects, creating Contract objects, parsing contract's schema files and deploying the contract.

_10
async function setup() {
_10
const contract_owner = await getAccountByName("account_0");
_10
const contract = new CounterContract();
_10
await contract.setupClient();
_10
_10
return { contract_owner, contract };
_10
}

Note: It is fine to have deploy, instantiate in each test as they are not executed multiple times for a given contract. Moving these steps in the setup() method is fine.

Chai matchers

A set of chai matchers, makes your test easy to write and read. Before you can start using the matchers, you have to tell chai to use the wasmkitChai plugin:


_10
import { use, expect } from "chai";
_10
import { getAccountByName, wasmKitChai } from "@kubiklabs/wasmkit";
_10
_10
use(wasmKitChai);

Below is the list of available matchers:

1. Execution Response

Testing what response was received after transaction execution:


_10
await expect(
_10
contract.balance({ "address": contract_owner.account.address })
_10
).to.respondWith({"balance": "50000000"});