7

I have migrated Angular 4 app to Angular 6 and now Test cases fail with following error message in terminal and it shows success on browser. Also, sometimes test cases pass without showing any error message. So we can say I get following error 60% of the times I run ng test.

Chrome 67.0.3396 (Linux 0.0.0) ERROR
 {
    "message": {
      "isTrusted": true
    },
    "str": "[object ErrorEvent]"
  }

I have tried upgrading and downgrading jasmine and jasmine-core

This is my package.json

{
  "name": "anonymos",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^6.0.3",
    "@angular/common": "^6.0.3",
    "@angular/compiler": "^6.0.3",
    "@angular/core": "^6.0.3",
    "@angular/forms": "^6.0.3",
    "@angular/http": "^6.0.3",
    "@angular/platform-browser": "^6.0.3",
    "@angular/platform-browser-dynamic": "^6.0.3",
    "@angular/router": "^6.0.3",
    "@ng-bootstrap/ng-bootstrap": "^2.2.0",
    "angular-cookie": "^4.1.0",
    "angular-in-memory-web-api": "^0.6.0",
    "angular2-cookie": "^1.2.6",
    "angular2-recaptcha": "1.1.0",
    "body-parser": "1.18.3",
    "bootstrap": "4.1.2",
    "cookie-parser": "1.4.3",
    "core-js": "^2.5.4",
    "es6-shim": "0.35.3",
    "http-server": "0.11.1",
    "jquery": "^3.3.1",
    "js-base64": "2.4.8",
    "node-sass": "^4.9.2",
    "popper.js": "^1.14.3",
    "reflect-metadata": "0.1.12",
    "rxjs": "^6.0.0",
    "zone.js": "^0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.6.8",
    "@angular/cli": "~6.0.8",
    "@angular/compiler-cli": "^6.0.3",
    "@angular/language-service": "^6.0.3",
    "@types/jasmine": "~2.8.6",
    "@types/jasminewd2": "~2.0.3",
    "@types/jquery": "3.3.4",
    "@types/node": "^8.10.21",
    "codelyzer": "~4.2.1",
    "jasmine": "^3.1.0",
    "jasmine-core": "^2.9.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "^2.0.4",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.0",
    "karma-jasmine": "~1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.3.0",
    "ts-node": "~5.0.1",
    "tslint": "~5.9.1",
    "typescript": "~2.7.2"
  }
}

If anything else is needed to debug please let me know. I can't upload test cases as they are around 350 of them.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • same here...could you find the solution? – DAG Aug 14 '18 at 09:08
  • @DAG No man! I am searching for the solution from past 2 weeks. –  Bobin Singla Aug 16 '18 at 07:49
  • I was getting this but got around it by narrowing down the files I was adding to the files array in Karma.conf.js. Basically I was including polyfills.ts and this file was causing the issue. I excluded it and the error disappeared. Not sure why yet – Neil Aug 20 '18 at 12:26

2 Answers2

0

I solved this by adding these imports to the test.ts file:

import 'zone.js/dist/long-stack-trace-zone';
import 'zone.js/dist/proxy.js';
import 'zone.js/dist/sync-test';
import 'zone.js/dist/mocha-patch';
import 'zone.js/dist/async-test';
import 'zone.js/dist/fake-async-test';

Don't know the exact cause. But it did it for me.

NOTE: I'm using mocha and not jasmine for testing, so I guess you need to change out 'zone.js/dist/mocha-patch' for 'zone.js/dist/jasmine-patch'

fredrik
  • 17,537
  • 9
  • 51
  • 71
0

adding teardown: { destroyAfterEach: true in test.ts fixed it for me

`getTestBed().initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting(), {
  teardown: { destroyAfterEach: true }
});`
bokkie
  • 1,477
  • 4
  • 21
  • 40