Karma: Karma is reporting everything twice

Created on 5 Feb 2016  Â·  25Comments  Â·  Source: karma-runner/karma

If I run my karma tests like this in node then the 'dots' reporter is outputting everything twice

var KarmaServer = require('karma').Server;
var karmaRunner = require('karma').runner;

var karmaConfig = {
    basePath: '',
    autoWatch: false,
    frameworks: ['jasmine'],
    reporters: ['dots'],
    files: [].concat(
        tasks['vendorJs'].getOutput(),
        'node_modules/angular-mocks/angular-mocks.js',
        'node_modules/ng-describe/dist/ng-describe.js',
        tasks['ts'].getOutput(),
        tasks['templates'].getOutput(),
        'Tests/Client/**/*.spec.js'
    ),
    browsers: ['PhantomJS']
};

var karmaServer = new KarmaServer(karmaConfig);
karmaServer.start();

karmaServer.on('browser_register', function (browser) {
    karmaRunner.run(karmaConfig);
})

This will output the following

05 02 2016 13:31:08.353:INFO [karma]: Karma v0.13.19 server started at http://localhost:9876/
05 02 2016 13:31:08.361:INFO [launcher]: Starting browser PhantomJS
05 02 2016 13:31:09.840:INFO [PhantomJS 2.1.1 (Windows 8 0.0.0)]: Connected on socket /#1rTJlVKpIc8bFuWGAAAA with id 17442726
.....
PhantomJS 2.1.1 (Windows 8 0.0.0): Executed 5 of 5 SUCCESS (0.003 secs / 0.066 secs)
.....
PhantomJS 2.1.1 (Windows 8 0.0.0): Executed 5 of 5 SUCCESS (0.003 secs / 0.066 secs)

My tests aren't actually being run twice but if I log something from a test it will show up twice in the output.

Any idea why this is happening?

Let me know if including my package.json or DEBUG output would be useful

investigation

Most helpful comment

I had similar issues when trying to get a prettifier working - I'd get in my terminal both raw console output and the prettified output.

But, I was able to find a karma.conf setting that stopped the raw console output, returning only the prettified output.

karma.conf has a property called client, which is an object with several properties of its own. The one relevant here is captureConsole. This defaults to true, which means any console logging in the browser is sent back to your bash, too. Setting it to false solved my duplicate output issues. I wonder if making that change would help with this?

    client: {
        captureConsole: false
    }

All 25 comments

Could you try checking if there is another phantomjs instance running that could be the reason for this.

There aren't any other node or phantom js process running. I noticed that if I give the karma runner a callback function like this

karmaRunner.run(karmaConfig, function () { });

Then I will only see the output once and when I kill the process node and phantom will keep running. But that may be unrelated

@robianmcd Are any of the output coloured? Or both?

@budde377 both are coloured
karma output

Same here but only with the phantomjs launcher, so this is probably an issue with that.

I still see the issue when I run the tests with chrome.

@robianmcd These issues might be related to the launchers and not to the karma lib. Some of the launchers, for example Firefox work properly by me.

I have same issue with karma-webdriver-launcher.

I had similar issues when trying to get a prettifier working - I'd get in my terminal both raw console output and the prettified output.

But, I was able to find a karma.conf setting that stopped the raw console output, returning only the prettified output.

karma.conf has a property called client, which is an object with several properties of its own. The one relevant here is captureConsole. This defaults to true, which means any console logging in the browser is sent back to your bash, too. Setting it to false solved my duplicate output issues. I wonder if making that change would help with this?

    client: {
        captureConsole: false
    }

I notice same issue in only one my spec file. The others specs are runed once.

I use karma+phantomjs launcher
03 02 2017 16:01:25.694:INFO [watcher]: Changed file "/home/alexey2baranov/htdocs/kp-

client/test/unit/specs/model/Kopnik.spec.js".
PhantomJS 2.1.1 (Linux 0.0.0) INFO LOG: 'You are running Vue in development mode.
Make sure to turn on production mode when deploying for production.
See more tips at https://vuejs.org/guide/deployment.html'

Kopnik
#get()
✓ #dom should be instance of Zemla

Kopnik
#get()
✓ #dom should be instance of Zemla

PhantomJS 2.1.1 (Linux 0.0.0): Executed 1 of 14 SUCCESS (1.052 secs / 0.262 secs)
TOTAL: 1 SUCCESS

PhantomJS 2.1.1 (Linux 0.0.0): Executed 1 of 63 SUCCESS (1.57 secs / 0.592 secs)
TOTAL: 1 SUCCESS

Chrome runner +dots or spec reporter result in same.

I have this problem when I use grunt + karma + grunt-contrib-watch: when I do a single run output is normal. When I do a test run as a result of a file change (in the watch task) the karma output is duplicated.

I am attaching a minimal set of files and config that reproduces the problem.
gruntkarmadoubleoutput.zip

Run

npm install
grunt karma:continuous # to see expected output
grunt dev # and change a file in src to see erroneous double output

I've added two dummy assertions to demonstrate that it is double output and not just the same tests being run twice. The random numbers produced by Math.random are the same in both versions.

@tcschiller Does this eat errors? Looks like the right solution, as long as Karma still reports VM-level exceptions and not just test failures.

@EzraBrooks That's a great question, and I can't off the top of my head recall seeing it one way or another. Worthy of a test.

I've just tested this on a mid-size Angular application testing in Chrome Headless. At least for me, setting captureConsole: false appears to do nothing at all.

I realize this is now a very old question, but @robianmcd how are you running your tests? Is there a script or tool? Also, are files included/loaded through any other (non-karma) configuration?

I came upon this with an Angular v6 CLI app (with Typescript) and getting output duplicated 2 or 3 times in the console. In my karma.conf.js I had two reporters defined which was causing part of the issue:

reporters: ['dots', 'kjhtml'],
Changing that to just 'dots' fixed this issue for me.

On Windows some of the browsers require CLI flags. Without the flags they can do duplications or fail, so it is always good to read the launcher description. Here are the custom launchers I use on win7:

        customLaunchers: {
            "ch": {
                "base": "Chrome",
                "flags": ["--user-data-dir=''", "--disable-web-security"]
            },
            ff: {
                base: "Firefox"
            },
            ie10: {
                base: "IE",
                'x-ua-compatible': 'IE=EmulateIE10',
                flags: ["-extoff"]
            },
            ie11: {
                base: "IE",
                'x-ua-compatible': 'IE=EmulateIE11',
                flags: ["-extoff"]
            },
            op: {
                base: "Opera",
                flags: ["--ran-launcher"]
            },
            ph: {
                base: "PhantomJS"
            }
        },

This doesn't use the PhantomJS duplication though.

I use the singleRun: true config option. It's worth a try too...

I'm not working on the project I was having these issues on anymore but I'll pass these additional ideas along to someone who is.

Thanks!

I checked again, I experience the same problem with Opera. I am pretty sure it worked well with an old version with --ran-launcher, but the current version tries to run the tests twice. Hmm actually not twice, but x times. I think the problem is that it does not close the old tabs, and by running karma again 2 or more instances of the karma client will run.

I move this to the repo of the opera launcher, maybe somebody fixes it there.

I did some investigation, and it looks like runner.js writing directly to process.stdout, on line 57, is the culprit.

Since the example in the original post has a reference to the server, it is technically possible to get around using the runner altogether, but it is likely not documented nor trivial. (Involves using the servers _injector to get its executor and invoking executor.schedule)

Perhaps we should be asking for a new method on server instances to run the configured tests.

Any ideas here? I'm also running karma with grunt-contrib-watch and have the same issue that @ezk84 has.

Basically, my grunt script runs karma:unit (with singleRun = true), and then karma:continuous:start (this should start up my karma server, but not run my tests. singleRun = false, background = true). At this point, everything is working properly. However, when I change one of my source files, and it triggers grunt-contrib-watch to fire off additional tasks (karma:continuous:run), the karma output is duplicated.

If I run karma:continuous:run from another console window, I see the correct output once in the new console, BUT I also see the duplicated output in my original console window that's running grunt watch. I'm thinking that grunt is outputting the results from the karma tests, even though the karma reporter is outputting the results to the console as well. For example, I see the dots from the "dots" reporter only once, but the final SUCCESS message is duplicated.

For now I'm just going to ignore the duplicate output, but just wondering if anyone has any ideas on how to prevent this?

I’ve added a separate issue on grunt-karma to track this (https://github.com/karma-runner/grunt-karma/issues/265), as it’s not quite a karma issue, but how the grunt integration uses karma.

I’ve resorted to commenting out the offending line on runner.js till a better solution is proposed.

Make sure that you don't include the test files:

files: [
      'test-main.js',
      { pattern: 'test/*.js', included: false }
],
Was this page helpful?
0 / 5 - 0 ratings

Related issues

jhildenbiddle picture jhildenbiddle  Â·  4Comments

wellyshen picture wellyshen  Â·  4Comments

schippie picture schippie  Â·  5Comments

kiramclean picture kiramclean  Â·  4Comments

simonh1000 picture simonh1000  Â·  3Comments