Truffle Debugging – Debugging JS Unit Tests with Truffle Framework in VS Code

dapp-debuggingtruffletruffle-contract

Is there a way to debug JS unit tests for smart contracts? I use VS Code for development, and when I try to run a Mocha debugger it complains that artifacts is not defined. I briefly looked at truffle code, it looks like it adds artifacts, contract and some other global js variables when it runs the unit tests. Is there a way for me to add those explicitly in the JS unit test file so I could debug that unit test file?

Best Answer

Your lucky day (had to solve this few days ago):

See that you have truffle-core locally in your project. If not, do:

npm install truffle-core

Then use a configuration similar to this: ( Debug -> Open Configurations )

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "truffle test (debugable)",
            "cwd": "${workspaceFolder}",
            "program": "${workspaceFolder}\\node_modules/truffle-core/cli.js",
            "args": [
                "test"
            ]

        }
    ]
}

Or add a new one (simple "node" launch, then edit it)

If your code does not spawn another node process (brings trouble which are essentially an node.js bug), you should be good.

Related Topic