Dependency Injection is a design pattern where NestJS automatically provides instances of classes (dependencies) to other classes that need them, instead of creating them manually.
Click to reveal answer
beginner
How does NestJS know which dependencies to inject?
NestJS uses decorators like @Injectable() to mark classes as providers and uses the constructor parameters to identify which dependencies to inject.
Click to reveal answer
beginner
What role does the @Injectable() decorator play in NestJS DI?
The @Injectable() decorator marks a class as a provider so NestJS can manage its lifecycle and inject it where needed.
Click to reveal answer
beginner
What is a provider in NestJS?
A provider is a class that can be injected as a dependency. It usually contains business logic or services that other parts of the app need.
Click to reveal answer
beginner
How do you inject a service into a controller in NestJS?
You add the service as a parameter in the controller's constructor. NestJS then injects the service instance automatically.
Click to reveal answer
Which decorator marks a class as injectable in NestJS?
A@Service()
B@Controller()
C@Module()
D@Injectable()
✗ Incorrect
The @Injectable() decorator tells NestJS that the class can be injected as a dependency.
How does NestJS inject dependencies into a class?
ABy passing dependencies through the constructor
BBy calling a special inject() method
CBy manually creating instances inside the class
DBy using global variables
✗ Incorrect
NestJS uses constructor parameters to inject dependencies automatically.
What is a provider in NestJS?
AA database connection
BA special type of controller
CA class that can be injected as a dependency
DA configuration file
✗ Incorrect
Providers are classes that NestJS can inject into other classes as dependencies.
Which of these is NOT a typical use of dependency injection?
ATo create tight coupling between classes
BTo make testing easier
CTo manage class lifecycles
DTo improve code modularity
✗ Incorrect
Dependency injection helps reduce tight coupling, not create it.
Where do you declare providers so NestJS can inject them?
AIn the package.json file
BIn the module's providers array
CIn the main.ts file
DInside the controller's methods
✗ Incorrect
Providers must be declared in the module's providers array for NestJS to manage and inject them.
Explain how dependency injection works in NestJS and why it is useful.
Think about how NestJS provides classes to others without manual creation.
You got /4 concepts.
Describe the steps to inject a service into a controller in NestJS.
Focus on decorators, module setup, and constructor usage.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of dependency injection in NestJS?
easy
A. To automatically provide required services to classes
B. To manually create instances of services inside components
C. To write all code inside a single file
D. To avoid using classes and use only functions
Solution
Step 1: Understand dependency injection concept
Dependency injection means NestJS automatically gives the needed services to classes without manual creation.
Step 2: Compare options with this concept
Only To automatically provide required services to classes describes automatic service provision, others describe manual or unrelated actions.
Final Answer:
To automatically provide required services to classes -> Option A
Quick Check:
Dependency injection = automatic service provision [OK]
Hint: Dependency injection means NestJS gives services automatically [OK]
Common Mistakes:
Thinking you must create service instances manually
Confusing dependency injection with code organization only
Believing dependency injection removes classes
2. Which decorator marks a class as injectable for NestJS dependency injection?
easy
A. @Module()
B. @Controller()
C. @Injectable()
D. @Service()
Solution
Step 1: Recall NestJS decorators for DI
Classes that can be injected must be marked with @Injectable() decorator.
Step 2: Check each option
@Controller is for controllers, @Module for modules, @Service is not a NestJS decorator.
Final Answer:
@Injectable() -> Option C
Quick Check:
@Injectable() marks injectable classes [OK]
Hint: Use @Injectable() to mark services for injection [OK]
Common Mistakes:
Using @Controller or @Module instead of @Injectable
Assuming @Service is a valid NestJS decorator
Forgetting to add any decorator
3. Given this code snippet, what will be the output when appService.getMessage() is called?
import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
getMessage() {
return 'Hello from AppService';
}
}
export class AppController {
constructor(private readonly appService: AppService) {}
showMessage() {
return this.appService.getMessage();
}
}
medium
A. Error: AppService not provided
B. 'Hello from AppService'
C. undefined
D. 'Hello from AppController'
Solution
Step 1: Understand injection in constructor
AppController receives AppService instance via constructor injection because AppService is marked @Injectable.
Step 2: Trace method call
showMessage calls appService.getMessage(), which returns 'Hello from AppService'.
Final Answer:
'Hello from AppService' -> Option B
Quick Check:
Injected service method returns message [OK]
Hint: Injected service methods return their defined values [OK]
Common Mistakes:
Assuming injection fails without @Module setup
Expecting AppController message instead
Confusing method return values
4. Identify the error in this NestJS service injection code:
UserService is injected into UserController but lacks @Injectable(), so NestJS cannot inject it.
Step 2: Verify other options
Constructor in controller is valid, fetchUser need not be async, and private readonly is correct for injection.
Final Answer:
UserService is missing @Injectable() decorator -> Option A
Quick Check:
Injectable classes must have @Injectable() [OK]
Hint: Injectable services must have @Injectable() decorator [OK]
Common Mistakes:
Forgetting @Injectable on services
Thinking constructor is invalid in controllers
Assuming async is required for injected methods
5. You want to inject two different services, LoggerService and ConfigService, into a controller. Which is the correct way to do this in NestJS?
hard
A. Inject only one service and import the other directly
B. Create instances manually inside the controller constructor
C. Use @Injectable() on the controller and call services statically
D. Use constructor injection with both services as parameters marked private readonly
Solution
Step 1: Recall how to inject multiple services
In NestJS, multiple services are injected by listing them as constructor parameters with access modifiers like private readonly.
Step 2: Evaluate options
Use constructor injection with both services as parameters marked private readonly correctly uses constructor injection. Create instances manually inside the controller constructor manually creates instances, breaking DI. Use @Injectable() on the controller and call services statically misuses @Injectable on controller and static calls. Inject only one service and import the other directly injects only one service, which is incomplete.
Final Answer:
Use constructor injection with both services as parameters marked private readonly -> Option D
Quick Check:
Multiple services injected via constructor parameters [OK]
Hint: Inject multiple services by listing them in constructor [OK]
Common Mistakes:
Manually creating service instances inside controllers
Using static calls instead of injection
Injecting only one service when multiple are needed
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.