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

the ng:/// prefix in source-map is added by : https://github.com/angular/angular/blob/master/packages/compiler/src/compile_metadata.ts#L683 and then loaded by the CLI via source-map-loader.

So, I think we can convert all sourcemaps to base64 sourceMapUrl for testing.

asnowwolf avatar Dec 14 '17 06:12 asnowwolf

Experiencing this issue when running an Angular app within a JavaFX Webview, as opposed to unit tests. Building with sourcemaps=false fixes the issue. JavaFx under the hood this is just plain WebKit.

Specific user agent: Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/604.1 (KHTML, like Gecko) JavaFX/9 Safari/604.1

It would be interesting to know if anyone gets this when running with something like PhantomJS.

marcuscraske avatar Dec 26 '17 03:12 marcuscraske

@limpygnome we definitely had the problem when running our unit tests with PhantomJS. But I think this also happened when running them with Chrome (with the then current version, i.e. something around version 60) so maybe not restricted to WebKit based browsers (if I remember correctly we also had this when running the tests through Firefox).

UrsMetz avatar Dec 26 '17 12:12 UrsMetz

Is it recommended now to always turn off sourcemaps when running tests using ng test -sm=false?

imolorhe avatar Jan 12 '18 07:01 imolorhe

Hi all, if angular 2+ project has ejected, need: in karma.conf.js in angularCli: {.. add sourcemaps: false, for eample: angularCli: { environment: 'dev', sourcemaps: false },

asmolientsev avatar Jan 16 '18 12:01 asmolientsev

I think with Angular CLI 1.5 the problem has at least gotten better. Maybe it is even resolved completely?

UrsMetz avatar Jan 16 '18 19:01 UrsMetz

@UrsMetz I'm using Angular Cli 1.5.4 here and nothing has gotten better. This is pretty annoying.

insidewhy avatar Jan 18 '18 14:01 insidewhy

I'm experiencing this issue with CLI 1.6.3. Using --sourcemaps=false does not help at all.

kanafghan avatar Jan 29 '18 13:01 kanafghan

For me it's failing during import ngrx StoreModule.forRoot([reducers]) to test. Solution: spyOn(StoreModule, 'forRoot').and.returnValue(Promise.resolve(true));

pchodak avatar Feb 01 '18 16:02 pchodak

Experiencing this issue with CLI 1.6.1

dwilches avatar Feb 04 '18 02:02 dwilches

1.6.6. - the error is still there, unfortunately. Disabling sourcemaps give some degree of clarity, but it's clearly just a workaround. Anyone has some ideas or guidelines on how not to get this error?

ganqqwerty avatar Feb 14 '18 09:02 ganqqwerty

Seeing the issue with angular cli 1.6.5 on ubuntu with Chrome 64.0.3282.140

ekucks21 avatar Feb 14 '18 18:02 ekucks21

I am using angular cli v 1.6.5. In my case ng test --sourcemap=false is not returning any error but ng test is giving the error similar to mentioned in original issue.

As I need to generate Istanbul code coverage report, which requires sourcemap for typescript files to generate it properly, I tried to run tests using Firefox instead of Chrome and it worked well.

shripalsoni04 avatar Feb 21 '18 11:02 shripalsoni04

I'm experiencing this issue - wasted a good 2-3 hours until I found this.

Any plans to fix?

chadbr avatar Mar 20 '18 13:03 chadbr

I'm facing the same issue, but with an ejected project. How to --sourcemap=false when running tests with karma start ?

qwertoyo avatar Mar 30 '18 09:03 qwertoyo

I wrote in post before: in karma.conf.js angularCli: { environment: 'dev', sourcemaps: false },

asmolientsev avatar Mar 30 '18 09:03 asmolientsev

@asmolientsev thanks, missed that.

qwertoyo avatar Mar 30 '18 09:03 qwertoyo

Still reproduces on Angular CLI 1.7.3. --sourcemap=false flag resolves this issue

sergey-morenets avatar Apr 03 '18 09:04 sergey-morenets

I saw an "Uncaught NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'ng:///DynamicTestModule/..too when writing a new component test.

In my case the reason was a poorly written template with *ngIf="queryTable.data". Revising this to *ngIf="queryTable && queryTable.data" solved the problem for me.

I think this confirms the observation from @Toxicable https://github.com/angular/angular-cli/issues/7296#issuecomment-347359285

cff3 avatar Apr 14 '18 15:04 cff3

@cf33 as a note, you can should use the angular operator. *ngIf="query Table?.data"

gallagherrchris avatar Apr 14 '18 17:04 gallagherrchris