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.
Evan Caplan
Thanks for this. I did the same exact thing. I haven’t worked in angular for a few weeks and was totally stumped.
Starla Yang
Thanks a lot! Finally solved the bug…
anon
omg. this literally saved me. THANK YOU
anon
Omg.. thanks a lot… lost half a day looking for the problem 🙂
PAUL C VOGLER
Thank you so much!
Andrey
Thanks for sharing, its a dumb error but it was taking a lot of time
Jesús Barriga
Thank you. You article really help me.
Doug Frevert
Thanks — that fixed my problem.
Angular Dev
Get this to the top of Google search results! Thank you!
Ryan
Thank you, Robert! This helped me fix my tests and saved me a lot of time.
Jordan
You sir, are a god.
Matt
Oh man, I’ve been wasting the last 20 minutes trying to figure out my dumb mistake. Thank you so much!