Angular 4 Unit test error - Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'ng:///DynamicTestModule/LoginComponent.ngfactory.js'

前端 未结 4 1622
花落未央
花落未央 2021-02-18 15:03

I am writing some unit tests for my component and i am getting this cryptic error message. I found a similar question at Angular 2 unit testing - getting error Failed to load &#

相关标签:
4条回答
  • 2021-02-18 15:37

    I encountered the same issue using angular-cli 6, so to get the right error message one should use the following:

    ng test --source-map=false
    

    Maybe it will help someone :) .

    0 讨论(0)
  • 2021-02-18 15:40

    I just ran into this error and the problem was my mocks. In the component.ngOnInit i used this.route.paramMap.subscribe(...) where route is an ActivatedRoute instance

    In my test i provided a mock service like this :

    providers: [
        { provide: ActivatedRoute, useValue: { snapshot: { params: { id: 1 } } } }
    ]
    

    And in fact i missed to mock the paramMap method

    Then i fix it adding a paramMap properties like this

    providers: [
        { provide: ActivatedRoute, useValue: { snapshot: { params: { id: 1 } }, paramMap: Observable.of({get: () => 1}) } }
    ]
    

    Then i don't have anymore this stupid error.

    So for you, i expect the class SessionServiceStub to be incomplete or erroneous. Does it get a login method that return an Observable ?

    If it's not the problem you can check the NotificationServiceStub

    You should use a debugger (with Webstorm it's easy to debug step-by-step) to help you.

    0 讨论(0)
  • 2021-02-18 15:52

    Been chasing this for hours. Finally discovered that the problem was simply that I had imported HttpClientModule, but not HttpClient:

    import { HttpClient, HttpClientModule } from '@angular/common/http';

    All those CORS errors, and '[Script Loader]', DOMException{stack: 'Error: Failed to execute 'send' on 'XMLHttpRequest' stuff, and it came down to just not having HttpClient!

    0 讨论(0)
  • 2021-02-18 15:56

    This is a problem with the Angular Cli version 1.2.2 or newer. Run your test with --sourcemaps=false and you will get the right error messages.

    In Angular 4-5

    • ng test --sourcemaps=false
      
      or
    • ng test -sm=false
      

    In Angular 6+

    • ng test --source-map=false
      

    See details here: https://github.com/angular/angular-cli/issues/7296

    0 讨论(0)
提交回复
热议问题