Challenge - 5 Problems
Ethers.js Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple ethers.js contract call
What is the output of this ethers.js code snippet when calling a contract's
getValue function that returns 42?Blockchain / Solidity
const { ethers } = require('ethers');
async function test() {
const provider = new ethers.providers.JsonRpcProvider();
const contractAddress = '0x1234567890123456789012345678901234567890';
const abi = [
'function getValue() view returns (uint256)'
];
const contract = new ethers.Contract(contractAddress, abi, provider);
const value = await contract.getValue();
console.log(value.toString());
}
test();Attempts:
2 left
💡 Hint
Remember that ethers.js returns BigNumber objects for uint256 values, so you need to convert them to string or number.
✗ Incorrect
The contract's
getValue returns 42 as a BigNumber. Calling toString() converts it to the string '42'.❓ Predict Output
intermediate2:00remaining
Result of sending a transaction with ethers.js signer
What will be the value of
txResponse.hash after sending this transaction?Blockchain / Solidity
const { ethers } = require('ethers');
async function sendTx() {
const provider = new ethers.providers.JsonRpcProvider();
const signer = provider.getSigner();
const txResponse = await signer.sendTransaction({
to: '0xabcdefabcdefabcdefabcdefabcdefabcdefabcd',
value: ethers.utils.parseEther('0.01')
});
console.log(txResponse.hash);
}
sendTx();Attempts:
2 left
💡 Hint
Transaction hashes are always 32 bytes represented as 66 hex characters including '0x'.
✗ Incorrect
The
txResponse.hash is the transaction hash, a 32-byte hex string starting with '0x' and 64 hex digits after.🔧 Debug
advanced2:00remaining
Identify the error in this ethers.js test setup
What error will this ethers.js test code produce when run?
Blockchain / Solidity
const { ethers } = require('ethers');
describe('MyContract', () => {
let contract;
beforeEach(async () => {
const factory = await ethers.getContractFactory('MyContract');
contract = await factory.deploy();
await contract.deployed();
});
it('should return correct value', async () => {
const value = await contract.getValue();
expect(value).to.equal(42);
});
});Attempts:
2 left
💡 Hint
Check if the testing assertion library is imported or available.
✗ Incorrect
The code uses
expect without importing it from a test library like Chai, causing a ReferenceError.📝 Syntax
advanced2:00remaining
Find the syntax error in this ethers.js async function
Which option correctly fixes the syntax error in this code?
Blockchain / Solidity
async function test() { const provider = new ethers.providers.JsonRpcProvider(); const balance = provider.getBalance('0xabc123...'); console.log(balance.toString()); } test();
Attempts:
2 left
💡 Hint
Remember that getBalance returns a Promise and must be awaited inside async functions.
✗ Incorrect
Without await, balance is a Promise, so calling toString() on it causes an error. Adding await fixes this.
🚀 Application
expert3:00remaining
How to test event emission with ethers.js and Mocha
You want to test that your contract emits an event
ValueChanged with the new value 100 after calling setValue(100). Which code snippet correctly tests this using ethers.js and Mocha?Attempts:
2 left
💡 Hint
Use the Chai matchers from ethereum-waffle or hardhat for event testing.
✗ Incorrect
Option C uses the correct syntax with
expect and to.emit matcher to test event emission with arguments.