nativescript-angular icon indicating copy to clipboard operation
nativescript-angular copied to clipboard

'socket.io error on connect: Error: xhr poll error' while importing zone.js when using Karma/Jasmine in Angular 2 with NativeScript project

Open Headcult opened this issue 10 years ago • 2 comments

Hi, I'm getting the below error while running my tests using Karma with Jasmine in my Angular2 with NativeScript project. The tests are being executed correctly, but, this error keeps repeating in the console. I observed the error is coming only after importing zone.js (imported it because it says zone is not defined while using inject, etc from Angular2/testing).

Can you please let me know how to fix this error?

Error:

JS: NSUTR-socket.io: transport error JS: NSUTR-socket.io: 1 JS: NSUTR: socket.io error on connect: Error: xhr poll error JS: NSUTR-socket.io: 2 JS: NSUTR: socket.io error on connect: Error: xhr poll error 04 05 2016 12:33:42.046:WARN [NativeScript / 23 (6.0; Google Nexus 6P - 6.0.0 - API 23 - 1440x2560)]: D isconnected (1 times), because no message in 10000 ms.

JS: NSUTR-socket.io: 3 JS: NSUTR: socket.io error on connect: Error: xhr poll error JS: NSUTR-socket.io: 4 JS: NSUTR: socket.io error on connect: Error: xhr poll error

My spec (test) file:

import 'reflect-metadata';
import 'zone.js/dist/zone.min.js';

import {Home} from '../components/home/home';
import {inject, beforeEachProviders, it, describe, expect} from 'angular2/testing';

beforeEachProviders(() => [Home]);

describe('Home Page ActionBar Title Test', () => {
  // it('Title has text', () => {
  it('Title has text', inject([Home], (homeObj: Home) => {
      expect(homeObj.title).toBe('CapacityReview');
      // expect(true).toBe(true);
    }));
});

Package.json:

 "dependencies": {
    "angular2": "2.0.0-beta.16",
    "es6-shim": "0.35.0",
    "nativescript-angular": "0.0.46",
    "nativescript-intl": "0.0.2",
    "nativescript-unit-test-runner": "^0.3.3",
    "parse5": "1.4.2",
    "punycode": "1.3.2",
    "querystring": "0.2.0",
    "reflect-metadata": "0.1.2",
    "rxjs": "5.0.0-beta.2",
    "tns-core-modules": "2.0.0",
    "url": "0.10.3",
    "zone.js": "0.6.12"
  },
  "devDependencies": {
    "babel-traverse": "6.7.6",
    "babel-types": "6.7.7",
    "babylon": "6.7.0",
    "filewalker": "0.1.2",
    "jasmine-core": "^2.4.1",
    "karma": "^0.13.22",
    "karma-jasmine": "^0.3.8",
    "karma-nativescript-launcher": "^0.4.0",
    "lazy": "1.0.11",
    "nativescript-dev-typescript": "^0.3.1",
    "typescript": "^1.8.10"
  }

karma.conf.js (default one):

module.exports = function(config) {
  config.set({

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '',

    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['jasmine'],

    // list of files / patterns to load in the browser
    files: [
      'app/**/*.js'
    ],


    // list of files to exclude
    exclude: [
    ],


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
    },


    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['progress'],


    // web server port
    port: 9876,


    // enable / disable colors in the output (reporters and logs)
    colors: true,


    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,


    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: [],

    customLaunchers: {
      android: {
        base: 'NS',
        platform: 'android'
      },
      ios: {
        base: 'NS',
        platform: 'ios'
      },
      ios_simulator: {
        base: 'NS',
        platform: 'ios',
        arguments: ['--emulator']
      }
    },

    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false
  })
}

Headcult avatar May 04 '16 07:05 Headcult

You are probably the first one to try "angular2/testing", so expect a bumpy ride. :8ball:

The only thing I notice in your configuration is the way you import zone.js. You need to import dist/zone-node, as the "nativescript-angular/application" module does here. To spare you the trouble of fixing that import once we add some hack around zone.js import time (I really wish I could say we have to do that rarely, but it wouldn't be true), it is best if you just import "nativescript-angular/application" as the first thing in your module.

hdeshev avatar May 09 '16 09:05 hdeshev

Thanks a lot for this @hdeshev!!

Like you said, I replaced my imports with this import as the first line:

import 'nativescript-angular/application';

It's resolved now and I'm able to execute Unit Tests using Karma with Jasmine in my NativeScript with Angular 2 project. I'm able to generate reports in HTML too (with an additional reporter). Thanks a ton!

Headcult avatar May 12 '16 17:05 Headcult