Unit Testing Smart Contracts

It is essential to write unit tests for your smart contracts. It is a good practice to unit test 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

https://github.com/Fantom-foundation/unittestexample-hardhat

Unit testing in Truffle

https://github.com/Fantom-foundation/unittestexample-truffle

Each example repository contains the following:

  • contracts folder: contains smart contract files.

  • test folder: the unit test files are under the test folder

  • README.md file: contains instructions for compiling, testing, deploying, and verifying the smart contracts.

In the above examples, unit tests are included in one single JavaScript file. However, you can have as many tests and files as you need for your project. In the examples, there are three types of tests:

  1. Checking if a value is what is expected

  2. Checking if an event is fired with the correct arguments

  3. Checking if a revert has occurred

Last updated

© 2024 Fantom Foundation