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

Regression wrong error message during tests if runtime error occurs after constructor

Open JSMike opened this issue 9 years ago • 56 comments

Bug Report or Feature Request (mark with an x)

- [ x ] bug report -> please search issues before submitting
- [ ] feature request

Versions.

@angular/cli: 1.2.2 node: 6.11.0 os: win32 x64 @angular/animations: 4.3.3 @angular/common: 4.3.3 @angular/compiler: 4.3.3 @angular/core: 4.3.3 @angular/forms: 4.3.3 @angular/http: 4.3.3 @angular/platform-browser: 4.3.3 @angular/platform-browser-dynamic: 4.3.3 @angular/router: 4.3.3 @angular/cli: 1.2.2 @angular/compiler-cli: 4.3.3 @angular/language-service: 4.3.3

Repro steps.

  • Create new project with @angular/cli version 1.2.2 or newer
  • Modify src/app/app.component.ts, add OnInit interface and add any runtime error to ngOnInit
  • run ng test

Simple runtime error example:

...
export class AppComponent implements OnInit {
  title = 'app';
  example: string;

  ngOnInit() {
    console.log(this.example.length);
  }
}

The log given by the failure.

 Error: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'ng:///DynamicTestModule/AppComponent_Host.ngfactory.js'.
   at http://localhost:9876/_karma_webpack_/polyfills.bundle.js:5182:35
   at XMLHttpRequest.proto.(anonymous function) [as send] (http://localhost:9876/_karma_webpack_/polyfills.bundle.js:3767:20)
   at Array.<anonymous> (node_modules/source-map-support/browser-source-map-support.js:107:176)
   at node_modules/source-map-support/browser-source-map-support.js:101:106
   at Array.<anonymous> (node_modules/source-map-support/browser-source-map-support.js:107:474)
   at node_modules/source-map-support/browser-source-map-support.js:101:106
   at a (node_modules/source-map-support/browser-source-map-support.js:101:312)
   at b (node_modules/source-map-support/browser-source-map-support.js:104:348)
   at node_modules/source-map-support/browser-source-map-support.js:105:245
   at Array.map (native)

Desired functionality.

Display the actual error message with details about the error. Here's the output from 1.2.1.

@angular/cli: 1.2.1 node: 6.11.0 os: win32 x64 @angular/animations: 4.3.3 @angular/common: 4.3.3 @angular/compiler: 4.3.3 @angular/core: 4.3.3 @angular/forms: 4.3.3 @angular/http: 4.3.3 @angular/platform-browser: 4.3.3 @angular/platform-browser-dynamic: 4.3.3 @angular/router: 4.3.3 @angular/cli: 1.2.1 @angular/compiler-cli: 4.3.3 @angular/language-service: 4.3.3

Failed: Cannot read property 'length' of undefined
TypeError: Cannot read property 'length' of undefined
    at AppComponent.webpackJsonp.../../../../../src/app/app.component.ts.AppComponent.ngOnInit (http://localhost:9876/_karma_webpack_/main.bundle.js:120:33)
    at checkAndUpdateDirectiveInline (http://localhost:9876/_karma_webpack_/vendor.bundle.js:47071:19)
    at checkAndUpdateNodeInline (http://localhost:9876/_karma_webpack_/vendor.bundle.js:48567:17)
    at checkAndUpdateNode (http://localhost:9876/_karma_webpack_/vendor.bundle.js:48506:16)
    at debugCheckAndUpdateNode (http://localhost:9876/_karma_webpack_/vendor.bundle.js:49367:59)
    at debugCheckDirectivesFn (http://localhost:9876/_karma_webpack_/vendor.bundle.js:49308:13)
    at Object.eval [as updateDirectives] (ng:///DynamicTestModule/AppComponent_Host.ngfactory.js:8:5)
    at Object.debugUpdateDirectives [as updateDirectives] (http://localhost:9876/_karma_webpack_/vendor.bundle.js:49293:21)
    at checkAndUpdateView (http://localhost:9876/_karma_webpack_/vendor.bundle.js:48473:14)
    at callWithDebugContext (http://localhost:9876/_karma_webpack_/vendor.bundle.js:49693:42)

Mention any other details that might be useful.

This error is still occurring in 1.3.0-rc.5, the regression was added in 1.2.2, https://github.com/angular/angular-cli/commit/19f04af0777391cc72d096a0e315fe87bab161c6.

If a runtime errors occur during the constructor then the proper error displays, but sourcemaps still fail to show line in TypeScript.

It would be best if the actual error and line in typescript could be referenced.

JSMike avatar Aug 07 '17 21:08 JSMike

I solved this problem by using propa?.propb instead of propa.propb in my template, without turn off source map, and the ng:// protocol works well in Chrome.

image

ZeekoZhu avatar May 11 '18 05:05 ZeekoZhu

Angular CLI v6 doesn't seem to support the --sourcemap=false flag:

$ ng test --sourcemap=false
> Unknown option: '--sourcemap'
ng version

     _                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/
    

Angular CLI: 6.0.3
Node: 8.11.2
OS: darwin x64
Angular: 6.0.2
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

Package                            Version
------------------------------------------------------------
@angular-devkit/architect          0.6.1
@angular-devkit/build-angular      0.6.1
@angular-devkit/build-ng-packagr   0.6.3
@angular-devkit/build-optimizer    0.6.1
@angular-devkit/core               0.6.1
@angular-devkit/schematics         0.6.3
@angular/cli                       6.0.3
@ngtools/json-schema               1.1.0
@ngtools/webpack                   6.0.1
@schematics/angular                0.6.3
@schematics/update                 0.6.3
ng-packagr                         3.0.0-rc.5
rxjs                               6.1.0
typescript                         2.7.2
webpack                            4.6.0

danguilherme avatar May 25 '18 15:05 danguilherme

@danguilherme It's still there you just need to configure it via angular.json.

diminutivesloop avatar May 25 '18 17:05 diminutivesloop

@diminutivesloop Thanks, it worked!

Just for completeness, in angular.json the property is called sourceMap. If the flag --sourceMap=false (note the capital M) is passed, it works.

danguilherme avatar May 25 '18 18:05 danguilherme

In angular 6, the cryptic error message related to http call is now replaced with [object ErrorEvent] thrown

Also for command line: --source-map=false argument continues to work

EDIT: in our case we also ended up getting [object ErrorEvent] thrown when change detection threw an error that a component is destroyed, but something tried to update the view (turned out to be not mocked setTimeout in some previous test). The correct error message was visible in chrome console, but only cryptic one in command line window. This wasn't obvious as we usually run the tests in headless chrome

FDIM avatar Jun 21 '18 11:06 FDIM

any update? sourceMap=false doesn't help (angular-cli 6.0.8)

Viktor-Bredihin avatar Jul 19 '18 10:07 Viktor-Bredihin

@Viktor-Bredihin it's --source-map=false now

JSMike avatar Jul 25 '18 17:07 JSMike

Using Angular CLI 1.3.2, Angular 4.4.7, and the given demo, I get

Chrome 67.0.3396 (Mac OS X 10.13.6): Executed 0 of 4 DISCONNECTED (10.003 secs / 0 secs)

instead of the failed to execute send. The above workaround still works.

mark-wiemer avatar Aug 09 '18 21:08 mark-wiemer

We have had this issue for more than a year now. We have to switch between --source-map=true and --source-map=false all the time. Is there a better solution?

dirkluijk avatar Aug 17 '18 13:08 dirkluijk

I have found the cause. It has nothing to do with karma, but the issue is triggered by the source-map-support package which is installed by @angular/cli.

The Angular source maps refer to paths like ng:///DynamicTestModule/YourComponent.ngfactory.js which triggers CORS errors.

Should be fixed by https://github.com/evanw/node-source-map-support/pull/222 and/or by the Angular CLI.

dirkluijk avatar Aug 17 '18 15:08 dirkluijk

After deleted my node_modules and install them again with yarn, I still have the same issue as described here:

image

Here is my package.json:

{
  "name": "mywebclient",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint --type-check",
    "e2e": "ng e2e",
  },
  "dependencies": {
    "@angular/animations": "6.1.3",
    "@angular/cdk": "6.4.6",
    "@angular/common": "6.1.3",
    "@angular/compiler": "6.1.3",
    "@angular/core": "6.1.3",
    "@angular/flex-layout": "6.0.0-beta.17",
    "@angular/forms": "6.1.3",
    "@angular/http": "6.1.3",
    "@angular/material": "6.4.6",
    "@angular/platform-browser": "6.1.3",
    "@angular/platform-browser-dynamic": "6.1.3",
    "@angular/router": "6.1.3",
    "@ngx-translate/core": "10.0.2",
    "@ngx-translate/http-loader": "3.0.1",
    "@types/chart.js": "2.7.31",
    "bootstrap": "4.1.3",
    "chart.js": "2.7.2",
    "chartjs-plugin-zoom": "0.6.4",
    "core-js": "2.5.7",
    "font-awesome": "4.7.0",
    "hammerjs": "2.0.8",
    "material-design-icons": "3.0.1",
    "ng-event-source": "1.0.11",
    "rxjs": "6.2.2",
    "typedoc": "0.12.0",
    "typescript": "2.9.2",
    "zone.js": "0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "0.7.4",
    "@angular/cli": "6.1.4",
    "@angular/compiler-cli": "6.1.3",
    "@angular/language-service": "6.1.3",
    "@types/jasmine": "2.8.8",
    "@types/jasminewd2": "2.0.3",
    "@types/node": "10.7.1",
    "codelyzer": "4.4.4",
    "jasmine-core": "3.2.1",
    "jasmine-spec-reporter": "4.2.1",
    "karma": "3.0.0",
    "karma-chrome-launcher": "2.2.0",
    "karma-cli": "1.0.1",
    "karma-coverage": "1.1.2",
    "karma-coverage-istanbul-reporter": "2.0.1",
    "karma-firefox-launcher": "1.1.0",
    "karma-jasmine": "1.1.2",
    "karma-jasmine-html-reporter": "1.3.1",
    "karma-json-reporter": "1.2.1",
    "karma-junit-reporter": "1.2.0",
    "ngx-i18nsupport": "0.17.0",
    "protractor": "5.4.0",
    "ts-node": "7.0.1",
    "tslint": "5.11.0"
  }
}

TitaneBoy avatar Aug 22 '18 15:08 TitaneBoy

In my case the error was reported to unrelated test, tried to comment out tests until I found the cause.

The test alone (fit) was passing but with all test running failed causing jasmine to report other test as faield - not sure why.

In my case I was missing the error handler in subscribe() function:

service.getOperations().subscribe(() => {
      fail('Should not be here.');
    },
      () => { /* must be defined in order to test success */ }, // after adding this the error disappeared
      () => { });

felikf avatar Sep 24 '18 09:09 felikf

I found same solution like @felikf

djindjic avatar Sep 24 '18 09:09 djindjic

Closing this as I cannot reproduce locally the first issue on master:

image

Please note that the comments about subscription and asynchronicity in Jasmine are not CLI issues, and are working as intended. Do you need to have a done argument in your Jasmine test handler? That might be the cause, if your tests are asynchronous and not handling Zone properly.

hansl avatar Sep 27 '18 19:09 hansl

If you find a similar or different issue, please open a new issue so we can track that one. Original issue was fixed.

hansl avatar Sep 27 '18 19:09 hansl

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.