@@ -15,126 +15,65 @@ and we'll use [Mocha](https://mochajs.org/) as our test runner.
15
15
### Prerequisites
16
16
17
17
- ` 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 ) .
19
19
20
20
---
21
21
## Environment setup
22
22
23
23
1 . Create a new project with the required dependencies and boilerplate paymaster implementations:
24
24
25
25
``` bash
26
- zksync-cli create test-greeter
26
+ zksync-cli create test-greeter --template hardhat_solidity
27
27
```
28
28
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:
30
30
31
- Install dependencies:
31
+ ` ` ` bash
32
+ ? Private key of the wallet responsible for deploying contracts (optional)
33
+ 0x7726827caac94a7f9e1b160f7ea819f172f7b6f9d2a97f992c38edeab82d4110
34
+ ` ` `
32
35
33
- ::code-group
36
+ The contract for this guide exists under ` /contracts/Greeter.sol ` .
34
37
35
- ` ` ` bash [yarn]
36
- yarn install
37
- ` ` `
38
+ 1. Install the dependencies:
39
+
40
+ ::code-group
38
41
39
42
` ` ` bash [npm]
40
43
npm install
41
44
` ` `
42
45
43
- ` ` ` bash [bun]
44
- bun install
45
- ` ` `
46
-
47
- ::
48
-
49
- 1. Add the following additional dependencies:
50
-
51
- ::code-group
52
-
53
46
` ` ` bash [yarn]
54
- yarn add -D @nomicfoundation/hardhat-chai-matchers @nomiclabs/hardhat-ethers
47
+ yarn install
55
48
` ` `
56
49
57
- ` ` ` bash [npm ]
58
- npm add @nomicfoundation/hardhat-chai-matchers @nomiclabs/hardhat-ethers --save-dev
50
+ ` ` ` bash [pnpm ]
51
+ pnpm install
59
52
` ` `
60
53
61
54
` ` ` bash [bun]
62
- bun add @nomicfoundation/hardhat-chai-matchers @nomiclabs/hardhat-ethers --dev
55
+ bun install
63
56
` ` `
64
57
65
58
::
66
59
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:
75
61
76
- 1. Start ` anvil-zksync` :
77
-
78
- ` ` ` bash
79
- ./target/release/anvil-zksync run
80
- ` ` `
62
+ :display-partial{path=" /_partials/commands/_compile" }
81
63
82
64
---
83
65
# # Run tests with Hardhat
84
66
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.
104
68
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" }
119
71
` ` `
120
72
121
73
To run this test:
122
74
123
- ::code-group
124
-
125
- ` ` ` bash [yarn]
126
- yarn test
127
- ` ` `
75
+ :display-partial{path=" /_partials/commands/_test-greeter" }
128
76
129
- ` ` ` bash [npm]
130
- npm test
131
- ` ` `
132
-
133
- ` ` ` bash [bun]
134
- bun run test
135
- ` ` `
136
-
137
- ::
138
77
You should see the following output:
139
78
140
79
` ` ` sh
@@ -153,7 +92,6 @@ Our aim is comprehensive coverage. Here are the test scenarios we will cover:
153
92
1. **Testing greet() function**: Check the returned greeting.
154
93
2. **Testing setGreeting() function**: Verify the ability to update greetings.
155
94
3. **Testing Insufficient Funds**: Ensure transactions fail without enough funds.
156
- 4. **Event Emission**: Ensure an event is emitted when changing the greeting.
157
95
158
96
Each of these test cases will rely on a common setup,
159
97
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.
162
100
Let' s refactor our test file with the provided script:
163
101
164
102
::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" }
240
106
` ` `
241
107
::
242
108
::
243
109
244
110
To run this test:
245
111
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" }
261
113
262
114
You should see the following output:
263
115
@@ -266,37 +118,23 @@ You should see the following output:
266
118
✔ Should return the new greeting once it' s changed (211ms)
267
119
✔ Should set a new greeting and return it (2682ms)
268
120
✔ Should fail when insufficient funds (299ms)
269
- ✔ Should emit an event when the greeting is changed (2939ms)
270
121
271
- 4 passing (6s)
122
+ 3 passing (6s)
272
123
```
273
124
274
125
## Understanding the test file
275
126
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:
277
128
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" }
284
131
` ` `
285
132
286
133
This section imports all necessary utilities and configurations needed to run our tests.
287
134
288
135
- ` 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.
300
138
301
139
#### Main Test Suite
302
140
@@ -337,11 +175,3 @@ Each test or nested suite inside provides specific scenarios or functionalities
337
175
```javascript
338
176
it("Should fail when insufficient funds", async function () { ... });
339
177
```
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
- ` ` `
0 commit comments