Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

Hardhat testing setup in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Hardhat Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Hardhat test snippet?

Consider this simple Hardhat test for a smart contract that stores a number. What will the test output when run?

Blockchain / Solidity
describe('SimpleStorage', function () {
  it('should return the initial value', async function () {
    const SimpleStorage = await ethers.getContractFactory('SimpleStorage');
    const storage = await SimpleStorage.deploy();
    await storage.deployed();

    const value = await storage.retrieve();
    console.log('Stored value:', value.toString());
  });
});
AStored value: 0
BStored value: undefined
CStored value: null
DTypeError: value.toString is not a function
Attempts:
2 left
💡 Hint

Think about the default value of a uint256 in Solidity.

🧠 Conceptual
intermediate
2:00remaining
Which Hardhat testing setup step is missing here?

You want to write tests for your smart contract using Hardhat. You have installed Hardhat and created a test file. You wrote tests but they fail because ethers is undefined. What step did you miss?

AImporting <code>ethers</code> from Hardhat in the test file
BInstalling <code>ethers</code> package separately
CRunning <code>npx hardhat compile</code> before tests
DAdding <code>mocha</code> as a dev dependency
Attempts:
2 left
💡 Hint

Check how you access Hardhat's built-in libraries in tests.

🔧 Debug
advanced
2:00remaining
Why does this Hardhat test fail with a timeout?

Look at this test code snippet. The test never finishes and times out. What is the cause?

Blockchain / Solidity
describe('Token', function () {
  it('should mint tokens', async function () {
    const Token = await ethers.getContractFactory('Token');
    const token = await Token.deploy();
    // Missing await here
    await token.deployed();

    const tx = await token.mint(accounts[0].address, 100);
    await tx.wait();

    const balance = await token.balanceOf(accounts[0].address);
    console.log('Balance:', balance.toString());
  });
});
Amint function does not exist on the contract
Btx.wait() is called incorrectly without await
Caccounts is not defined, causing a ReferenceError
DMissing await on token.deployed() causes the test to hang
Attempts:
2 left
💡 Hint

Check asynchronous calls that might block the test.

📝 Syntax
advanced
2:00remaining
Which option fixes the syntax error in this Hardhat test snippet?

Identify the option that corrects the syntax error in this test code:

describe('MyContract', function() {
  it('does something', async function() {
    const contract = await ethers.getContractFactory('MyContract')
    const instance = await contract.deploy()
    await instance.deployed()

    const result = await instance.myFunction()
    console.log('Result:', result)
  )
})
AChange <code>async function()</code> to <code>async () =></code>
BAdd a semicolon after <code>await instance.deployed()</code>
CReplace the closing parenthesis <code>)</code> after <code>console.log</code> with a closing curly brace <code>}</code>
DAdd a missing comma after <code>describe</code> function
Attempts:
2 left
💡 Hint

Look carefully at the function closing syntax.

🚀 Application
expert
2:00remaining
How many tests run with this Hardhat test file?

Given this test file, how many individual tests will run when executing npx hardhat test?

Blockchain / Solidity
describe('ContractA', function () {
  it('test 1', async function () {});
  it('test 2', async function () {});

  describe('Nested Suite', function () {
    it('test 3', async function () {});
  });
});

describe('ContractB', function () {
  it('test 4', async function () {});
});
A3
B4
C2
D5
Attempts:
2 left
💡 Hint

Count all it blocks, including nested ones.

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

  1. Step 1: Understand Hardhat testing role

    Hardhat testing setup is designed to help developers write and run tests for smart contracts safely.
  2. Step 2: Identify the environment used

    Tests run on a local blockchain, which means no real money or main network is involved.
  3. Final Answer:

    To write and run tests for smart contracts on a local blockchain -> Option C
  4. 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

  1. Step 1: Recall Mocha test structure

    Hardhat uses Mocha for testing, which organizes tests with describe blocks.
  2. Step 2: Identify correct syntax

    The describe function takes a string and a callback function to group tests.
  3. Final Answer:

    describe('MyContract', function() { ... }) -> Option B
  4. 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?
describe('SimpleTest', function() {
  it('should return true', async function() {
    const result = true;
    console.log(result);
    expect(result).to.equal(true);
  });
});
medium
A. true and test passes
B. false and test fails
C. undefined and test passes
D. Error: expect is not defined

Solution

  1. Step 1: Analyze console output

    The code logs the variable result which is set to true, so console shows 'true'.
  2. Step 2: Check assertion

    The test asserts result equals true, which is correct, so test passes.
  3. Final Answer:

    true and test passes -> Option A
  4. Quick Check:

    console.log(true) + expect(true) passes [OK]
Hint: Check console.log and expect values carefully [OK]
Common Mistakes:
  • Assuming expect is undefined without import
  • Confusing console output with test result
  • Thinking test fails because of async function
4. Identify the error in this Hardhat test code snippet:
describe('Token', function() {
  it('should deploy contract', async () => {
    this.timeout(20000);
    const Token = await ethers.getContractFactory('Token');
    const token = await Token.deploy();
    await token.waitForDeployment();
    expect(token.address).to.be.a('string');
  });
});
medium
A. Missing import of ethers from Hardhat
B. deploy() should not be awaited
C. Incorrect assertion method for address type
D. Using arrow function in it() causes context loss

Solution

  1. 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.
  2. Step 2: Verify other parts

    Imports and await usage are correct; assertion syntax is valid for checking string type.
  3. Final Answer:

    Using arrow function in it() causes context loss -> Option D
  4. 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

  1. Step 1: Understand event testing syntax

    Hardhat with Chai uses await expect(transaction).to.emit(contract, eventName).withArgs(...) to test events.
  2. 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.
  3. 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.
  4. Final Answer:

    await expect(token.transfer(addr1.address, 100)).to.emit(token, 'Transfer').withArgs(owner.address, addr1.address, 100); -> Option A
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard