Angular 2 unit testing - getting error Failed to load 'ng:///DynamicTestModule/module.ngfactory.js'

前端 未结 10 2377
执念已碎
执念已碎 2020-12-23 11:04

I have angular 2 webpack application, all webpack,karma configuration created as per angular.io webpack guide. I am not using aot. I am writing jasmine unit test spec to te

相关标签:
10条回答
  • 2020-12-23 11:49

    I ran into this issue myself yesterday. The problem was that I had an Input() property on my component class that I wasn't setting in the test. So for example, in my-component.ts:

    @Component({
      selector: 'my-component'
    })
    export class MyComponent {
      @Input() title: string;
    }
    

    and my-component.spec.ts:

    beforeEach(() => {
      fixture = TestBed.createComponent(MyComponent);
      component = fixture.componentInstance;
      component.title = 'Hello there!' // <-- this is required!
      fixture.detectChanges();
    });
    

    Or you could provide a default value in the component somewhere. Either way, the test will crash if any inputs are not set and you'll get that unintuitive error.

    Note: Running ng test -sm=false will give the actual error message causing the problem. Credit: https://stackoverflow.com/a/45802115/61311

    0 讨论(0)
  • 2020-12-23 11:52

    running tests with --sourcemaps=false will not fail Karma silently but give you some detail about the error instead.

    0 讨论(0)
  • 2020-12-23 11:52

    I just had this issue as well. It turned out to be a simple null reference error in my component in one of my *ngIf checks.

    I would suggest running ng serve and checking the component is working in the browser without any errors, or simply run ng test --source-map=false to get a more useful error message.

    0 讨论(0)
  • 2020-12-23 11:54

    I had the same error when I accessed a variable of an undefined object.

    Example:

    Component:

    soemthing = {}
    

    Template:

    <demo-something [someinput]="something.anotherthing.data"> ...
    

    So something was defined, anotherthing was undefined and data could therefor not be accessed.

    Very annoying error and as far as I could tell not in the list yet :)

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