0
0
Blockchain / Solidityprogramming~20 mins

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

Choose your learning style9 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.