Test Smart Contract Functionality with Ethers and Hardhat

Share this video with your friends

Send Tweet

Tests are important, as always but it may be more important when you are developing an smart contract. Why? Because the blockchain is immutable.

The main difference between Solidity development and other languages or environment is that after the implementation of an smart contract in the Ethereum network the contract is immutable, meaning that it will never be modified or updated again.

The first code that get's deployed is the one that will remains unchanged for the rest of time. This is because one the main concerns of Solidity is security. If there's a flaw in your contract code, there is no way to patch it later. You would have to tell to the users of the contract to use a different contract (identified by an address) to get the fix.

Sounds uncomfortable right? But, believe it or not, this is a feature. The code that your wrote it becomes a law. IF you read the code of an smart contract (and verified that that code is actually the one deployed) you can be sure that every time you call a function it will behave exactly as the code says it.

So you need to be sure before deploy and the best way to get that confidence is by testing.

Hardhat (the development environment of choice for this course) offers the tools to test the smart contract using Javascript or Typescript by leveraging the power of Ethers.js to interact with the Ethereum network and Waffle, a testing library built on top of Ethere.js

You already dip your toes on a bit of testing code, now let's add a more robust test to check that the contract behaves as expected.