Angular & StompJS provokes Karma test load failure

2019-08-27 22:25发布

问题:

We are using Angular 5.2.0 and we recently added StompJS 2.2.3 for WS features.

  "dependencies": {
    "@angular/animations": "^5.2.0",
    "@angular/common": "^5.2.0",
    "@angular/compiler": "^5.2.0",
    "@angular/core": "^5.2.0",
    "@angular/forms": "^5.2.0",
    "@angular/http": "^5.2.0",
    "@angular/platform-browser": "^5.2.0",
    "@angular/platform-browser-dynamic": "^5.2.0",
    "@angular/router": "^5.2.0",
    "...",
    "stompjs": "^2.3.3",
  },

However, since the addition, Karma tests won't load with the error :

ERROR in ./node_modules/stompjs/lib/stomp-node.js
Module not found: Error: Can't resolve 'net' in 'C:\Workspace\Projets\HAW_WEB\common\hubanywhere-angular-common\node_modules\stompjs\lib'
 @ ./node_modules/stompjs/lib/stomp-node.js 14:8-22
 @ ./node_modules/stompjs/index.js
 @ ./src/app/common/service/stomp/stomp.service.ts
 @ ./src/app/common/service/stomp/stomp.service.spec.ts
 @ ./src \.spec\.ts$
 @ ./src/test.ts

It seems we have to allow karma to load the native 'net' packages, but I can't find the correct configuration to place in the karma.conf.js file.

Thanks for any help.

回答1:

Please use maintained version of stompjs from https://github.com/stomp-js/stompjs (npm @stomp/stompjs). There is also a specialized version for Angular https://github.com/stomp-js/ng2-stompjs (npm @stomp/ng2-stompjs).

These work well with Webpack, Rollup, RxJS and other newer libraries/tool-chain.

These have been re-written in TypeScript and all of these are distributed with type libraries. These come with guides and samples (see: https://stomp-js.github.io/).