Skip to content

Commit b7ac911

Browse files
fix: hh testing guide, docs cleanup (#424)
- fixes the hardhat testing guide - fixes hardhat migration guide - replaces some hard-coded code snippets with imports - replaces dropdown html with panel components - fixes minor typos
1 parent b7f1817 commit b7ac911

37 files changed

+2943
-594
lines changed

content/00.zksync-era/10.guides/30.testing/10.testing-with-hardhat.md

Lines changed: 31 additions & 201 deletions
Original file line numberDiff line numberDiff line change
@@ -15,126 +15,65 @@ and we'll use [Mocha](https://mochajs.org/) as our test runner.
1515
### Prerequisites
1616

1717
- `zksync-cli` installed from the [zksync-cli section](/build/zksync-cli).
18-
- `anvil-zksync` installed and running. See [anvil-zksync](/build/test-and-debug/in-memory-node).
18+
- `anvil-zksync` installed. See [anvil-zksync](/build/test-and-debug/in-memory-node).
1919

2020
---
2121
## Environment setup
2222

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

2525
```bash
26-
zksync-cli create test-greeter
26+
zksync-cli create test-greeter --template hardhat_solidity
2727
```
2828

29-
Choose `Hardhat + Solidity` to setup the project repository. The contract for this guide exists under `/contracts/Greeter.sol`.
29+
Use the private key below to use a pre-configured rich wallet for deployment and testing:
3030

31-
Install dependencies:
31+
```bash
32+
? Private key of the wallet responsible for deploying contracts (optional)
33+
0x7726827caac94a7f9e1b160f7ea819f172f7b6f9d2a97f992c38edeab82d4110
34+
```
3235

33-
::code-group
36+
The contract for this guide exists under `/contracts/Greeter.sol`.
3437

35-
```bash [yarn]
36-
yarn install
37-
```
38+
1. Install the dependencies:
39+
40+
::code-group
3841

3942
```bash [npm]
4043
npm install
4144
```
4245

43-
```bash [bun]
44-
bun install
45-
```
46-
47-
::
48-
49-
1. Add the following additional dependencies:
50-
51-
::code-group
52-
5346
```bash [yarn]
54-
yarn add -D @nomicfoundation/hardhat-chai-matchers @nomiclabs/hardhat-ethers
47+
yarn install
5548
```
5649

57-
```bash [npm]
58-
npm add @nomicfoundation/hardhat-chai-matchers @nomiclabs/hardhat-ethers --save-dev
50+
```bash [pnpm]
51+
pnpm install
5952
```
6053

6154
```bash [bun]
62-
bun add @nomicfoundation/hardhat-chai-matchers @nomiclabs/hardhat-ethers --dev
55+
bun install
6356
```
6457

6558
::
6659

67-
1. Import `@nomicfoundation/hardhat-chai-matchers` into the `hardhat.config.ts` file:
68-
69-
```typescript [hardhat.config.ts]
70-
import "@nomicfoundation/hardhat-chai-matchers";
71-
```
72-
73-
The `@nomicfoundation/hardhat-chai-matchers` plugin adds Ethereum specific capabilities
74-
to the [Chai](https://www.chaijs.com/) assertion library for testing smart contracts.
60+
1. Compile the contracts:
7561

76-
1. Start `anvil-zksync`:
77-
78-
```bash
79-
./target/release/anvil-zksync run
80-
```
62+
:display-partial{path="/_partials/commands/_compile"}
8163

8264
---
8365
## Run tests with Hardhat
8466

85-
Under the `/test` directory there is a `main.test.ts` . The initial test checks if our `Greeter` contract returns the set greeting.
86-
87-
```typescript [/test/main.test.ts]
88-
import { expect } from "chai";
89-
import { Wallet, Provider, Contract } from "zksync-ethers";
90-
import * as hre from "hardhat";
91-
import { Deployer } from "@matterlabs/hardhat-zksync";
92-
import { zkSyncTestnet } from "../hardhat.config";
93-
94-
const RICH_WALLET_PK = "0x7726827caac94a7f9e1b160f7ea819f172f7b6f9d2a97f992c38edeab82d4110";
95-
96-
async function deployGreeter(deployer: Deployer): Promise<Contract> {
97-
const artifact = await deployer.loadArtifact("Greeter");
98-
return await deployer.deploy(artifact, ["Hi"]);
99-
}
100-
101-
describe("Greeter", function () {
102-
it("Should return the new greeting once it's changed", async function () {
103-
const provider = new Provider(zkSyncTestnet.url);
67+
Under the `/test` directory there is a `greeter.test.ts` file. The initial test checks if our `Greeter` contract returns the set greeting.
10468

105-
const wallet = new Wallet(RICH_WALLET_PK, provider);
106-
const deployer = new Deployer(hre, wallet);
107-
108-
const greeter = await deployGreeter(deployer);
109-
110-
expect(await greeter.greet()).to.eq("Hi");
111-
112-
const setGreetingTx = await greeter.setGreeting("Hola, mundo!");
113-
// wait until the transaction is mined
114-
await setGreetingTx.wait();
115-
116-
expect(await greeter.greet()).to.equal("Hola, mundo!");
117-
});
118-
});
69+
```typescript [/test/greeter.test.ts]
70+
:code-import{filePath="hardhat-sol-template/test/greeter.test.ts"}
11971
```
12072

12173
To run this test:
12274

123-
::code-group
124-
125-
```bash [yarn]
126-
yarn test
127-
```
75+
:display-partial{path="/_partials/commands/_test-greeter"}
12876

129-
```bash [npm]
130-
npm test
131-
```
132-
133-
```bash [bun]
134-
bun run test
135-
```
136-
137-
::
13877
You should see the following output:
13978

14079
```sh
@@ -153,7 +92,6 @@ Our aim is comprehensive coverage. Here are the test scenarios we will cover:
15392
1. **Testing greet() function**: Check the returned greeting.
15493
2. **Testing setGreeting() function**: Verify the ability to update greetings.
15594
3. **Testing Insufficient Funds**: Ensure transactions fail without enough funds.
156-
4. **Event Emission**: Ensure an event is emitted when changing the greeting.
15795
15896
Each of these test cases will rely on a common setup,
15997
which involves creating a provider connected to the %%zk_testnet_name%%, initializing a wallet with a known private key,
@@ -162,102 +100,16 @@ and deploying the `Greeter` contract.
162100
Let's refactor our test file with the provided script:
163101
164102
::drop-panel
165-
::panel{label="test/main.test.ts"}
166-
```typescript [main.test.ts]
167-
import { expect } from "chai";
168-
import { Wallet, Provider, Contract } from "zksync-ethers";
169-
import * as hre from "hardhat";
170-
import { Deployer } from "@matterlabs/hardhat-zksync";
171-
import { zkSyncTestnet } from "../hardhat.config";
172-
173-
const RICH_WALLET_PK = "0x7726827caac94a7f9e1b160f7ea819f172f7b6f9d2a97f992c38edeab82d4110";
174-
175-
// Deploy the Greeter contract
176-
async function deployGreeter(deployer: Deployer): Promise<Contract> {
177-
// Load the Greeter contract artifact
178-
const artifact = await deployer.loadArtifact("Greeter");
179-
// Deploy the contract with an initial greeting
180-
return await deployer.deploy(artifact, ["Hi"]);
181-
}
182-
183-
describe("Greeter", function () {
184-
let greeter;
185-
let wallet;
186-
let deployer;
187-
188-
// Initialize commonly used variables before running the tests
189-
before(async function () {
190-
// Create a provider connected to the ZKsync testnet
191-
const provider = new Provider(zkSyncTestnet.url);
192-
193-
// Create a wallet instance using the rich wallet's private key
194-
wallet = new Wallet(RICH_WALLET_PK, provider);
195-
// Create a deployer instance for contract deployments
196-
deployer = new Deployer(hre, wallet);
197-
// Deploy the Greeter contract
198-
greeter = await deployGreeter(deployer);
199-
});
200-
201-
// Test the greet() function
202-
it("Should return the new greeting once it's changed", async function () {
203-
// Ensure the greet function returns the initial greeting after deployment
204-
expect(await greeter.greet()).to.eq("Hi");
205-
});
206-
207-
// Test the setGreeting() function
208-
it("Should set a new greeting and return it", async function () {
209-
// Set a new greeting
210-
const setGreetingTx = await greeter.setGreeting("Hola, mundo!");
211-
// Wait for the transaction to be confirmed
212-
await setGreetingTx.wait();
213-
214-
// Ensure the greet function returns the newly set greeting
215-
expect(await greeter.greet()).to.equal("Hola, mundo!");
216-
});
217-
218-
// Test for lack of funds (or other tx failures)
219-
it("Should fail when insufficient funds", async function () {
220-
// Create an empty wallet with no funds
221-
const userWallet = Wallet.createRandom();
222-
// Connect the empty wallet to the greeter contract and attempt to set a new greeting
223-
try {
224-
await greeter.connect(userWallet).setGreeting("fail");
225-
// The following line should not be reached if the transaction fails
226-
expect(true).to.equal(false);
227-
} catch (e) {
228-
// Expect an error to be thrown for the transaction
229-
expect(e).to.exist;
230-
}
231-
});
232-
233-
// Test event emission
234-
it("Should emit an event when the greeting is changed", async function () {
235-
const newGreeting = "Bonjour, monde!";
236-
// Use the provided .emit method to test event emissions
237-
await expect(greeter.setGreeting(newGreeting)).to.emit(greeter, "GreetingChanged").withArgs(newGreeting);
238-
});
239-
});
103+
::panel{label="test/greeter.test.ts"}
104+
```typescript [greeter.test.ts]
105+
:code-import{filePath="hardhat-sol-template/test/greeter-expanded.test.ts"}
240106
```
241107
::
242108
::
243109
244110
To run this test:
245111
246-
::code-group
247-
248-
```bash [yarn]
249-
yarn test
250-
```
251-
252-
```bash [npm]
253-
npm test
254-
```
255-
256-
```bash [bun]
257-
bun run test
258-
```
259-
260-
::
112+
:display-partial{path="/_partials/commands/_test-greeter"}
261113
262114
You should see the following output:
263115
@@ -266,37 +118,23 @@ You should see the following output:
266118
✔ Should return the new greeting once it's changed (211ms)
267119
✔ Should set a new greeting and return it (2682ms)
268120
✔ Should fail when insufficient funds (299ms)
269-
✔ Should emit an event when the greeting is changed (2939ms)
270121
271-
4 passing (6s)
122+
3 passing (6s)
272123
```
273124
274125
## Understanding the test file
275126
276-
Have a look at the `test/main.test.ts` file's imports:
127+
Have a look at the `test/greeter.test.ts` file's imports:
277128

278-
```typescript [test/main.test.ts]
279-
import { expect } from "chai";
280-
import { Wallet, Provider, Contract } from "zksync-ethers";
281-
import * as hre from "hardhat";
282-
import { Deployer } from "@matterlabs/hardhat-zksync";
283-
import { zkSyncTestnet } from "../hardhat.config";
129+
```typescript [test/greeter.test.ts]
130+
:code-import{filePath="hardhat-sol-template/test/greeter-expanded.test.ts:imports"}
284131
```
285132

286133
This section imports all necessary utilities and configurations needed to run our tests.
287134

288135
- `expect` from Chai provides assertion functionalities for our tests.
289-
- `Wallet`, `Provider`, and `Contract` from `zksync-ethers` help us with ZKsync functionalities like creating wallets and interacting with contracts.
290-
- `hre` and `Deployer` give us hardhat specific functionalities for deploying and interacting with our contract.
291-
- `zkSyncTestnet` from our hardhat configuration provides network details of our running `anvil-zksync.`
292-
293-
#### Contract Deployment Utility
294-
295-
```javascript
296-
async function deployGreeter(deployer: Deployer): Promise<Contract> { ... }
297-
```
298-
299-
This utility function simplifies deploying the Greeter contract for our tests.
136+
- `Wallet`, and the `Contract` type from `ethers` help us creating a wallet and defining a contract's type.
137+
- `hre` gives us hardhat specific functionalities for deploying and interacting with our contract.
300138
301139
#### Main Test Suite
302140
@@ -337,11 +175,3 @@ Each test or nested suite inside provides specific scenarios or functionalities
337175
```javascript
338176
it("Should fail when insufficient funds", async function () { ... });
339177
```
340-
341-
5. **Test event emission**
342-
343-
We test the emission of an event when the greeting changes in the contract making use of the `hardhat-chai-matchers`.
344-
345-
```javascript
346-
it("Should emit an event when the greeting is changed", async function () { ... });
347-
```

content/00.zksync-era/40.tooling/10.local-setup/10.anvil-zksync-node.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ The Foundry-ZKsync suite installs `anvil-zksync` alongside `forge`, and `cast`.
2020

2121
If you want to install it separately, follow the instructions for your preferred method below:
2222

23-
Install `anvil-zksync` with from the below options:
23+
Install `anvil-zksync` with one of the below options:
2424

2525
::drop-panel
2626

content/00.zksync-era/40.tooling/20.hardhat/10.installation.md

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,7 @@ zksync-cli create <project-name> --template hardhat_vyper
100100

101101
Ensure your smart contracts are located in the `contracts` folder of your project. To compile them, use:
102102

103-
::code-group
104-
105-
```bash [yarn]
106-
yarn hardhat compile
107-
```
108-
109-
```bash [npx]
110-
npx hardhat compile
111-
```
112-
113-
::
103+
:display-partial{path="/_partials/commands/_compile"}
114104

115105
This will compile your contracts and generate the necessary artifacts in the `artifacts-zk` and `cache-zk` folders.
116106

content/00.zksync-era/40.tooling/20.hardhat/20.guides/10.getting-started.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ The default network is set to `ZKsyncEraSepolia`, but mainnet and local test nod
8484
```
8585
8686
::callout{icon="i-heroicons-information-circle" color="blue"}
87-
For local ZKsync testing, modify the default network name.
87+
For local ZKsync testing, modify the default network name, and
88+
add `import dotenv from "dotenv"; dotenv.config();` to use a `.env` file.
8889
::
8990
9091
::callout{icon="i-heroicons-information-circle" color="blue"}

0 commit comments

Comments
 (0)