Recently, I was writing some unit tests for Angular. It was a pretty basic test, just checking to see if a method on an injected provider was called when a method in my component executed… but suddenly everything blew up with this weird error:
TypeError: Cannot read property ‘ngInjectableDef’ of undefined
Googling around didn’t really help, as everything seemed to be GitHub issues related to specific Angular projects, and none of them really had any solutions.
My test code looked pretty straight-foward to me:
And VSCode / TSLint didn’t show any errors… everything should work…
But then I realized I had a dumb. When you TestBed.get, you need to pass in the type (as in, capital letter).
TestBed.get(MyService);
… And then everything works.
Thanks for this. I did the same exact thing. I haven’t worked in angular for a few weeks and was totally stumped.
Thanks a lot! Finally solved the bug…
omg. this literally saved me. THANK YOU
Omg.. thanks a lot… lost half a day looking for the problem 🙂
Thank you so much!
Thanks for sharing, its a dumb error but it was taking a lot of time
Thank you. You article really help me.
Thanks — that fixed my problem.
Get this to the top of Google search results! Thank you!
Thank you, Robert! This helped me fix my tests and saved me a lot of time.
You sir, are a god.
Oh man, I’ve been wasting the last 20 minutes trying to figure out my dumb mistake. Thank you so much!