-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
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
NestJS doesn't error when provider's dependencies haven't been resolved #13059
Comments
I believe that this is a limitation of typescript, and could be handled by a linter rule We can add classes without the There is no way to nestjs to know if you missed the |
Why is this allowed? Allowing classes to be in the providers without |
The |
Okay, also read https://github.com/nestjs/docs.nestjs.com/pull/2481/files. So nest can't possibly resolve dependencies of a class (with a non-empty constructor) in the providers array without at least a @Inject / @Injectable decorator because there won't be enough metadata to do so. Is that correct? |
@snigdha920 exactly and nestjs can't tell if a class has a non-empty constructor if the class has no decorators. That's why I believe this is a limitation we can't fix on nestjs side |
Is there an existing issue for this?
Current behavior
We have the case where:
BookService
BookModule
BookController
The
BookService
injects another serviceAuthorService
, butbookService.authorService
is undefined and nest does not error out - no errors on theBookService
orBookController
(which is using the service)In a unit test, importing the
BookModule
and then doingservice
isdefined
. butbookService.authorService
isundefined
, because it was not injected correctly.Minimum reproduction code
https://github.com/snigdha920/nestjs-di
Steps to reproduce
Steps to reproduce
pnpm i
pnpm start:dev
in the rootBookService
constructor:BookService.authorService is undefined
book.service.spec.ts
, we successfully get theBookService
from the module context, butbookService.authorService
isundefined
. Run the test bypnpm test
Expected behavior
This behaviour is weird, what I expect is:
Since
BookService
is included in the providers array, nest should throw an error can't resolve dependancies ofBookService
OR
Since
BookService
is being used by theBookController
, theBookController
should throw an error that it couldn't resolve theBookService
dependency since the dependencies ofBookService
itself hasn't been resolvedOR
The unit test to should error saying it could not find the provider
BookService
in the module context sinceBookService
hasn't been initialised properly because it's dependencies weren't resolvedI basically want some error at compile time that we're using a service in a controller whose dependencies have not been resolved. This service is also included in the
providers
array, so there should be some error at compile time.Is there a unit test I can write that catches this? Instead of having to manually check for every dependency of a service in a unit test (which I cannot do if
BookService.authorService
was a private field)Package
@nestjs/common
@nestjs/core
@nestjs/microservices
@nestjs/platform-express
@nestjs/platform-fastify
@nestjs/platform-socket.io
@nestjs/platform-ws
@nestjs/testing
@nestjs/websockets
Other package
No response
NestJS version
10.3.0
Packages versions
Node.js version
20.10.0
In which operating systems have you tested?
Other
No response
The text was updated successfully, but these errors were encountered: