Truffle: Debugging unit tests

Created on 27 Feb 2018  路  5Comments  路  Source: trufflesuite/truffle

  • [x ] I've asked for help in the Truffle Gitter before filing this issue.

Issue

Debugging JS unit tests

Steps to Reproduce

Do you know if there is 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?

Expected Behavior

Be able to debug js unit tests

Actual Results

Not able to debug unit tests as artifacts, contracts and other JS objects injected by truffle are not present when running the debug session

Environment

  • Operating System: Mac OSX
  • Ethereum client: Ganache
  • Truffle version (truffle version): 4.0.7
  • node version (node --version): 7.7.3
  • npm version (npm --version): 4.0.3
  • IDE: VS Code

Most helpful comment

Just for reference - the same solution above can be used to start the debugger from command line and debug in node inspector (in Chrome dev tools):

npm install truffle-core
node --inspect-brk ./node_modules/truffle-core/cli.js test test/test_to_debug.js
  • Open chrome://inspect page and click the "Open dedicated DevTools for Node" link.
  • We should see the cli.js file with execution paused on the first line.
  • Add the test sources into the inspector: click the "Filesystem" on the left and then "Add folder to workspace".
  • Browse to the folder with tests and add it.
  • Open the file with the test to debug and set the breakpoint, continue the execution until it reaches the breakpoint
  • Now you can step through your test, inspect variables, etc

All 5 comments

@lazaridiscom You are my hero - this is gonna save me hours of pulling my hair! I am going to post this question on ethereum.stackexchange.com and if you are on there you can post this answer and get points, or if you don't use it I will post the answer, so the community can use it. I know several people who were looking for this as well.

@lazaridiscom sure, go ahead and answer this: https://ethereum.stackexchange.com/questions/41094/debugging-js-unit-tests-with-truffle-framework-in-vs-code
I'll accept your answer

Issue resolved, thanks to @lazaridiscom

Just for reference - the same solution above can be used to start the debugger from command line and debug in node inspector (in Chrome dev tools):

npm install truffle-core
node --inspect-brk ./node_modules/truffle-core/cli.js test test/test_to_debug.js
  • Open chrome://inspect page and click the "Open dedicated DevTools for Node" link.
  • We should see the cli.js file with execution paused on the first line.
  • Add the test sources into the inspector: click the "Filesystem" on the left and then "Add folder to workspace".
  • Browse to the folder with tests and add it.
  • Open the file with the test to debug and set the breakpoint, continue the execution until it reaches the breakpoint
  • Now you can step through your test, inspect variables, etc

I鈥檓 not sure if Truffle鈥檚 folder / project structure changed, but in any case the CLI runner is present at this path now: node_modules/truffle/build/cli.bundled.js. No need to install truffle-core.

Was this page helpful?
0 / 5 - 0 ratings