Skip to content
This repository has been archived by the owner on Dec 28, 2023. It is now read-only.

Messages written to console.log don't show up in the terminal #47

Closed
demisx opened this issue Sep 6, 2014 · 33 comments
Closed

Messages written to console.log don't show up in the terminal #47

demisx opened this issue Sep 6, 2014 · 33 comments

Comments

@demisx
Copy link

demisx commented Sep 6, 2014

Hi. I am using karma server to run my mocha unit tests for an angular app. The problem I am having is that messages that I write inside each it to console.log don't show up in the terminal. They do show up, however, if I simply run mocha [unit_test_file_name].js at command line. Am I missing something in the configuration? Here are my files:

// gulfile.js
karma = require('karma').server,
gulp.task('unit', function (done) {
  karma.start({
    configFile: __dirname + '/config/unit/karma-unit.conf.js',
    singleRun: true
  }, done);
});
// karma-unit-conf
module.exports = function(config) {
  config.set({

    basePath: '../../',
    autoWatch: false,
    frameworks: ['mocha', 'chai', 'sinon-chai', 'chai-as-promised'],
    // frameworks: ['mocha'],
    browsers: ['PhantomJS'],

    files: [
      'bower_components/angular/angular.js',
      'bower_components/lodash/dist/lodash.js',
      'app/**/*.coffee'
    ],

    plugins: [
      'karma-chrome-launcher',
      'karma-firefox-launcher',
      'karma-phantomjs-launcher',
      'karma-script-launcher',
      'karma-mocha',
      'karma-chai-plugins',
      'karma-coffee-preprocessor'
    ],

    preprocessors: {
      '**/*.coffee': ['coffee']
    },

    coffeePreprocessor: {
      options: {
        bare: true,
        sourceMap: false
      },
      transformPath: function(path) {
        return path.replace(/\.coffee$/, '.js');
      }
    },

    logLevel: config.LOG_DEBUG,

    client: {
      mocha: {
        bail: true
      }
    }
  });
};

Thank you!

@demisx
Copy link
Author

demisx commented Sep 6, 2014

I've noticed that I gain the ability to see written to console messages in my terminal back when I comment out the client property from the config object, i.e.:

// karma-unit-conf
module.exports = function(config) {
  config.set({
   ... ... ...
    logLevel: config.LOG_DEBUG,

    //  client: {
    //  mocha: {
    //     bail: true
    //   }
    // }
  });
};

Does this make sense?

@pkozlowski-opensource
Copy link
Member

@demisx I would be keen to help out with this issue but there are just way to many dependencies in your project to do it in an efficient manner. Could you please create a minimal repository that exposes the issue? Like remove all the plugins / config options that are not essential to reproducing the issue?

We just basically need to have a minimal reproduce scenario that demonstrates an issue in one of the karma plugins. This will make the issue resolution faster. Thnx!

@demisx
Copy link
Author

demisx commented Sep 7, 2014

@pkozlowski-opensource I understand. Here is a stripped down repo to replicate this issue:
https://github.com/demisx/karma-mocha-console.

Instructions are in the readme file. Please let me know if there is anything else I can help with troubleshooting this issue.

@maksimr
Copy link
Contributor

maksimr commented Sep 7, 2014

@demisx hi!

Put captureConsole to property client in karma.conf.js:

    client: {
      captureConsole: true,
      mocha: {
        bail: true
      }
    }

This problem relate to Karma and we have issue.
Thanks

@maksimr maksimr closed this as completed Sep 7, 2014
@demisx
Copy link
Author

demisx commented Sep 8, 2014

Hi there. Indeed, this fixed the problem. Would be great if docs mentioned this option. I'll see if I can contribute to it.

@alexmgrant
Copy link

🍺

@winnemucca
Copy link

is the mocha object necessary to run this? I cannot get this option to run with jasmine.

@maksimr
Copy link
Contributor

maksimr commented Mar 8, 2016

@winnemucca no, It should work without mocha object

@blackmjck
Copy link

Likewise, adding the client/captureConsole object doesn't fix the issue for me (also using Jasmine)

@AshCoolman
Copy link

Console.logs in my mocha tests are not showing up in terminal with

  logLevel: config.LOG_DEBUG,
  client: {
    captureConsole: true,
    mocha: {
      bail: true
    }
  },

@dignifiedquire
Copy link
Member

please make sure to use [email protected] there was a bug in 1.0

@IdahoEv
Copy link

IdahoEv commented Jul 2, 2016

UPDATE: karma-mocha-reporter should be emitting logs, but isn't due to a bug in karma. The fix for that bug has been merged, so it should start working again with the next release of karma. Consider my suggestion below an ugly workaround in the meantime. See references:
litixsoft/karma-mocha-reporter#67
karma-runner/karma#2220

In case anyone else is coming across this issue: one possible cause is that you have turned off karma's default reporter. karma's default reporter (called 'progress') captures and re-emits log lines. If you have specified any other reporters, the progress reporter gets turned off unless you re-specify it.

As a result, if your karma.conf.js has an entry like this (ours did), you won't see your console.log messages, because neither karma-mocha-reporter nor karma-coverage-reporter emit log lines:

reporters: ['mocha', 'coverage']

If you just include progress in the list, you should get your console.log messages back, though having both progress and mocha reporters is a bit of a visual mess.

reporters: ['progress', 'mocha', 'coverage']

@bkakadiya42
Copy link

bkakadiya42 commented Jul 12, 2016

karma version:1.1.1

have following in the config:

reporters: ['progress', 'mocha'],
logLevel: config.LOG_DEBUG,
client: {
  captureConsole: true,
  mocha: {
    bail: true
  }
},

karma.config.js
package.json

We are still not being able to see console.logs in the terminal output.

@anvish
Copy link

anvish commented Sep 28, 2016

fix for PhantomJS:

        client: {
            config: {
                browserConsoleLogOptions: true
            }
        }

@danday74
Copy link

My logs work with ...
client: {
captureConsole: true
}

but I seem to be getting loads of logging statements from third party libs .... annoying!

@niksumeiko
Copy link

Had the same problem after switching to use only "verbose" reporter‌! After adding "progress" to the reporters array in karma.conf.js, Terminal started to display logs I was adding via console.log().

...
reporters: ['progress', 'verbose'],
...

With only "verbose" reporter, logs weren't displayed.

@madroneropaulo
Copy link

madroneropaulo commented Feb 1, 2017

Could not make it work either, as a REALLY hacky workaround for Chrome, you can monkeypatch the window console.log function from a second document. Useful ONLY when you are desperate, have complex tests and want to debug your tests with Chrome.

function consoleLog(str) {
    var i = document.createElement('iframe');
    i.style.display = 'none';
    document.body.appendChild(i);
    window.console = i.contentWindow.console;
   //(<any> window).console = i.contentWindow.console; // for Typescript tests.
    console.log(str);
}

@stevemao
Copy link

stevemao commented Mar 6, 2017

Looks like this issue reappears in v1.5.0.
Downgrading to v1.4.1 fixed it.

@davidturissini
Copy link

If its a help to anyone experiencing this, it appears that console.warn and console.error still work as expected

@maksimr
Copy link
Contributor

maksimr commented Mar 16, 2017

@stevemao @davidturissini Look at this issue maybe it's your case.

Thanks

@stevemao
Copy link

stevemao commented Mar 16, 2017

I like the ideas in that issue :) But I think think should be a breaking change if console.log by default doesn't show any messages.

@sdtsui
Copy link

sdtsui commented Mar 17, 2017

As of v1.5.0, modifications on the client key do not solve this issue. For example: 👎

client: {
  captureConsole: true,
  mocha: {
    bail: true
  }
},

Modifying browserConsoleLogOptions: 👍 worked.

    browserConsoleLogOptions: {
      level: 'log',
      format: '%b %T: %m',
      terminal: true
    }

See issues: #2582 & #2228 for more info.

@pearldiver
Copy link

@sdtsui it works but it log all the messages twice, i still did not figure out how to make it once only.

@spencer-brown
Copy link

@sdtsui 's solution does not double-log for me. Thanks for your solution, @sdtsui !

niksy added a commit to niksy/generator-paleta that referenced this issue Apr 2, 2017
@tiefling
Copy link

tiefling commented Apr 7, 2017

@sdtsui 's solution worked for me (once I put it in the right place)

@kylebake
Copy link

kylebake commented Apr 21, 2017

The solution provided by @sdtsui worked beautifully for me as well, thank you. Just throw the browserConsoleLogOptions into the karma.conf.js if you're having trouble on where to place it.

bhh1988 added a commit to box/box-content-preview that referenced this issue May 7, 2017
@smelted-code
Copy link

@sdtsui You're a hero among men. Thank you, sir.

@riegersn
Copy link

Im still unable to see console.log unless I have logLevel: config.LOG_DEBUG Is there no way around this?

@phil123456
Copy link

none of these work with karma 4.0 :-(

@collinkrawll
Copy link

Just going to add that some of the solutions posted here were actually breaking my karma config under the current version (4.0). Removing them as described here fixed it.

@phil123456
Copy link

phil123456 commented Feb 7, 2019

I dont even have these...also my browser closes even using autowatch (brand new karma version)

module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', '@angular/cli'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage-istanbul-reporter'),
      require('@angular/cli/plugins/karma')
    ],
    client:{
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    coverageIstanbulReporter: {
      reports: [ 'html', 'lcovonly' ],
      fixWebpackSourcePaths: true
    },
    angularCli: {
      environment: 'dev'
    },
    reporters: ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_DEBUG,
    autoWatch: true,
    browsers: ['Chrome'], 
    singleRun: true, 
    customLaunchers: 
    { 
    	ChromeHeadless: 
    	{ 
    		base: 'Chrome', 
    		flags: [ 
//    			'--headless', 
    			'--disable-gpu', 
    			'--disable-translate', 
    			'--disable-extensions', 
    			'--remote-debugging-port=9222', 
    			'--single-run=false',
    			'--auto-watch'
    			], 
    	} 
    }
  });
};

@JonLevy
Copy link

JonLevy commented May 16, 2019

I'm having this problem with Karma 4.0.1 and developing in NativeScript.

@juniorschen
Copy link

I'm having this problem with Karma 4.0.1 and developing in NativeScript.

I'm too

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests