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:
_40import { use } from "chai";_40import { getAccountByName, wasmKitChai } from "@kubiklabs/wasmkit";_40_40import { CounterContract } from "../artifacts/typescript_schema/CounterContract";_40_40use(wasmKitChai);_40_40describe("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
anduse
from chai,Contract
,getAccountByName
andwasmkitChai
from wasmkit and add wasmkit asserts to chai usinguse(wasmkitChai)
.
_10import { use } from "chai";_10import { getAccountByName, wasmKitChai } from "@kubiklabs/wasmkit";_10_10import { CounterContract } from "../artifacts/typescript_schema/CounterContract";_10_10use(wasmKitChai);
setup()
method does the initial common steps for each test, such as creatingAccount
objects, creatingContract
objects, parsing contract's schema files and deploying the contract.
_10async 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:
_10import { use, expect } from "chai";_10import { getAccountByName, wasmKitChai } from "@kubiklabs/wasmkit";_10_10use(wasmKitChai);
Below is the list of available matchers:
1. Execution Response
Testing what response was received after transaction execution:
_10await expect(_10 contract.balance({ "address": contract_owner.account.address })_10).to.respondWith({"balance": "50000000"});