karma: Missing error handler on `socket`. TypeError: (msg || "").replace is not a function Error

I’m getting the above error when I try to run jenkins jobs using karma. Any ideas what could be causing this?

Expected behavior

karma runs angular2 tests on jenkins, they run fine on desktop.

Actual behavior

`Missing error handler on `socket`.
TypeError: (msg || "").replace is not a function
    at /home/hudson/.hudson/jobs/workspace/example/node_modules/karma/lib/reporter.js:45:23
    at onBrowserError (/home/hudson/.hudson/jobs/workspace/example/node_modules/karma/lib/reporters/base.js:58:60)
    at null.<anonymous> (/home/hudson/.hudson/jobs/workspace/example/node_modules/karma/lib/events.js:13:22)
    at emitTwo (events.js:100:13)
    at emit (events.js:185:7)
    at onKarmaError (/home/hudson/.hudson/jobs/workspace/example/node_modules/karma/lib/browser.js:95:13)
    at Socket.<anonymous> (/home/hudson/.hudson/jobs/workspace/example/node_modules/karma/lib/events.js:13:22)
    at emitOne (events.js:95:20)
    at Socket.emit (events.js:182:7)
    at Socket.onevent (/home/hudson/.hudson/jobs/workspace/example/node_modules/socket.io/lib/socket.js:335:8)
    at Socket.onpacket (/home/hudson/.hudson/jobs/workspace/example/node_modules/socket.io/lib/socket.js:295:12)
    at Client.ondecoded (/home/hudson/.hudson/jobs/workspace/example/node_modules/socket.io/lib/client.js:193:14)
    at Decoder.Emitter.emit (/home/hudson/.hudson/jobs/workspace/example/node_modules/component-emitter/index.js:134:20)
    at Decoder.add (/home/hudson/.hudson/jobs/workspace/example/node_modules/socket.io-parser/index.js:247:12)
    at Client.ondata (/home/hudson/.hudson/jobs/workspace/example/node_modules/socket.io/lib/client.js:175:18)
    at emitOne (events.js:90:13)`

Enviroment Details

  • Karma version (output of karma --version):
  • Relevant part of your karma.config.js file
  • node --version v5.7.0
  • npm --version 3.6.0 karma version 0.13.21

Steps to reproduce the behaviour

  1. run tests in jenkins, (independent of the tests running) the above stacktrace is displayed

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 6
  • Comments: 20 (2 by maintainers)

Commits related to this issue

Most upvoted comments

i fixed this locally by adding the following lines to karma/lib/reporter.js @ line 45:

      if ( typeof msg !== "string") {
          msg = JSON.stringify(msg);
      }

so at least there’s output

@dignifiedquire there’s still a problem to be fixed and that is the error message. It is very unhelpful and it would be very much appreciated by the community, I’m guessing, if the quality of that was improved. As shown here, the problem was a missing file. A missing file error would be much more appropriate and helpful to users.

Update: My problem was IntelliJ messing up imports in a spec. It couldn’t handle angular2 dependencies properly and gave the wrong path. Build worked fine, but testing failed.

Just giving an update on this issue, I eventually got console access to the build server and found the root cause of this issue.

In my files section I had the following entry

{ pattern: 'node_modules/rxjs/bundles/rx.js', included: true, watched: true }

The file name is actually Rx.js and not rx.js, this was not a problem with case insensitive windows. Fixing the filename case and everything is now consistent.

{ pattern: 'node_modules/rxjs/bundles/Rx.js', included: true, watched: true }

I appreciate any time anyone spent on this issue till now. Turns out the exception I focused in on was a symptom of the file not being found.

Thanks Paul

I think this is really a bug somewhere, though I’m not sure where. The actual excepion occurs because the karma reporter assumes that only strings come as error messages from the browser, but that’s apparently not the case. To have a guess what causes the actual error for you, you can just open up node_modules/karma/lib/reporter.js and change the method around line 42:

  return function (msg, indentation) {
    // remove domain and timestamp from source files
    // and resolve base path / absolute path urls into absolute path
    // ---
    if (msg && typeof(msg) !== 'string') {
      console.error('msg', msg);
    }
    // ---
    msg = (msg || '').replace(URL_REGEXP, function (_, prefix, path, __, ___, line, ____, column) {

For my particular case it tells me that some error occured in SystemJS, though It doesn’t tell more.

The actual fix would be either not to assume here that msg is a string, or maybe the browsers should send objects as error messages.

I had this problem a few times. I managed to fix this by checking the files: [] section in the karma.conf.js. Make sure all the sources that are needed for your application (in my case Angular2) are in that array. Just as you would add them to index.html

I have the same problem, but I already have Rx.js file with capital R. (And I’m using my desktop computer with Windows).

What else could cause this problem?