Search code examples
typescriptvisual-studio-codeethereumethers.jshardhat

How to attach debugger to hardhat chai tests in Vscode and use Typescript breakpoints?


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)?


Solution

  • 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