Mastering Blockchain Development: Hardhat Implementation on Testing Smart Contract With TypeScript

Mastering web 3 part 2— smart contract testing

Arista Indrajaya
Better Programming

--

Freepik Image from @pch.vector

Let’s continue our tutorial series from this Part 1, we have done our first smart hello contract and we have already had success compiling. And the next step, we should test our code to make sure that what we write on the Solidity file is as we expected.

In Hardhat we need to add some libraries again and don’t forget to make it stay in our development environment.

$ yarn add -D @nomiclabs/hardhat-ethers ethers @nomiclabs/hardhat-waffle ethereum-waffle chai

And then you can also add the typescript configuration, like this:

$ yarn add --save-dev ts-node typescript

In the last one, you should add the testing types and most of the smart contract developers will use chai as the primary testing libraries (in this case we need typescript configuration too for chai).

$ yarn add --save-dev chai @types/node @types/mocha @types/chai

Because we have already installed and will implement some typescript, don’t forget to change the hardhat.config.js file to become hardhat.config.ts. It will automatically change our file into a typescript based file. After that, add on the top of the line from the file hardhat.config.ts this code:

Test Contract

Now, let’s write some tests. Remember, based on our explanation before about the project structure, we should create a test folder before we create the test file(s). And then you can name the test file(s) whatever you want (don’t forget we used a typescript-based file).

Import the Nomiclabs hardhat (you can see it here for more explanation), but the simplify of it, it’s the plugin that allows us to interact with the Ethereum blockchain in a simple way (if you have experience on using Truffle, it will need to run local blockchain before running all of Truffle services), so that’s why Hardhat have a little bit easiness for me personally, and safe my memory usage when I develop blockchain in our local computer, for more about hardhat you can see (here), how is Hardhat working?

The flow that runs in our test file, will make communication with our compiled contracts (ABI) that are saved on the artifacts/contracts folder.

And then the binary code will make interacting with the Ethereum node.
So, here’s the code test that we will be using to test our smart contract and then call our hello() function. Here’s the code:

Then run:

$ npx hardhat test

At the first, you will see an error, just like this (I hope it will be solved), and then you just run again the recommendation commands on this error, or you can follow the step below about what should be written in our command line.

For solving this error you can add this command:

$ yarn add -D ts-node...
//After done, run test again
$ npx hardhat test

And here’s the result should be:

Conclusion

So, what’s just happened? First of all, the simple explanation is, we test our function inside our smart contract before, that called hello(), so the function hello() will be run and provide string “Hello, World from Web3”, if we change our test with another string, it will cause an error and the test is failed.

And for more explanation, where we can interact with our smart contract? I don’t see anything running outside our project. Yes, that’s true. Hardhat has the ability to run up the Ethereum blockchain instantly according to our purpose.

So, if you see in our last line of code that calls the hello() function, that’s the power of hardhat that can make Ethereum network virtually running inside our project, call the hello() function to show the “Hello, World from Web3” string to test.

--

--