Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing javascript with Mocha - how can I use console.log to debug a test?

I am using the javascript test-runner "Mocha".

I have a test that is failing, so I would to debug it using console.log.

But when the tests are run, there is no output (only the test results from Mocha). It seems like Mocha has captured and suppressed my console.log output!

How can I get Mocha to show my output? (at for tests that fail)?

EDIT:

Huge apologies! — console.log does work during tests! I must have been expecting it to suppress the output, and I didn't properly check my own code. Thanks for responding. So... that being said... maybe it actually would be nice to suppress the output for tests that pass? hmm...

On a related note: I want to use console.log because I am having a lot of trouble trying to get the Eclipse debugger to connect to node.js.

Am I the only one who finds this tricky? How do you guys debug node.js? With a debugger, or with console.log statements?

like image 646
Nick Perkins Avatar asked Oct 17 '22 09:10

Nick Perkins


People also ask

How do you run a debug in mocha?

run mocha with flag --inspect-brk and click 'open dedicated DevTools for node' in chrome from page chrome://inspect . In dedicated DevTools window add connection localhost:9229 to connect automatically. Also add a debugger statement to the file you want debug.

How do I run a JavaScript console log?

The JavaScript console log function is mainly used for code debugging as it makes the JavaScript print the output to the console. To open the browser console, right-click on the page and select Inspect, and then click Console.

How do you test and debug a JavaScript application?

Console command. Tools→Advanced→Developer Tools command. Select the Developer→JavaScript Console command. Select the Developer→Debug JavaScript command.


1 Answers

What Mocha options are you using?

Maybe it is something to do with reporter (-R) or ui (-ui) being used?

console.log(msg);

works fine during my test runs, though sometimes mixed in a little goofy. Presumably due to the async nature of the test run.

Here are the options (mocha.opts) I'm using:

--require should
-R spec
--ui bdd

Hmm..just tested without any mocha.opts and console.log still works.

like image 56
Zach Bonham Avatar answered Oct 28 '22 21:10

Zach Bonham