151

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?

franzlorenzon
5,9236 gold badges38 silver badges58 bronze badges
asked May 19, 2012 at 15:07
5
  • Good deal! :) Right now I'm still very much 'debugging' node via moca tests. I do see a time in the near future when I might want to eval some of the options here: stackoverflow.com/questions/1911015/… Commented May 19, 2012 at 16:45
  • I would suggest that you don't use Eclipse for Node.js, it really isn't the best environment to use IMO. JetBrains' WebStorm is a great Node.js IDE, although it costs money. If you're looking for free, I've really been liking the new Visual Studio Code, which has great built-in support for Node debugging and other things that make Node development nice. Commented Aug 6, 2015 at 16:36
  • @dsw88 - My experience with WebStorm is that it slowed way down once our file structure started getting large and deep. Reminded me of the old days with Java apps. Commented Oct 4, 2015 at 23:53
  • 2
    In addition to what @dsw88 wrote: Use VS Code: Insert a "debugger;" statement somewhere in your code. Start your test with the --inspect-brk option and use the VS Code debugging action "NodeJs attach". The debugger start at the first line in the mocha script and you'll have to press Resume once. The next time your "debugger;" statement is reached, you are good to go. Commented Oct 18, 2018 at 7:53
  • RE Suppress logs for passing tests: stackoverflow.com/questions/53100760/… Commented Oct 4, 2021 at 17:02

5 Answers 5

61

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.

answered May 19, 2012 at 15:24
Sign up to request clarification or add additional context in comments.

3 Comments

where does log() actually lot TO?
HHHmmm with 9 upvotes, on where it actually logs TO?!? might someone help? I'm using webstorm, and I'm not seeing it in the Process Console or the Debugger Console of the debug window.
That depends on the reporter, but usually the output is logged to stdout of the process that calls mocha.
39

If you are testing asynchronous code, you need to make sure to place done() in the callback of that asynchronous code. I had that issue when testing http requests to a REST API.

answered Nov 7, 2013 at 20:13

Comments

27

You may have also put your console.log after an expectation that fails and is uncaught, so your log line never gets executed.

answered Feb 28, 2016 at 20:49

1 Comment

Yep, that was my issue, thanks for suggesting this. I moved the console logs to BEFORE the failing .expect, and they show now.
0

I had an issue with node.exe programs like test output with mocha.

In my case, I solved it by removing some default "node.exe" alias.

I'm using Git Bash for Windows(2.29.2) and some default aliases are set from /etc/profile.d/aliases.sh,

 # show me alias related to 'node'
 $ alias|grep node
 alias node='winpty node.exe'`

To remove the alias, update aliases.sh or simply do

unalias node

I don't know why winpty has this side effect on console.info buffered output but with a direct node.exe use, I've no more stdout issue.

answered Nov 25, 2020 at 12:23

Comments

-2

Use the debug lib.

import debug from 'debug'
const log = debug('server');

Use it:

log('holi')

then run:

DEBUG=server npm test

And that's it!

answered May 17, 2020 at 2:46

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.