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 Mochaarrow-up-right as our test runner.

Prerequisites


Environment setup

  1. Create a new project with the required dependencies and boilerplate paymaster implementations:

    zksync-cli create test-greeter

    Choose Hardhat + Solidity to setup the project repository. The contract for this guide exists under /contracts/Greeter.sol. Install dependencies:yarnnpmbun

    yarn install
  2. Add the following additional dependencies:yarnnpmbun

    yarn add -D @nomicfoundation/hardhat-chai-matchers @nomiclabs/hardhat-ethers
  3. Import @nomicfoundation/hardhat-chai-matchers into the hardhat.config.ts file:hardhat.config.ts

    import "@nomicfoundation/hardhat-chai-matchers";

    The @nomicfoundation/hardhat-chai-matchers plugin adds Ethereum specific capabilities to the Chaiarrow-up-right assertion library for testing smart contracts.

  4. Start era_test_node:

    ./target/release/era_test_node run

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:

  1. Testing greet() function: Check the returned greeting.

  2. Testing setGreeting() function: Verify the ability to update greetings.

  3. Testing Insufficient Funds: Ensure transactions fail without enough funds.

  4. 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, and Contract from zksync-ethers help us with Validium functionalities like creating wallets and interacting with contracts.

  • hre and Deployer give us hardhat specific functionalities for deploying and interacting with our contract.

  • zkSyncTestnet from our hardhat configuration provides network details of our running era_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.

  1. Initialization Before running any test, we initialize commonly used variables like the provider, wallet, deployer, and the greeter contract.

  2. Test greet() function We check that the greet function returns the initial greeting of 'Hi' after deployment.

  3. Test setGreeting() function We test that setting a new greeting updates the contract's state as expected.

  4. 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 your zksync-ethers Contract object to connect it to a different account.

  5. 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