Consider this simple Hardhat test for a smart contract that stores a number. What will the test output when run?
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()); }); });
Think about the default value of a uint256 in Solidity.
The default value of an uninitialized uint256 in Solidity is 0. The retrieve() function returns this value, which is a BigNumber object. Calling toString() converts it to '0'.
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?
Check how you access Hardhat's built-in libraries in tests.
Hardhat injects ethers into the test environment, but you must import it explicitly in your test file using const { ethers } = require('hardhat'); to use it.
Look at this test code snippet. The test never finishes and times out. What is the cause?
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()); }); });
Check asynchronous calls that might block the test.
The token.deployed() call returns a promise that must be awaited. Without await, the test continues before deployment finishes, causing unpredictable behavior and timeout.
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)
)
})Look carefully at the function closing syntax.
The it callback function is closed with a parenthesis ) instead of a curly brace }. This causes a syntax error. Replacing it with } fixes the block closure.
Given this test file, how many individual tests will run when executing npx hardhat test?
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 () {}); });
Count all it blocks, including nested ones.
There are 4 it blocks total: two in ContractA top level, one nested inside Nested Suite, and one in ContractB.