Unit Testing Smart Contracts

It is essential to write unit tests for your smart contracts. It is a good practice to unit testing before deploying your smart contracts.
Smart contract unit tests are written in Javascript.
In this tutorial, we provide two examples of unit testing using Hardhat and Truffle.

Unit testing in Hardhat

Unit testing in Truffle

Each example repository contains the following:
  • contracts folder: contains smart contract files.
  • test folder: the unit test files are under the test folder
  • file: contains instructions for compiling, testing, deploying and verifying of the smart contracts.
In the above examples, unit tests are included in one single Javascript file. But you can have as many tests and files as you need for your project. In the examples, there are three types of tests:
  1. 1.
    checking if a value is what is expected
  2. 2.
    checking if an event is fired with the correct arguments
  3. 3.
    checking if a revert has occurred