18

I'm using mocha along with yeiwd and chai to test a javascript web application. I'd like to see console logs from within the application to help debug some tests.

When I run the tests from the comand line with npm test I don't see any console.log() output, only mocha output.

Is there a way to view logs created from within the application code?

Update - here is my package.json

{
  "devDependencies": {
    "chai": "^2.3.0",
    "chai-colors": "^1.0.0",
    "yiewd": "^0.6.0"
  },
  "scripts": {
    "test": "mocha --harmony --timeout 0 tests/*/*.js"
  }
}
barry
  • 4,037
  • 6
  • 41
  • 68
  • Hm I don't use npm test often but just tried it and had no problem having my console.log print during the test. What happens when you run a mocha test directly? By running mocha testName.js from the command? Do you see anything there? – KJ3 Sep 28 '15 at 15:32
  • 3
    I can use console.log *within* my tests but not from within the application code – barry Sep 28 '15 at 15:36
  • Yes console.log works within the application code for me, and it is indeed supposed to. Are you using any special command line arguments? See this thread http://stackoverflow.com/questions/10666349/testing-javascript-with-mocha-how-can-i-use-console-log-to-debug-a-test – KJ3 Sep 28 '15 at 15:41
  • I'll update the question with my package.json – barry Sep 28 '15 at 15:42
  • 1
    I can confirm that running your exact parameters in my test still shows the console.logs from within my application code on the terminal right there alongside my mocha logs. The only difference is I'm not running yiewd but I don't see how that would affect anything. Either way console.log is supposed to work in a mocha test by default, so there must be something stopping it in your environment somewhere. – KJ3 Sep 28 '15 at 15:49
  • Ok, thanks for confirming that – barry Sep 28 '15 at 15:51
  • If there's actually a problem that needs solving, we're going to need a [mcve]. Otherwise, the only thing we can say is exactly what KJ3 said: "works for me." – Louis Aug 16 '16 at 16:03
  • 11
    Wish this had been answered – developius Mar 19 '17 at 17:46
  • Do you see the same behaviour as me @developius ? – barry Mar 19 '17 at 20:20
  • It's weird. I see it in some places, but not others. It's usually when I'm trying to log an error object from a callback (e.g. a mongoose query etc) – developius Mar 19 '17 at 20:24
  • Anybody tried passing some `--verbose` or `--debug` flags to NPM, with `npm test -- --verbose`? That helped me on the [same issue when running Jest via NPM](https://stackoverflow.com/a/54190964/3088045). – Kamafeather Jan 15 '19 at 01:46
  • [`--debug` seems supported](https://mochajs.org/#usage); it should be `npm test -- --verbose`. Give a try. – Kamafeather Jan 15 '19 at 01:57

0 Answers0