0
0
Blockchain / Solidityprogramming~10 mins

Hardhat testing setup in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Hardhat testing setup
Write test file
Run 'npx hardhat test'
Hardhat compiles contracts
Hardhat deploys contracts to local network
Tests execute functions
Assertions check results
Report pass/fail
Exit
The flow shows writing tests, running Hardhat test command, compiling, deploying, executing tests, checking results, and reporting.
Execution Sample
Blockchain / Solidity
const { expect } = require("chai");
describe("SimpleStorage", function () {
  it("Should store and retrieve value", async function () {
    const SimpleStorage = await ethers.getContractFactory("SimpleStorage");
    const storage = await SimpleStorage.deploy();
    await storage.deployed();
    await storage.store(42);
    expect(await storage.retrieve()).to.equal(42);
  });
});
This test deploys a SimpleStorage contract, stores 42, and checks if retrieve returns 42.
Execution Table
StepActionEvaluationResult
1Load test fileTest code readyTest suite defined
2Run 'npx hardhat test'Hardhat startsCompiling contracts
3Compile SimpleStorage.solCompilation successBytecode ready
4Deploy SimpleStorage contractDeployment successContract instance created
5Call store(42)Transaction minedValue stored in contract
6Call retrieve()Returns stored value42
7Assert retrieved value == 42Check passesTest passes
8Report resultsAll tests passedExit test run
💡 All tests passed, Hardhat test run completes successfully
Variable Tracker
VariableStartAfter DeployAfter store(42)After retrieve()Final
SimpleStorageundefinedcontract instancecontract instancecontract instancecontract instance
storedValueundefinedundefined424242
retrievedValueundefinedundefinedundefined4242
Key Moments - 3 Insights
Why do we need to deploy the contract before calling its functions?
Because the contract must exist on the local blockchain network to interact with it. See execution_table step 4 where deployment creates the contract instance.
What happens if the assertion fails?
The test run stops reporting failure at that step. In execution_table step 7, if the value was not 42, the test would fail here.
Why does Hardhat compile contracts before running tests?
Because tests interact with compiled bytecode. Without compilation (step 3), deployment and function calls cannot happen.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of storedValue after step 5?
Aundefined
B42
C0
Dnull
💡 Hint
Check variable_tracker row for storedValue after 'After store(42)' column
At which step does Hardhat deploy the contract to the local network?
AStep 3
BStep 5
CStep 4
DStep 6
💡 Hint
See execution_table action column for deployment step
If the assertion in step 7 fails, what happens next?
ATest run reports failure and stops
BTest run continues with next test
CContract redeploys automatically
DStored value resets to 0
💡 Hint
Refer to key_moments explanation about assertion failure
Concept Snapshot
Hardhat testing setup:
- Write tests in JavaScript using Mocha and Chai
- Run tests with 'npx hardhat test'
- Hardhat compiles contracts before tests
- Contracts deploy to local network for testing
- Tests call contract functions and assert results
- Test results show pass/fail status
Full Transcript
This visual execution shows how Hardhat testing setup works step-by-step. First, you write a test file that uses Mocha and Chai to describe tests. When you run 'npx hardhat test', Hardhat compiles your smart contracts. Then it deploys the contracts to a local blockchain network. After deployment, the test calls contract functions like store and retrieve. The test checks if the retrieved value matches the expected value using assertions. If all assertions pass, the test run reports success and exits. Variables like storedValue change as functions execute. Key moments include understanding deployment before interaction, the importance of compilation, and what happens if assertions fail. The quizzes help confirm understanding of these steps and variable states. This setup helps developers test smart contracts safely and automatically before deploying to real networks.