Search code examples
debuggingwebstormsolidityhardhat

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?


Solution

  • 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