Hardhat - Testing Contracts
Learn how to test your smart contracts using Hardhat.
Verify your smart contracts work as intended before you deploy them by writing tests.
Testing Smart Contracts
Update Hardhat configuration
Ensure your Hardhat configuration file is configured to use zksolc
, as outlined in the installation guide:
Install zksync-ethers
The zksync-ethers package provides a modified version of the ethers library that is compatible with Abstract and the ZKsync VM. Install the package by running the following command:
Write test definitions
Write test definitions inside the /test
directory, for example, test/HelloWorld.test.ts
.
Add a deployer private key
Create a new configuration variable called DEPLOYER_PRIVATE_KEY
that contains the private key of a wallet you want to deploy the contract from.
Enter the private key of a new wallet you created for this step.
Use one of the rich wallets as the DEPLOYER_PRIVATE_KEY
when using a local node.
Run the tests
Run the tests by running the following command:
Running a local node
The zksync-cli package provides a command-line interface for instantiating local nodes.
Run a local node as your test environment is beneficial for many reasons:
- Speed: Local nodes are faster than testnet/mainnet, increasing iteration speed.
- Rich wallets: Local nodes come with “rich wallets” pre-funded with ETH.
- Isolation: Local nodes are separate environments with no existing state.
Run a local node
Docker is required to run a local node. Installation guide ↗
Run a local node by running the following command:
Select the anvil-zksync
option when prompted:
This will start a local node and a development wallet for you to use:
Add the local node as a Hardhat network
Add the local node as a network in your Hardhat configuration file:
Update the deployer private key configuration variable
Update the DEPLOYER_PRIVATE_KEY
configuration variable to use one of the pre-funded rich wallet private keys.
Enter the private key of one of the rich wallets.
Use one of the rich wallets as the DEPLOYER_PRIVATE_KEY
when using a local node.
Run the tests
Run the tests on the local node using the following command:
Rich Wallets
The local node includes pre-configured “rich” accounts for testing:
Same mnemonic rich wallets
Mnemonic: stuff slice staff easily soup parent arm payment cotton trade scatter struggle
Was this page helpful?