Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug hardhat solidity tests In WebStorm

After running Hardhat tests in the console with npx hardhat test I decided that being able to set break points would help me iterate faster.

How can I get Webstorm to run the underlying functions started by npx hardhat test so that I can use the built in Debugger?

like image 648
Caleb Gates Avatar asked Apr 30 '26 09:04

Caleb Gates


1 Answers

I've since discovered that hardhat runs mocha under the hood.

To debug in WebStorm you can:

  1. delete your existing configurations
  2. create a new mocha configuration
  3. set any configurations in 'Node options'. Note: since I'm forking the main net it takes a while for tests to start so I added the --timeout 10000 because mocha's default timeout is only 2000ms
  4. select the mocha package, WebStorm doesn't select it by default
  5. set your test file pattern
  6. add const {ethers} = require('hardhat'); to your test file because it is no longer injected by hardhat during run time.
  7. If the green debug icon does not appear I had success in closing and reopening WebStorm.

At this point I could successfully set break points in my test file but not in the MyContract.sol file. This is not surprising given that the contract is compiled before its run.

enter image description here

enter image description here

like image 152
Caleb Gates Avatar answered May 02 '26 00:05

Caleb Gates



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!