Canister unit testing

Hi,

I’ve been developing canisters in both Rust and Motko for a while now. And one of the things that I always wanted was to create unit tests, that I can quickly run and check if my canisters are behaving correctly. Working with DFX make this whole ordeal even harder. To solve this problem I’ve created npm package lightic to actually be able to quickly test canisters. I’ve tested it with jest, mocha and vitest and it works quite well.

I’ve prepared examples on how can you unit test your canister code:

Tests using vitest

Tests using mocha

What this package does, it loads wasm file in to nodejs memory and gives you an api (really similar to @dfinity packages) to run queries/updates against given code. As the inter canister calls are also working, you can test multi canister projects. For eg. you can test ICP ledger transactions, NNS integration and many others.

In order to start testing, add npm package lightic and create TestContext in your js/ts file. It gives you possibility to install canisters and execute queries/updates against it.

I’ve tested it against dfx and it is faster. There is no need to wait for dfx to start replica and deploy your canisters.

Do you have some ideas on what can be added that lightic is usefull in your project?

8 Likes