-
Notifications
You must be signed in to change notification settings - Fork 169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
question: How do you test your services with dependency injection? #1055
Comments
One approach is to use Container.set and Container.get in your tests.
Then in your test, you do
|
I did exactly that and suddenly it stopped working:
I cannot get it to work, and it doesn't have any sense whatsoever to stop working on me like that |
how are you injecting ExampleApiClient into ServiceUsingExampleApiClient ? |
Could you create a repo with this? Its hard to see whats wrong without context. |
I cannot publish code from my work, and this is not reproducible since it doesnt work in that one only case, for now I can say that I've tried using Service() via type, and named Service() syntax (Service('name-of-service')) along with @Inject('name-of-service') and it doesnt work in either way. |
What is funny, is that when I remove type from Inject() statement, it says:
Needless to say, I've tried using only Service() and type syntax, without names, and it doesn't work - injected value in AddPocketSectionItem is just undefined |
It's weird because it works for me locally. Is there a chance your lib is in a monorepo? |
Yes, but other apps are not using any DI containers. It stopped working when I added some new code (unrelated to this), using httpContext for acquiring some headers from each request, and setting getConfig() method in a container via Container.set('get-config', getConfig). It's tested and mockable
those tests are passing |
The problem is the metadata storage of the DI, technically it's tied to your node_modules folder. If your app happens to be in a monorepo that has separate node_module instances for each part, you are kinda out of luck. The Container instance will be different for each. Could you verify if that's the case? |
First of all, thanks for your help, it's really kind of you. I verified, my API only uses one exclusive node_modules. |
That is expected, if you never reference the class that registers the token it won't be available in the container.
This is the more interesting part, because if the container is not able to find the token it should throw an error. |
In test it returns mocked one, in service's body there is undefined, without error |
Do you use |
|
Tried both, it still doesn't work (sorry, I got excited because test passed) |
Keep in mind that with |
After using constructor injection (without @Inject() decorator) it just injects original dependency instead of mock:
|
|
Do you clear your container after each test? It could be that the container instanciated your service already, so you setting the dependency of your service does nothing since |
That ought to be the case here! How do I clear container? I searched in documentation, and I didn't see anything like "reset" |
|
It doesn't work unfortunately, still getting original service instead of mocked one |
Just for the sake of the test, could you remove the |
|
Ok at this point all I can think of is that something else creates that service for you beforehand somewhere. Could you try to reset the container in the test itself before setting the mock? |
Yeah, I've tried that with the same effect. No worries mate, I'm done for today anyways, tomorrow I will try some other ideas Thank you so much |
You could add a log before the |
its respectively |
pinging @NoNameProvided for additional ideas |
maybe as an experiment try changing the implementation of AddPocketSectionItem to
|
See this |
It works for me! |
Im trying to test some global standalone classes injected in my code, Im new to Typedi, I really like it, but the lack of documentation is killing me.
My problem is mostly with testing, I see no example at all of how to test with typedi, not sure about you guys, but I just took as granted that some examples of how to test with jest (or whatever) will be present, and now I found that maybe I just lost a week cause there is no example at all.
How do you test your injected dependencies?
By the way, not as manually parametrized, pls, I saw a lot of this in huge amount of helping places, not sure why, but If I could send my classes instances as parameters I will not need dependency injection at all.
The text was updated successfully, but these errors were encountered: