Hardhat is a development environment to compile, deploy, test, and debug Ethereum software. It helps developers write smart contracts and test them locally.
Click to reveal answer
beginner
Which command initializes a new Hardhat project?
The command npx hardhat starts the Hardhat project setup wizard, which helps create the project structure and configuration files.
Click to reveal answer
beginner
What file contains the configuration for Hardhat testing?
The hardhat.config.js file contains settings for the Hardhat environment, including network configurations and plugins for testing.
Click to reveal answer
intermediate
How do you write a simple test in Hardhat using Mocha and Chai?
You write tests inside the test/ folder using JavaScript. Use describe blocks to group tests and it blocks for individual tests. Use expect from Chai to check results.
Click to reveal answer
beginner
What command runs the tests in a Hardhat project?
Use npx hardhat test to run all tests in the test/ directory. This runs your smart contract tests locally.
Click to reveal answer
Which folder typically contains Hardhat test files?
Atest/
Bsrc/
Ccontracts/
Dscripts/
✗ Incorrect
Hardhat test files are usually placed in the test/ folder.
What library is commonly used with Hardhat for assertions in tests?
ALodash
BReact
CExpress
DChai
✗ Incorrect
Chai is a popular assertion library used with Hardhat tests.
Which command initializes a Hardhat project?
Ahardhat start
Bnpm install hardhat
Cnpx hardhat
Dnode hardhat.js
✗ Incorrect
Running npx hardhat starts the project setup wizard.
What does npx hardhat test do?
ARuns all tests in the test folder
BCompiles smart contracts
CDeploys contracts to mainnet
DStarts a local blockchain node
✗ Incorrect
It runs all the tests written in the test/ directory.
Which file configures Hardhat settings?
Apackage.json
Bhardhat.config.js
Cindex.js
Dtruffle-config.js
✗ Incorrect
The hardhat.config.js file holds Hardhat configuration.
Explain the steps to set up a basic Hardhat testing environment from scratch.
Think about commands, folder structure, and test writing.
You got /4 concepts.
Describe how you write a simple test for a smart contract function using Hardhat and Chai.
Focus on test structure and assertion.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using Hardhat testing setup in blockchain development?
easy
A. To create user interfaces for blockchain applications
B. To deploy smart contracts directly to the main Ethereum network
C. To write and run tests for smart contracts on a local blockchain
D. To mine real cryptocurrency coins
Solution
Step 1: Understand Hardhat testing role
Hardhat testing setup is designed to help developers write and run tests for smart contracts safely.
Step 2: Identify the environment used
Tests run on a local blockchain, which means no real money or main network is involved.
Final Answer:
To write and run tests for smart contracts on a local blockchain -> Option C
Quick Check:
Hardhat testing = local smart contract tests [OK]
Hint: Hardhat tests run locally, not on mainnet [OK]
Common Mistakes:
Confusing testing with deployment to mainnet
Thinking Hardhat mines real coins
Assuming Hardhat builds user interfaces
2. Which of the following is the correct way to start a test suite in Hardhat using Mocha?
easy
A. test('MyContract', function() { ... })
B. describe('MyContract', function() { ... })
C. suite('MyContract', () => { ... })
D. context('MyContract', function() { ... })
Solution
Step 1: Recall Mocha test structure
Hardhat uses Mocha for testing, which organizes tests with describe blocks.
Step 2: Identify correct syntax
The describe function takes a string and a callback function to group tests.
Final Answer:
describe('MyContract', function() { ... }) -> Option B
Quick Check:
Mocha test suites use describe() [OK]
Hint: Use describe() to group tests in Hardhat [OK]
Common Mistakes:
Using test() instead of describe() for suites
Confusing suite() or context() with Mocha syntax
Missing function keyword or arrow function
3. Given this Hardhat test snippet, what will be the output when running the test?
D. Using arrow function in it() causes context loss
Solution
Step 1: Check function style in it()
Using arrow functions in Mocha's it blocks can cause loss of this context, which Hardhat sometimes relies on.
Step 2: Verify other parts
Imports and await usage are correct; assertion syntax is valid for checking string type.
Final Answer:
Using arrow function in it() causes context loss -> Option D
Quick Check:
Use function() not arrow in it() [OK]
Hint: Avoid arrow functions in it() blocks [OK]
Common Mistakes:
Ignoring context loss with arrow functions
Thinking deploy() is synchronous
Assuming ethers import is automatic
5. You want to test a smart contract function that transfers tokens and emits an event. Which setup correctly tests the event emission using Hardhat and Chai?
hard
A. await expect(token.transfer(addr1.address, 100)).to.emit(token, 'Transfer').withArgs(owner.address, addr1.address, 100);
B. expect(token.transfer(addr1.address, 100)).to.emit('Transfer').withArgs(owner.address, addr1.address, 100);
C. await token.transfer(addr1.address, 100); expect(token).to.emit('Transfer');
D. token.transfer(addr1.address, 100); expect(token).to.emit(token, 'Transfer');
Solution
Step 1: Understand event testing syntax
Hardhat with Chai uses await expect(transaction).to.emit(contract, eventName).withArgs(...) to test events.
Step 2: Check options for correct usage
await expect(token.transfer(addr1.address, 100)).to.emit(token, 'Transfer').withArgs(owner.address, addr1.address, 100); correctly awaits the transaction and specifies contract and event with arguments.
Step 3: Identify errors in other options
The other options either miss the await before expect, fail to specify the contract instance in to.emit(), or use incorrect syntax chaining.
Final Answer:
await expect(token.transfer(addr1.address, 100)).to.emit(token, 'Transfer').withArgs(owner.address, addr1.address, 100); -> Option A
Quick Check:
Use await expect(...).to.emit(contract, event) [OK]
Hint: Always await expect() when testing events [OK]
Common Mistakes:
Forgetting to await the expect call
Not specifying the contract in to.emit()
Calling expect on transaction result instead of promise
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.