angular-cli: ng test - TypeError: (msg || "").replace is not a function

When you generate new project with ‘ng new’ and immediately run tests - they work. But when going a bit further and adding some dependencies (like jQuery, Bootstrap 4 and Trumbowyg) tests starts to fail right away with TypeError: (msg || “”).replace is not a function. Here’s the output:

10 05 2016 04:19:17.581:WARN [karma]: No captured browser, open http://localhost:9876/
10 05 2016 04:19:17.595:INFO [karma]: Karma v0.13.22 server started at http://localhost:9876/
10 05 2016 04:19:17.600:INFO [launcher]: Starting browser Chrome
10 05 2016 04:19:18.359:INFO [Chrome 50.0.2661 (Windows 10 0.0.0)]: Connected on socket /#-kPxpXeuzKFiXoPcAAAA with id 7707656
Missing error handler on `socket`.
TypeError: (msg || "").replace is not a function
    at D:\Documents\Visual Studio 2015\Projects\TestAppCLI\node_modules\karma\lib\reporter.js:45:23
    at onBrowserError (D:\Documents\Visual Studio 2015\Projects\TestAppCLI\node_modules\karma\lib\reporters\base.js:58:60)
    at null.<anonymous> (D:\Documents\Visual Studio 2015\Projects\TestAppCLI\node_modules\karma\lib\events.js:13:22)
    at emitTwo (events.js:100:13)
    at emit (events.js:185:7)
    at onKarmaError (D:\Documents\Visual Studio 2015\Projects\TestAppCLI\node_modules\karma\lib\browser.js:95:13)
    at Socket.<anonymous> (D:\Documents\Visual Studio 2015\Projects\TestAppCLI\node_modules\karma\lib\events.js:13:22)
    at emitOne (events.js:95:20)
    at Socket.emit (events.js:182:7)
    at Socket.onevent (D:\Documents\Visual Studio 2015\Projects\TestAppCLI\node_modules\karma\node_modules\socket.io\lib\socket.js:335:8)
    at Socket.onpacket (D:\Documents\Visual Studio 2015\Projects\TestAppCLI\node_modules\karma\node_modules\socket.io\lib\socket.js:295:12)
    at Client.ondecoded (D:\Documents\Visual Studio 2015\Projects\TestAppCLI\node_modules\karma\node_modules\socket.io\lib\client.js:193:14)
    at Decoder.Emitter.emit (D:\Documents\Visual Studio 2015\Projects\TestAppCLI\node_modules\karma\node_modules\socket.io\node_modules\socket.io-parser\node_modules\component-emitter\index.js:134:20)
    at Decoder.add (D:\Documents\Visual Studio 2015\Projects\TestAppCLI\node_modules\karma\node_modules\socket.io\node_modules\socket.io-parser\index.js:247:12)
    at Client.ondata (D:\Documents\Visual Studio 2015\Projects\TestAppCLI\node_modules\karma\node_modules\socket.io\lib\client.js:175:18)
    at emitOne (events.js:90:13)
    at Socket.emit (events.js:182:7)
    at Socket.onPacket (D:\Documents\Visual Studio 2015\Projects\TestAppCLI\node_modules\karma\node_modules\socket.io\node_modules\engine.io\lib\socket.js:101:14)
    at emitOne (events.js:90:13)
    at WebSocket.emit (events.js:182:7)
    at WebSocket.Transport.onPacket (D:\Documents\Visual Studio 2015\Projects\TestAppCLI\node_modules\karma\node_modules\socket.io\node_modules\engine.io\lib\transport.js:104:8)
    at WebSocket.Transport.onData (D:\Documents\Visual Studio 2015\Projects\TestAppCLI\node_modules\karma\node_modules\socket.io\node_modules\engine.io\lib\transport.js:115:8)
10 05 2016 04:19:21.367:WARN [Chrome 50.0.2661 (Windows 10 0.0.0)]: Disconnected (1 times)

Does anyone know how to fix it? It’s really annoying. I already tried to recreate project from scratch, step-by-step => no luck 😦

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 26 (8 by maintainers)

Most upvoted comments

I am also receiving this error using CLI version 1.0.0-beta.9. Haven’t tried beta.10 yet.

It only happens when I try to return a mocked Observable like this:

class MockClientEnvironmentsService {
  public getData() {
    return <Observable<RecentIndexesItem[]>> new Observable((observer) => {
      observer.next([
        new ClientEnvironmentItem()
      ]);
    })
  }
}

When I comment out the return block all is well.

Here’s the error for reference:

20 07 2016 10:39:19.400:WARN [web-server]: 404: /base/dist/vendor/rxjs/index.js
Missing error handler on `socket`.
TypeError: (msg || "").replace is not a function
    at /Users/bmcalister/Documents/DEV/platform-indexing-dashboard/node_modules/kar
ma/lib/reporter.js:45:23
    at onBrowserError (/Users/bmcalister/Documents/DEV/platform-indexing-dashboard/
node_modules/karma/lib/reporters/base.js:58:60)
    at .<anonymous> (/Users/bmcalister/Documents/DEV/platform-indexing-dashboard/no
de_modules/karma/lib/events.js:13:22)
    at emitTwo (events.js:106:13)
    at emit (events.js:191:7)
    at onKarmaError (/Users/bmcalister/Documents/DEV/platform-indexing-dashboard/no
de_modules/karma/lib/browser.js:95:13)
    at Socket.<anonymous> (/Users/bmcalister/Documents/DEV/platform-indexing-dashbo
ard/node_modules/karma/lib/events.js:13:22)
    at emitOne (events.js:101:20)
    at Socket.emit (events.js:188:7)
    at Socket.onevent (/Users/bmcalister/Documents/DEV/platform-indexing-dashboard/
node_modules/socket.io/lib/socket.js:335:8)
    at Socket.onpacket (/Users/bmcalister/Documents/DEV/platform-indexing-dashboard
/node_modules/socket.io/lib/socket.js:295:12)
    at Client.ondecoded (/Users/bmcalister/Documents/DEV/platform-indexing-dashboar
d/node_modules/socket.io/lib/client.js:193:14)
    at Decoder.Emitter.emit (/Users/bmcalister/Documents/DEV/platform-indexing-dash
board/node_modules/component-emitter/index.js:134:20)
    at Decoder.add (/Users/bmcalister/Documents/DEV/platform-indexing-dashboard/nod
e_modules/socket.io-parser/index.js:247:12)
    at Client.ondata (/Users/bmcalister/Documents/DEV/platform-indexing-dashboard/n
ode_modules/socket.io/lib/client.js:175:18)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at Socket.onPacket (/Users/bmcalister/Documents/DEV/platform-indexing-dashboard
/node_modules/engine.io/lib/socket.js:101:14)
    at emitOne (events.js:96:13)
    at WebSocket.emit (events.js:188:7)
    at WebSocket.Transport.onPacket (/Users/bmcalister/Documents/DEV/platform-index
ing-dashboard/node_modules/engine.io/lib/transport.js:104:8)
    at WebSocket.Transport.onData (/Users/bmcalister/Documents/DEV/platform-indexin
g-dashboard/node_modules/engine.io/lib/transport.js:115:8)
20 07 2016 10:39:21.531:WARN [Chrome 51.0.2704 (Mac OS X 10.11.5)]: Disconnected (1
 times)

When I’ve had this problem, it has meant that there was a TypeScript compilation error someplace else in the code. So, once I fixed all of the errors, the problem went away.

This error is unhelpful because it hides the actual problem, but perhaps you’ll be fine if there are other errors that you can clean up.

I got the similar error when Angular CLI is upgraded to 1.0.0-beta.11-webpack.2 and @angular2-material to 2.0.0-alpha.7-4, when run ng test in the command line, and got the following info.

Missing error handler on `socket`.                                              
TypeError: (msg || "").replace is not a function                                
    at E:\hantsylabs\angular2-sample\node_modules\karma\lib\reporter.js:45:23   
    at onBrowserError (E:\hantsylabs\angular2-sample\node_modules\karma\lib\repo
rters\base.js:58:60)                                                            
    at .<anonymous> (E:\hantsylabs\angular2-sample\node_modules\karma\lib\events
.js:13:22)                                                                      
    at emitTwo (events.js:111:20)                                               
    at emit (events.js:191:7)                                                   
    at onKarmaError (E:\hantsylabs\angular2-sample\node_modules\karma\lib\browse
r.js:95:13)                                                                     
    at Socket.<anonymous> (E:\hantsylabs\angular2-sample\node_modules\karma\lib\
events.js:13:22)                                                                
    at emitOne (events.js:101:20)                                               
    at Socket.emit (events.js:188:7)                                            
    at Socket.onevent (E:\hantsylabs\angular2-sample\node_modules\socket.io\lib\
socket.js:335:8)                                                                
    at Socket.onpacket (E:\hantsylabs\angular2-sample\node_modules\socket.io\lib
\socket.js:295:12)                                                              
    at Client.ondecoded (E:\hantsylabs\angular2-sample\node_modules\socket.io\li
b\client.js:193:14)                                                             
    at Decoder.Emitter.emit (E:\hantsylabs\angular2-sample\node_modules\componen
t-emitter\index.js:134:20)                                                      
    at Decoder.add (E:\hantsylabs\angular2-sample\node_modules\socket.io-parser\
index.js:247:12)                                                                
    at Client.ondata (E:\hantsylabs\angular2-sample\node_modules\socket.io\lib\c
lient.js:175:18)                                                                
    at emitOne (events.js:96:13)                                                
    at Socket.emit (events.js:188:7)                                            
    at Socket.onPacket (E:\hantsylabs\angular2-sample\node_modules\engine.io\lib
\socket.js:101:14)                                                              
    at emitOne (events.js:96:13)                                                
    at WebSocket.emit (events.js:188:7)                                         
    at WebSocket.Transport.onPacket (E:\hantsylabs\angular2-sample\node_modules\
engine.io\lib\transport.js:104:8)                                               
    at WebSocket.Transport.onData (E:\hantsylabs\angular2-sample\node_modules\en
gine.io\lib\transport.js:115:8)                                                 
23 08 2016 22:47:01.612:DEBUG [Chrome 52.0.2743 (Windows 10 0.0.0)]: Disconnecte
d during run, waiting 2000ms for reconnecting.                                  

Thanks, but that didn’t help. Same error.

We came across this very issue as well.

We fixed it by adding symbol-observable dependency:

//app/system-config.ts
const map: any = {
  'symbol-observable': 'vendor/symbol-observable',
  ...
}

const packages: any = {
   'symbol-observable': {
    main: 'index'
  },
  ...
}
//angular-cli-build.js
var Angular2App = require('angular-cli/lib/broccoli/angular2-app');

module.exports = function(defaults) {
  return new Angular2App(defaults, {
    vendorNpmFiles: [
      ...
      'symbol-observable/*.js',
      ...
    ]
  });
};