I am trying to test my angular 4.1.0 component -
export class CellComponent implements OnInit {
lines: Observable<Array<ILine>>;
@Input() dep: string;
@Input() embedded: boolean;
@Input() dashboard: boolean;
constructor(
public dataService: CellService,
private route: ActivatedRoute,
private router: Router, private store: Store<AppStore>) {
}
}
However, a simple "should create" test throws this cryptic error...
NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'ng:///DynamicTestModule/module.ngfactory.js'.
so I found this question, which suggests that the issue is the component has @Input)_
params which aren't set, however, if I modify my test like so:
it('should create', inject([CellComponent], (cmp: CellComponent) => {
cmp.dep = '';
cmp.embedded = false;
cmp.dashboard = false;
expect(cmp).toBeTruthy();
}));
then I still get the same issue, similarly, if I remove the @Input()
annotations from the component, still no difference. How can I get these tests to pass?
This can be related to Chrome hiding an actual test error. The test area will be confusing some mock http factory that it can't load and therefore that is the error it will report. Most likely the error will be around the ngOnInit area where an object is, say, expecting sub objects and they are not defined.
To try and get to the bottom of the error, switch to PhantomJS temporarily which seems to suffer less from these initialisation errors and this will hopefully report the actual error to you. Several occasions I found to be where an object expected on initialisation wasn't complete. IE:
Correcting the object allowed PhantomJS to complete and also Chrome to move on to the next test.
Other than that I've not seen a solution to remove the issue from Chrome. As ever try and adopt a "remove the code, until error goes" policy to chase down the error.
I also had this error, which truth be told is fairly non talkative.
It was related to the HTTP calls trough my services
I use myService.ts with 2 methods
I am mocking this service : mockMyService.ts
The error was here because my component was using getAll() method that I forgot to implement in the mockMyService, so I just added the method :
Error was gone :)
For my case there was a mock data problem and in case of
Array
, I was returningstring
from the mock.Many time it happens when you mock data is incomplete or incorrect.
As suggested above here: https://stackoverflow.com/a/45570571/7085047 my problem was in my
ngOnInit
. I was calling a mock swagger-generated REST controller proxy. It was returning null, and I was subscribing to that null, which doesn't work...The error came back:
Failed to load ng:///DynamicTestModule/MockNodeDashboardComponent_Host.ngfactory.js: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.
I fixed the issue using ts-mockito: https://github.com/NagRock/ts-mockito
I added code to create a mock instance like this:
And then added the instance to the test's providers array like this:
I faced the same issue and I've found out that to fix it you have to set your inputs for the component in the method beforeEach as shown below:
This will definitely resolve your issue.
You can either set input() property to default value in component.ts
OR
Modify your component.spec.ts file in following way,