I was trying to be good and keep up with my Karma unit tests in my Angular 5 Typescript project, but I was running in to a really weird error that left a lot to be desired:
The logs in the console window are not helpful either…
Chrome 63.0.3239 (Windows 10 0.0.0) ERROR
Uncaught NetworkError: Failed to execute ‘send’ on ‘XMLHttpRequest’: Failed to load ‘ng:///DynamicTestModule/MyComponent.ngfactory.js’.
After a whole bunch of Googling, looking at GitHub issues, and StackOverflow posts, it turns out the answer is pretty simple.
MyComponent includes an @Input, which is satisfied when the component is created (as part of a different component).
The view for MyComponent uses a function defined on the @Input’ed class, so it depends on it being defined to work properly.
This is fine when you run the application, but the unit test runner injects itself straight into the middle… so that @Input isn’t satisfied automatically. Instead, you have to satisfy it yourself in the start-up portion of the test definition:
NOTE that I don’t think this would be an issue if I wasn’t using properties and methods from the dependent class. At any rate, the error message was so ambiguous (I doubt this is the only cause for this generic error to happen), I thought it was worth jotting down.
MaxAxeHax
You saved my ass my dude. It was defintely worth it jotting it down :)))
[object ErrorEvent] thrown … again – Darchuk.NET
[…] I keep seeing this error while trying to write Angular tests. I… *think* this happens when something goes wrong during the constructor of a component. I’ve written before about how it could be due to an @Input property not being initialized properly. […]