Chris Frantz
Chris Frantz

Reputation: 43

Debugging unit tests in Node Tools Visual Studio (NTVS)

Is it possible to debug mocha unit tests using Visual Studio Node Tools? I can run all my mocha tests using the command line command mocha . -- but debugging these unit tests is not possible at the moment. Is there any way to debug unit tests entirely within visual studio, or even at all?

Upvotes: 3

Views: 2160

Answers (2)

Rich N
Rich N

Reputation: 9475

It is possible to debug Mocha unit tests in NTVS in proper Visual Studio 2017. This worked fine before Nodejs v8.0.0. Since then it has had a tendency to ignore your break points.

However, in Node v10.13.0, which is the LTS version at the time of writing, the steps below do seem to work. When you install node you need to select to install the 'Tools for Native Modules' at the end of the install process, or your breakpoints may well be missed. Be warned that if you do this the node installer is likely to reboot your machine without asking you at the end of the install.

  • Create a JavaScript blank Node.js Console Application.
  • Drop to a command prompt in the project folder. To do this right-click the project in Solution Explorer/Open Command Prompt Here… Then enter: npm install mocha --save-dev.
  • In Visual Studio, add a JavaScript Mocha Unit Test file (in Solution Explorer right-click the project/Add/New Item..). Debugging tests won’t work with a regular JavaScript file. Build your project.
  • Your unit test file has tests in it provided for you. Put breakpoints in them.
  • Bring up the Test Explorer window (Test/Windows/Test Explorer). It should show the tests. Right-click at the top level and 'Debug Selected Tests'. It should run the tests and break on your breakpoints.

This was tested in Visual Studio 2017 15.9.1, both Enterprise and Community editions.

Upvotes: 1

KJ3
KJ3

Reputation: 5298

See this SO post for a way to debug Mocha tests using Visual Studio.

or even at all?

For a more streamlined Mocha testing debug interface, I highly recommend WebStorm as the functionality is built into the IDE. Makes debugging a breeze and has a much better UI for Mocha testing.

Upvotes: 0

Related Questions