6

I know I can put console.logs in Chai tests and get them printed in console. However, I want to know how can I put a regular breakpoint (or debugger; statement) in Vscode, and hit it, and debug as usual with stepping, evaluation, viewing local variables etc.

How do I truly debug hardhat chai test with breakpoints in Vscode (instead of console.logs)?

Can Poyrazoğlu
  • 33,241
  • 48
  • 191
  • 389
  • I have started learning hardhat and now hit a point need to debug the chai tests instead of console.log. Did you @Can Poyrazoğlu get any further on the same? – RJ- Jan 25 '23 at 11:56
  • can't you just set breakpoints in the typescript code and include the `npx hardhat test` command in a [launch configuration](https://code.visualstudio.com/Docs/editor/debugging#_launch-configurations) of vscode? – philipphutterer Jan 25 '23 at 12:30
  • @philipphutterer I did try to have that command, but then the control did not stop the breakpoint. It is a silly mistake but completely valid for a newbie like me. Finally found the answer in [HERE](https://stackoverflow.com/a/71880965/7394630). This will do for the time being. – RJ- Jan 25 '23 at 13:49

2 Answers2

2

For debugger in VSCode I have following in my .vscode/launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Hardhat individual test",
            "type": "node",
            "request": "launch",
            "console": "integratedTerminal",
            "runtimeExecutable": "yarn",
            "runtimeArgs": [
                "testhh",
                "${workspaceFolder}/test-hardhat/<testFile>.ts"
            ]
        }
    ]
}

testhh is a name of a script in my package.json, which look like "testhh": "yarn hardhat test --no-compile". Debugger can then be run via Run and Debug menu.

To add a bit of context to script. I run individual test files with yarn testhh test-hardhat/<testFile>.ts

Urska Krivc
  • 815
  • 1
  • 10
  • 17
  • Yup. I've also tried this with a custom script by having my yarn script with the name and putting runtimeArgs as [`myscriptname`] and it also worked. – Can Poyrazoğlu Mar 24 '23 at 18:50
0

As specified in the comments section above, I was in a similar situation, unable to have a breakpoint on chai tests.

There could be multiple ways to debug and I will keep updating the answer as and when I find a new one.

  1. Deploy the contract in the local HardHat node and test as explained in the Theread Answer.
  2. This is the closest answer I came across is as explained in LinkedIn post comment.
    a) Place a breakpoint in the typescript test code.
    b) Run the following command in Javascript Debug Terminal.
    hardhat test [testfoldername]\[testfile].ts \
RJ-
  • 136
  • 1
  • 13