Debug Hardhat tests and scripts in VSCode

PeterZ
1 min readJan 31, 2024

Hardhat is a fantastic framework for developing and testing smart contracts. Using Hardhat together with VSCode is probably the most common choice for developers nowadays to develop smart contracts.

However, you might face the question of how to set up VSCode to enable debugging (e.g., setting up a breakpoint or stepping in/out a function) the Hardhat tests and scripts written in Typescript. The following is the quick solution:

  1. Install yarn
  2. Inside package.json, add
"scripts": {
"test-hardhat": "yarn hardhat test",
"run-hardhat": "yarn hardhat run"
}

3. Inside .vscode/launch.json, add

{
"name": "Debug hardhat ts script",
"type": "node",
"request": "launch",
"console": "integratedTerminal",
"runtimeExecutable": "yarn",
"runtimeArgs": [
"run-hardhat",
"${file}"
]
},
{
"name": "Debug harthat ts test",
"type": "node",
"request": "launch",
"console": "integratedTerminal",
"runtimeExecutable": "yarn",
"runtimeArgs": [
"test-hardhat",
"${file}"
]
}

Now, you are ready to go. Simply open the Typescript file and press F5 to start debugging.

Hope this article help you smooth your experience of using Hardhat and VSCode. Cheers.

--

--