angular-cli: Regression wrong error message during tests if runtime error occurs after constructor

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.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 103
  • Comments: 56 (5 by maintainers)

Commits related to this issue

Most upvoted comments

@playground as a temporary workaround you can run tests with --sourcemaps=false to see the actual error message but lets keep this issue more generic, this issue occurs with any runtime error, as per the example. Your service/function may not be returning what you expect, you’ll see a better message with sourcemaps off.

@playground I had this same issue, and when I ran ng test --sourcemaps=false, I got feedback on the test that failed like “TypeError: Cannot read property ‘count’ of undefined”, where I had a child component like this:

<span>{{entity.count}}</span>

Where entity was defined in the parent component, and defined asynchronously. So I added the safe navigation operator to the property binding like {{entity?.count}}, and that error went away.

@kamok Try running ng test --sourcemaps=false (and not using npm)

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 },

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
      () => { });

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.

@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.

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

When the project has been ejected, how can we translate the --source=false option into karma.conf.js options? https://github.com/angular/angular-cli/issues/6583 mentions a few options but nothing seems very conclusive.

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

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

This approach, ng test -sm=false works, but I do not think it has solved the fundamental problem. I did some experiments today, studied it in depth, and share some results @here, hoping to be helpful.

In Chrome, some additional information appeared in the browser console of Karma DEBUG runner:

ng: ///DynamicTestModule/SandboxComponent.ngfactory.js: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

I think the root cause is here.

So, I switch to Firefox. It works.

So, I think the fundamental reason here is that Chrome’s cross-domain detection is stronger than Firefox, blocking the ng:/// protocol in the sourcemaps, causing this issue.

So, I think, maybe we can just replace the “ng: ///” prefix with a prefix such as “http: ///” to resolve this issue.

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

Any plans to fix?

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

@benelliott sorry, should have been more clear. There is no stack trace, and I was hoping that --sourcemaps=false would produce a more traceable error. image

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

Experiencing this issue with CLI 1.6.1

I found that this only happens when an error occurs during CD in my case. This can range from things like you template referencing components that aren’t in the module or having an exception thrown in a lifecycle hook or any view related errors.

to confirm this you can do

it('should fail', () => {
    component.detectChanges();
    component.componentInstance.throw();
    expect(true).toEqual(false);
  })

with your component looking like

throw(){
  throw new Error('err');
}
ngOnInit(){
  throw new Error('err');
}

Now if you run with ng test the browser will hang and eventually crash, you can see the error in the chrome log. However, if you remove the error from ngOnInit then it throws it as expected and dosen’t break then swapping to ng test --sm=false it always works as expected

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

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

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

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.

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.

I see this error ‘[object ErrorEvent] thrown’ when I run all tests. There are always 1 - 3 test failing with this error, and it is not always the same tests failing. Run it a second time and other tests will fail. If I run the spec which contains the fail test (the spec itself contains multiple tests) then the test does not fail.

Running ‘ng test --sourcemaps=false’ does not provide any additional information.

PS C:\ixia\ixnetwork-RestClient> ng test --sourcemaps=false
Your global Angular CLI version (1.5.0) is greater than your local
version (1.4.3). The local Angular CLI version is used.

To disable this warning use "ng set --global warnings.versionMismatch=false".
 10% building modules 1/1 modules 0 active13 12 2017 14:28:55.497:WARN [karma]: No captured browser, open http://localhost:9876/
13 12 2017 14:28:55.506:INFO [karma]: Karma v1.7.1 server started at http://0.0.0.0:9876/
13 12 2017 14:28:55.506:INFO [launcher]: Launching browser Chrome with unlimited concurrency
13 12 2017 14:28:55.511:INFO [launcher]: Starting browser Chrome                                                                                                                                                                                                         13 12 2017 14:29:11.979:WARN [karma]: No captured browser, open http://localhost:9876/
13 12 2017 14:29:13.870:INFO [Chrome 62.0.3202 (Windows 10 0.0.0)]: Connected on socket 9_9PO_RC50gBeZnsAAAA with id 50712456
Chrome 62.0.3202 (Windows 10 0.0.0) ERROR
  Script error.
Chrome 62.0.3202 (Windows 10 0.0.0): Executed 140 of 166 (skipped 7) ERROR (0 secs / 3.944 secs)
Chrome 62.0.3202 (Windows 10 0.0.0) ERROR
Chrome 62.0.3202 (Windows 10 0.0.0): Executed 140 of 166 (skipped 7) ERROR (4.055 secs / 3.944 secs)

I’m using chrome, and the following version.

@angular/cli: 1.4.3
node: 8.9.1
os: win32 x64
@angular/animations: 4.4.6
@angular/common: 4.4.6
@angular/compiler: 4.4.6
@angular/core: 4.4.6
@angular/forms: 4.4.6
@angular/http: 4.4.6
@angular/platform-browser: 4.4.6
@angular/platform-browser-dynamic: 4.4.6
@angular/router: 4.4.6
@angular/cli: 1.4.3
@angular/compiler-cli: 4.4.6
@angular/language-service: 4.4.6
typescript: 2.6.1

For me it’s failing in addEventListner()

  receiveMessage() {
    this.commonService.getNativeWindow().addEventListener(
      'message',
      (e) => {
        if(e.origin.match(/localhost/) && e.data['myIframe']) {
          if(!this.bypassCookie) {
            this.getConfig(e.data);
          }
        }
      }, false)
  }
zone.js:2642 XMLHttpRequest cannot load ng:///DynamicTestModule/MyComponent_Host.ngfactory.js. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.
(anonymous) @ zone.js:2642
proto.(anonymous function) @ zone.js:1227
(anonymous) @ browser-source-map-support.js:107
(anonymous) @ browser-source-map-support.js:101
(anonymous) @ browser-source-map-support.js:107
(anonymous) @ browser-source-map-support.js:101
a @ browser-source-map-support.js:101
b @ browser-source-map-support.js:104
(anonymous) @ browser-source-map-support.js:105
f @ browser-source-map-support.js:105
ExceptionFormatter.stack @ jasmine.js:1500
stack @ jasmine.js:1645
buildExpectationResult @ jasmine.js:1610
expectationResultFactory @ jasmine.js:655
Spec.addExpectationResult @ jasmine.js:342
onException @ jasmine.js:398
onException @ jasmine.js:373
onException @ jasmine.js:1988
handleException @ jasmine.js:1992
attemptSync @ jasmine.js:1952
QueueRunner.run @ jasmine.js:1938
(anonymous) @ jasmine.js:1962
(anonymous) @ jasmine.js:1905
(anonymous) @ testing.es5.js:92
webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke @ zone.js:391
webpackJsonp.../../../../zone.js/dist/async-test.js.AsyncTestZoneSpec.onInvoke @ async-test.js:49
webpackJsonp.../../../../zone.js/dist/proxy.js.ProxyZoneSpec.onInvoke @ proxy.js:76
webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke @ zone.js:390
webpackJsonp.../../../../zone.js/dist/zone.js.Zone.run @ zone.js:141
(anonymous) @ testing.es5.js:87
(anonymous) @ async-test.js:38
webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask @ zone.js:424
webpackJsonp.../../../../zone.js/dist/zone.js.Zone.runTask @ zone.js:191
webpackJsonp.../../../../zone.js/dist/zone.js.ZoneTask.invokeTask @ zone.js:498
ZoneTask.invoke @ zone.js:487
timer @ zone.js:1829
setTimeout (async)
scheduleTask @ zone.js:1839
webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.scheduleTask @ zone.js:410
webpackJsonp.../../../../zone.js/dist/zone.js.Zone.scheduleTask @ zone.js:235
webpackJsonp.../../../../zone.js/dist/zone.js.Zone.scheduleMacroTask @ zone.js:258
(anonymous) @ zone.js:1865
proto.(anonymous function) @ zone.js:1227
(anonymous) @ async-test.js:36
webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke @ zone.js:391
webpackJsonp.../../../../zone.js/dist/zone.js.Zone.run @ zone.js:141
webpackJsonp.../../../../zone.js/dist/async-test.js.AsyncTestZoneSpec._finishCallbackIfDone @ async-test.js:35
webpackJsonp.../../../../zone.js/dist/async-test.js.AsyncTestZoneSpec.onHasTask @ async-test.js:68
webpackJsonp.../../../../zone.js/dist/proxy.js.ProxyZoneSpec.onHasTask @ proxy.js:116
webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.hasTask @ zone.js:444
webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate._updateTaskCount @ zone.js:464
webpackJsonp.../../../../zone.js/dist/zone.js.Zone._updateTaskCount @ zone.js:288
webpackJsonp.../../../../zone.js/dist/zone.js.Zone.runTask @ zone.js:208
drainMicroTaskQueue @ zone.js:595
Promise resolved (async)
scheduleMicroTask @ zone.js:578
webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.scheduleTask @ zone.js:413
webpackJsonp.../../../../zone.js/dist/zone.js.Zone.scheduleTask @ zone.js:235
webpackJsonp.../../../../zone.js/dist/zone.js.Zone.scheduleMicroTask @ zone.js:255
webpackJsonp.../../../../zone.js/dist/jasmine-patch.js.jasmine.QueueRunner.ZoneQueueRunner.execute @ jasmine-patch.js:129
queueRunnerFactory @ jasmine.js:714
TreeProcessor.execute @ jasmine.js:2426
Env.execute @ jasmine.js:776
(anonymous) @ adapter.js:336
../../../../../src/test.ts @ test.ts:32
__webpack_require__ @ bootstrap 8398b79f44fceec48211:54
webpackJsonpCallback @ bootstrap 8398b79f44fceec48211:25
(anonymous) @ main.bundle.js:1
zone.js:195 Uncaught DOMException: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'ng:///DynamicTestModule/MyComponent_Host.ngfactory.js'.
    at http://localhost:9876/_karma_webpack_/webpack:/Users/ljeff/git_repo/sandbox/angular/~/zone.js/dist/zone.js:2642:1
    at XMLHttpRequest.proto.(anonymous function) [as send] (http://localhost:9876/_karma_webpack_/webpack:/Users/ljeff/git_repo/sandbox/angular/~/zone.js/dist/zone.js:1227:1)
    at Array.<anonymous> (http://localhost:9876/base/node_modules/@angular/cli/node_modules/source-map-support/browser-source-map-support.js:107:176)
    at http://localhost:9876/base/node_modules/@angular/cli/node_modules/source-map-support/browser-source-map-support.js:101:106
    at Array.<anonymous> (http://localhost:9876/base/node_modules/@angular/cli/node_modules/source-map-support/browser-source-map-support.js:107:474)
    at http://localhost:9876/base/node_modules/@angular/cli/node_modules/source-map-support/browser-source-map-support.js:101:106
    at a (http://localhost:9876/base/node_modules/@angular/cli/node_modules/source-map-support/browser-source-map-support.js:101:312)
    at b (http://localhost:9876/base/node_modules/@angular/cli/node_modules/source-map-support/browser-source-map-support.js:104:348)
    at http://localhost:9876/base/node_modules/@angular/cli/node_modules/source-map-support/browser-source-map-support.js:105:245
    at Array.map (<anonymous>)

Running cli 1.2.4