Hardhat
Learn how to test on era-test-node with Hardhat.
In the world of decentralized applications, the margin for error is remarkably narrow. A single mistake in a contract can have catastrophic implications. For those seeking an efficient method to test and refine their contracts, this guide showcases how to utilize Hardhat and era_test_node
for all testing needs.
To test our contract, we are going to use Hardhat and era_test_node
for rapid local development. In our tests we're going to use zksync-ethers
to interact with the Greeter
contract, and we'll use Mocha as our test runner.
Prerequisites
zksync-cli
installed from the zksync-cli section.era_test_node
installed and running. See In-memory Node.
Environment setup
Create a new project with the required dependencies and boilerplate paymaster implementations:
Choose
Hardhat + Solidity
to setup the project repository. The contract for this guide exists under/contracts/Greeter.sol
. Install dependencies:yarnnpmbunAdd the following additional dependencies:yarnnpmbun
Import
@nomicfoundation/hardhat-chai-matchers
into thehardhat.config.ts
file:hardhat.config.tsThe
@nomicfoundation/hardhat-chai-matchers
plugin adds Ethereum specific capabilities to the Chai assertion library for testing smart contracts.Start
era_test_node
:
Run tests with Hardhat
Under the /test
directory there is a main.test.ts
. The initial test checks if our Greeter
contract returns the set greeting.
/test/main.test.ts
To run this test:
yarnnpmbun
You should see the following output:
Expand test coverage
Our aim is comprehensive coverage. Here are the test scenarios we will cover:
Testing greet() function: Check the returned greeting.
Testing setGreeting() function: Verify the ability to update greetings.
Testing Insufficient Funds: Ensure transactions fail without enough funds.
Event Emission: Ensure an event is emitted when changing the greeting.
Each of these test cases will rely on a common setup, which involves creating a provider connected to the Validium Sepolia Testnet, initializing a wallet with a known private key, and deploying the Greeter
contract.
Let's refactor our test file with the provided script:
test/main.test.ts
To run this test:
yarnnpmbun
You should see the following output:
Understanding the test file
Have a look at the test/main.test.ts
file's imports:
test/main.test.ts
This section imports all necessary utilities and configurations needed to run our tests.
expect
from Chai provides assertion functionalities for our tests.Wallet
,Provider
, andContract
fromzksync-ethers
help us with Validium functionalities like creating wallets and interacting with contracts.hre
andDeployer
give us hardhat specific functionalities for deploying and interacting with our contract.zkSyncTestnet
from our hardhat configuration provides network details of our runningera_test_node.
Contract Deployment Utility
This utility function simplifies deploying the Greeter contract for our tests.
Main Test Suite
Here, we've declared our main test suite. Each test or nested suite inside provides specific scenarios or functionalities we want to test regarding the Greeter contract.
Initialization Before running any test, we initialize commonly used variables like the provider, wallet, deployer, and the greeter contract.
Test greet() function We check that the greet function returns the initial greeting of 'Hi' after deployment.
Test setGreeting() function We test that setting a new greeting updates the contract's state as expected.
Test insufficient funds Here, we simulate a scenario where an empty wallet (with no funds) tries to set a new greeting. We make use of the
connect
method on yourzksync-ethers
Contract object to connect it to a different account.Test event emission We test the emission of an event when the greeting changes in the contract making use of the
hardhat-chai-matchers
.
Last updated