0
0
Blockchain / Solidityprogramming~10 mins

Testing with ethers.js in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Testing with ethers.js
Write test script
Setup ethers.js provider and signer
Deploy or connect to contract
Call contract functions
Check results with assertions
Report pass/fail
End test
The test script sets up ethers.js, deploys or connects to a contract, calls functions, checks results, and reports success or failure.
Execution Sample
Blockchain / Solidity
import { expect } from 'chai';
import { ethers } from 'hardhat';

describe('SimpleStorage', () => {
  it('stores and retrieves a value', async () => {
    const SimpleStorage = await ethers.getContractFactory('SimpleStorage');
    const storage = await SimpleStorage.deploy();
    await storage.deployed();

    await storage.store(42);
    expect(await storage.retrieve()).to.equal(42n);
  });
});
This test deploys a SimpleStorage contract, stores the number 42, then checks if retrieving returns 42.
Execution Table
StepActionEvaluationResult
1Import chai and ethersModules loadedReady to use expect and ethers
2Get contract factory for SimpleStorageFactory createdCan deploy contract
3Deploy SimpleStorage contractTransaction sentContract deployed at address
4Wait for deployment to completeDeployment minedContract ready
5Call store(42) on contractTransaction sentValue 42 stored
6Call retrieve() on contractReturns 42Value retrieved is 42
7Assert retrieved value equals 42Check passesTest passes
8Test endsNo errorsTest suite completes successfully
💡 Test ends after assertion passes confirming contract stores and retrieves value correctly
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 6Final
SimpleStorageundefinedContract factory objectContract factory objectContract factory objectContract factory object
storageundefinedContract instance deployedContract instance deployedContract instance deployedContract instance deployed
storedValueundefinedundefined42 stored42 retrieved42
Key Moments - 3 Insights
Why do we wait for deployment to complete before calling store()?
Because the contract must be fully deployed on the blockchain before we can interact with it. See step 4 in execution_table where deployment is mined before proceeding.
What happens if the retrieved value does not equal 42?
The assertion in step 7 would fail, causing the test to fail and report an error. This ensures the contract logic is correct.
Why do we use async/await in the test?
Because blockchain calls are asynchronous and we must wait for transactions to complete before checking results, as shown in steps 3, 4, 5, and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of storedValue after step 5?
Aundefined
BContract deployed
C42 stored
D42 retrieved
💡 Hint
Check the 'storedValue' row in variable_tracker after step 5
At which step does the test confirm the contract stores the value correctly?
AStep 7
BStep 5
CStep 3
DStep 6
💡 Hint
Look at the assertion step in execution_table where the test passes
If we remove await before storage.store(42), what likely happens?
ATest runs faster and passes
BTest fails because store transaction may not complete before retrieve
CNo change, test still passes
DDeployment fails
💡 Hint
Recall why async/await is used in key_moments and steps 5-6 in execution_table
Concept Snapshot
Testing with ethers.js:
- Import ethers and chai for blockchain and assertions
- Deploy or connect to contract asynchronously
- Call contract functions with await
- Use expect() to check results
- Tests pass if assertions succeed
- Async/await ensures correct order of blockchain calls
Full Transcript
This visual execution shows how to test a smart contract using ethers.js. First, the test script imports necessary modules. Then it gets the contract factory and deploys the contract, waiting for deployment to finish. Next, it calls the store function to save a value and waits for the transaction. Then it calls retrieve to get the stored value. Finally, it asserts the retrieved value equals the stored one. The test passes if the assertion succeeds. Variables like the contract instance and stored value change step by step. Key moments include waiting for deployment and using async/await to handle asynchronous blockchain calls. The quiz checks understanding of variable states and test flow.