Karma: console log not working

Created on 28 Mar 2017  路  6Comments  路  Source: karma-runner/karma

Expected behaviour

I expect to be able to console.log() on any spec.

Actual behaviour

Logs are printed only within the "describe" scope, but not within blocks beforeEach, it, etc.
Instead, a TypeError is returned "undefined is not a constructor (evaluating 'console.log()'...)"

describe('Some spec', function() {
    console.log('it works!') // is logged
    it('Should do something',  function () {
        console.log('it does NOT work') // Type error. No logs. Spec is stopped (error thrown)
    })
})

Environment Details

  • Karma version (output of karma --version): 1.5.0
  • Relevant part of your karma.config.js file
{
        basePath: '',

        frameworks: ['jasmine'],

        exclude: [],

        files: ['test_files.js'],

        preprocessors: {
            './test_files.js': ['webpack']
        },

        reporters: ['spec', 'junit'],

        junitReporter: {
            outputDir: './results'
        },

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

        webpack: require('./webpack_config.js'),

        specReporter: {
            suppressPassed: true,  // do not print information about passed tests
            suppressSkipped: false,  // do not print information about skipped tests
        },

        port: 9876,

        colors: true,

        browsers: ['PhantomJS'],

        plugins: [
            require('karma-jasmine'),
            require('karma-webpack'),
            require('karma-junit-reporter'),
            require('karma-spec-reporter'),
            require('karma-sourcemap-loader'),
            require('karma-chrome-launcher'),
            require('karma-phantomjs-launcher')
        ]
    }
  • related used packages
    "karma-chrome-launcher": "2.0.0",
    "karma-firefox-launcher": "1.0.1",
    "karma-jasmine": "1.1.0",
    "karma-jenkins-reporter": "0.0.2",
    "karma-junit-reporter": "1.2.0",
    "karma-phantomjs-launcher": "1.0.2",
    "karma-script-launcher": "1.0.0",
    "karma-sourcemap-loader": "0.3.7",
    "karma-spec-reporter": "0.0.30",
    "karma-webpack": "2.0.2",

Steps to reproduce the behaviour

  1. Write some console.logs all over your specs (within describe blocks, it blocks, beforeEach, etc)
  2. Run karma server
  3. look into console
feedback-from-author investigation support

Most helpful comment

All 6 comments

@madroneropaulo this

console.log('it doesn't work :( ')

is invalid javascript.

Try this in your test:

console.log('foo');

@maksimr Ok that was a random text comment, I have valid text on my specs. Please reopen. I updated the issue. I don't want to create a new one.

@madroneropaulo can you create a stub project which reproduces this error?
Because I see nothing strange in your config.

Thanks

Looks like you are missing two things in your config: client.captureConsole = true and logLevel: config.LOG_LOG,. As @wbruno says, take a look at #2582 very carefully.

@wbruno and @leegee are correct, your issue should be solved by implementing the config changes described in the last comment on #2582.

Was this page helpful?
0 / 5 - 0 ratings