yarn test. In case you didn’t, then we recommend you copy the necessary node files (e.g. package.json) from one of our templates.
For Go contracts, unit tests must be run with the near-go CLI, which uses TinyGo under the hood. The SDK’s system package uses TinyGo-specific //go:wasmimport declarations that cannot be compiled by the standard Go toolchain.
MockSystem from near-sdk-go to simulate the NEAR environment locally without a blockchain.
PrerequisitesYou need the
near-go CLI installed. If you followed the Quickstart, everything is already set up — TinyGo and all dependencies are handled by the install.sh script automatically.Snippet I: Testing a Counter
The tests in the Counter Example rely on basic functions to check that theincrement, decrement, and reset methods work properly.
- 🦀 Rust
- 🐹 GO
Snippet II: Modifying the Context
While doing unit testing you can modify the Environment variables through theVMContextBuilder. This will enable you to, for example, simulate calls from different users, with specific attached deposit and GAS. Here we present a snippet on how we test the donate method from our Donation Example by manipulating the predecessor and attached_deposit.
- 🦀 Rust
- 🐹 GO
⚠️ Limitations
Unit tests are useful to check for code integrity, and detect basic errors on isolated methods. However, since unit tests do not run on a blockchain, there are many things which they cannot detect. Unit tests are not suitable for:- Testing gas and storage usage
- Testing transfers
- Testing cross-contract calls
- Testing complex interactions, i.e. multiple users depositing money on the contract