Karma: sockets.forEach is not a function

Created on 6 Jan 2016  路  37Comments  路  Source: karma-runner/karma

Running karma 0.13.16 I encountered the following issue after the last test run successfully:

Missing error handler on `socket`.
TypeError: sockets.forEach is not a function
at disconnectBrowsers (/project-path/node_modules/karma/lib/server.js:314:13)
at [object Object].<anonymous> (/project-path/node_modules/karma/lib/server.js:291:7)
at emitTwo (events.js:92:20)
at [object Object].emit (events.js:172:7)
at emitRunCompleteIfAllBrowsersDone (/project-path/node_modules/karma/lib/server.js:256:12)
at [object Object].<anonymous> (/project-path/node_modules/karma/lib/server.js:278:9)
at emitTwo (events.js:92:20)
at [object Object].emit (events.js:172:7)
at [object Object].onComplete (/project-path/node_modules/karma/lib/browser.js:142:13)
at Socket.<anonymous> (/project-path/node_modules/karma/lib/events.js:13:22)
at emitTwo (events.js:92:20)
at Socket.emit (events.js:172:7)
at Socket.onevent (/project-path/node_modules/socket.io/lib/socket.js:335:8)
at Socket.onpacket (/project-path/node_modules/socket.io/lib/socket.js:295:12)
at Client.ondecoded (/project-path/node_modules/socket.io/lib/client.js:193:14)
at Decoder.Emitter.emit (/project-path/node_modules/component-emitter/index.js:134:20)
at Decoder.add (/project-path/node_modules/socket.io-parser/index.js:247:12)
at Client.ondata (/project-path/node_modules/socket.io/lib/client.js:175:18)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at Socket.onPacket (/project-path/node_modules/engine.io/lib/socket.js:101:14)
at emitOne (events.js:77:13)
at WebSocket.emit (events.js:169:7)
at WebSocket.Transport.onPacket (/project-path/node_modules/engine.io/lib/transport.js:93:8)
at WebSocket.Transport.onData (/project-path/node_modules/engine.io/lib/transport.js:104:8)
at WebSocket.onData (/project-path/node_modules/engine.io/lib/transports/websocket.js:76:30)
at emitTwo (events.js:87:13)
at WebSocket.emit (events.js:172:7)
at Receiver.ontext (/project-path/node_modules/ws/lib/WebSocket.js:816:10)
at Receiver.parse (/project-path/node_modules/ws/lib/Receiver.hixie.js:145:8)
at doAdd (/project-path/node_modules/ws/lib/Receiver.hixie.js:104:19)
at Receiver.add (/project-path/node_modules/ws/lib/Receiver.hixie.js:108:22)
at Socket.realHandler (/project-path/node_modules/ws/lib/WebSocket.js:800:20)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at readableAddChunk (_stream_readable.js:146:16)
at Socket.Readable.push (_stream_readable.js:110:10)
at TCP.onread (net.js:523:20)

05 01 2016 23:59:46.725:WARN [PhantomJS 1.9.8 (Linux 0.0.0)]: Disconnected (1 times), because no message in 10000 ms.
JS 1.9.8 (Linux 0.0.0): Executed 42 of 42 DISCONNECTED (10.248 secs / 0.329 secs)
05 01 2016 23:59:46.726:ERROR [karma]: [TypeError: sockets.forEach is not a function]
TypeError: sockets.forEach is not a function
at disconnectBrowsers (/project-path/node_modules/karma/lib/server.js:314:13)
at [object Object].<anonymous> (/project-path/node_modules/karma/lib/server.js:291:7)
at emitTwo (events.js:92:20)
at [object Object].emit (events.js:172:7)
at emitRunCompleteIfAllBrowsersDone (/project-path/node_modules/karma/lib/server.js:256:12)
at [object Object].<anonymous> (/project-path/node_modules/karma/lib/server.js:278:9)
at emitOne (events.js:82:20)
at [object Object].emit (events.js:169:7)
at [object Object]._onTimeout (/project-path/node_modules/karma/lib/browser.js:50:15)
at Timer.listOnTimeout (timers.js:92:15)

/project-path/node_modules/karma/lib/server.js:314
sockets.forEach(function (socket) {
^

TypeError: sockets.forEach is not a function
at disconnectBrowsers (/project-path/node_modules/karma/lib/server.js:314:13)
at process.<anonymous> (/project-path/node_modules/karma/lib/server.js:355:5)
at emitOne (events.js:77:13)
at process.emit (events.js:169:7)
at process._fatalException (node.js:234:26)

I didn't make any changes to the code or to the test itself but reinstalled the node_modules.

bug

Most helpful comment

I succeed!!!!
I input 'npm install socket.[email protected] --save' in command
and it is work~

All 37 comments

Same here, even my previous passing tests are all failing because of this after a node modules reinstall.

Thanks for the report, can you please check on the latest version and create a small repo that reproducea this error or point me to your project if it's open source?

@Dignifiedquire We got the same issue in our project. Just take a look at Travis in the bottom:
https://travis-ci.org/trueadm/inferno

Seems to run into a infinity loop just now.

Going to downgrade for now until this is sorted out.

Code that fails is this:

    sockets.forEach(function (socket) {
      socket.removeAllListeners('disconnect')
      if (!socket.disconnected) {
        // Disconnect asynchronously. Socket.io mutates the `sockets.sockets` array
        // underneath us so this would skip every other browser/socket.
        process.nextTick(socket.disconnect.bind(socket))
      }
    })

I tried downgrading and even then I have the same error, was using chrome launcher with karma-mocha if it helps..

This is due to socket.io bumping to 1.4.0. 1.3.7 worked just fine. We are having this problem too and it is causing our CI to fail.

Does 0.13.15 work?

All newer 0.13 releases seem to be broken, as there seems to be some breaking change in socket.[email protected]

The issues is in package.json for karma: "socket.io": "^1.3.7", and 1.4.0 matches this (at least http://jubianchi.github.io/semver-check/ says so...)

Yes, but breaking backwards compatible changes should only be happening in major bumps, not minor version increases. So the semver range specified is fine in that regard.

@pushups probably

As a workaround try setting this
"socket.io": "1.3.7"

in your package.json.

Meaning not in karmas package.json in your own which is the one requiring karma.

0.13.9 should have socket.io dependency:
"socket.io": "~1.3.5", which doesn't match 1.4.0 in semver, Trying [email protected] now.

Just tried [email protected] and it worked.

Confirming [email protected] works! Anything above it will match socket.io 1.4.0.

Patch incoming

Just published 0.13.19 which fixes this.

Do you mean 19?

Do you mean 19?

yes

OK :D thanks for the fast work! :clap:

Yes! Thanks for the quick turn around!

+1 thanks alot!

Thanks for the quick turnaround! :+1:

I've just noticed a side effect from upgrading to 0.13.19; gulp karma test tasks take much longer to exit after the test run.

gulp.task('test:ng', function(done) {
  new Server({
    configFile: __dirname + '/karma.conf.js',
    singleRun: true
  }, done).start();
});

Previously the above task used to complete immediately after tests have completed. on 0.13.9, there is a pause of ~30 seconds after the karma runner completes the tests and the task exits.

gulp.task('test:ng', function(done) {
  new Server({
    configFile: __dirname + '/karma.conf.js',
    singleRun: true
  }, function() {
    console.log('done');
    done();
  }).start();
});

The above outputs done to the console but the gulp task still doesn't complete until after ~30 seconds.

Is anyone else seeing this or is this maybe something particular to my Gulpfile?

+1 I'm seeing the same lag with 0.13.19.

Please open a new issue for this

Confirm, 0.13.19 (from previous 0.13.15) fixed the originally mentioned bug.
This bug was for a small time, a true showstopper for me. Thanks for quick fix!

So, I had a "Missing error handler on socket. TypeError: sockets.forEach is not a function"
while building a project, which had essentially devDependencies as:

"devDependencies": {
"angular-gettext": "2.1.2",
"glob": "6.0.1",
(... omit large number of 'grunt' deps...)
"jasmine-reporters": "2.0.7",
"jasmine-spec-reporter": "2.4.0",
"json-proxy": "0.9.1",
"karma": "0.13.19",
"karma-chrome-launcher": "0.2.1",
"karma-coverage": "0.5.3",
"karma-firefox-launcher": "0.1.7",
"karma-ie-launcher": "0.2.0",
"karma-jasmine": "0.3.6",
"karma-junit-reporter": "0.3.3",
"karma-phantomjs-launcher": "0.2.1",
"karma-requirejs": "0.2.2",
"karma-spec-reporter": "0.0.22",
"load-grunt-tasks": "3.3.0",
"npm-check-updates": "2.5.1",
"phantomjs": "1.9.18",
"protractor": "2.5.1",
"requirejs": "2.1.22",
"updatehammer": "0.3.4",
"webdriver": "0.0.1"
}

I can confirm that it works with 0.13.19. Thank you guys for the super quick fix!

changed to "socket.io": "0.13.19" still can't work!

@aviman1109
Change karma, not socket. Ie. have:
"karma": "0.13.19"

Because that change will take to effect the changes made by people in here, which in turn will fix that socket-related issue.

If that change doesn't do good, then it is something else.

I change the version directly on package.json is that right??
Or should I input any thing in terminal??

I succeed!!!!
I input 'npm install socket.[email protected] --save' in command
and it is work~

I seem to still have this problem with the latest version of Karma:

10 03 2016 17:07:39.954:INFO [karma]: Karma v0.13.22 server started at http://localhost:9876/
10 03 2016 17:07:39.959:INFO [launcher]: Starting browser PhantomJS
10 03 2016 17:07:40.751:INFO [PhantomJS 1.9.8 (Mac OS X 0.0.0)]: Connected on socket /#whtCZIqgWvlZk5nVAAAA with id 56004832
[snip]
PhantomJS 1.9.8 (Mac OS X 0.0.0): Executed 61 of 862 (4 FAILED) ERROR (0.458 secs / 0.519 secs)
Missing error handler on `socket`.
TypeError: Cannot read property 'charAt' of undefined
    at posix (/project/node_modules/karma-junit-reporter/node_modules/path-is-absolute/index.js:4:13)
    at writeXmlForBrowser (/project/node_modules/karma-junit-reporter/index.js:61:9)
    at onBrowserComplete (/project/node_modules/karma-junit-reporter/index.js:126:5)
    at null.<anonymous> (/project/node_modules/karma/lib/events.js:13:22)
    at emitTwo (events.js:92:20)
    at emit (events.js:172:7)
    at onComplete (/project/node_modules/karma/lib/browser.js:142:13)
    at Socket.<anonymous> (/project/node_modules/karma/lib/events.js:13:22)
    at emitTwo (events.js:92:20)
    at Socket.emit (events.js:172:7)
    at Socket.onevent (/project/node_modules/karma/node_modules/socket.io/lib/socket.js:335:8)
    at Socket.onpacket (/project/node_modules/karma/node_modules/socket.io/lib/socket.js:295:12)
    at Client.ondecoded (/project/node_modules/karma/node_modules/socket.io/lib/client.js:193:14)
    at Decoder.Emitter.emit (/project/node_modules/karma/node_modules/socket.io/node_modules/socket.io-parser/node_modules/component-emitter/index.js:134:20)
    at Decoder.add (/project/node_modules/karma/node_modules/socket.io/node_modules/socket.io-parser/index.js:247:12)
    at Client.ondata (/project/node_modules/karma/node_modules/socket.io/lib/client.js:175:18)
10 03 2016 17:07:41.710:WARN [PhantomJS 1.9.8 (Mac OS X 0.0.0)]: Disconnected (1 times)
PhantomJS 1.9.8 (Mac OS X 0.0.0): Executed 61 of 862 (4 FAILED) DISCONNECTED (10.462 secs / 0.519 secs)
10 03 2016 17:07:51.712:ERROR [karma]: [TypeError: Cannot read property 'charAt' of undefined]
TypeError: Cannot read property 'charAt' of undefined
    at posix (/project/node_modules/karma-junit-reporter/node_modules/path-is-absolute/index.js:4:13)
    at writeXmlForBrowser (/project/node_modules/karma-junit-reporter/index.js:61:9)
    at onBrowserComplete (/project/node_modules/karma-junit-reporter/index.js:126:5)
    at null.<anonymous> (/project/node_modules/karma/lib/events.js:13:22)
    at emitOne (events.js:82:20)
    at emit (events.js:169:7)
    at null._onTimeout (/project/node_modules/karma/lib/browser.js:50:15)
    at Timer.listOnTimeout (timers.js:92:15)

Any idea what it could be? Tried downgrading to .19, but I had the same problem.

Yep I'm seeing it once again too.

Yeah, I'm seeing a very similar issue to this. Here are the details: http://stackoverflow.com/questions/35952704/karma-move-folder-missing-error-handler-on-socket

These are different issue, the error message is just similar. Please file separate issues

I input 'npm install socket.[email protected] --save' in command
it worked for me

Was this page helpful?
0 / 5 - 0 ratings