0
0
NestJSframework~20 mins

Dependency injection basics in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NestJS Dependency Injection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this NestJS service injection?
Consider the following NestJS service and controller. What will be logged when the controller method is called?
NestJS
import { Injectable, Controller, Get } from '@nestjs/common';

@Injectable()
export class GreetingService {
  getGreeting() {
    return 'Hello, NestJS!';
  }
}

@Controller()
export class GreetingController {
  constructor(private readonly greetingService: GreetingService) {}

  @Get()
  greet() {
    console.log(this.greetingService.getGreeting());
    return this.greetingService.getGreeting();
  }
}
A"Hello, NestJS!" is logged and returned
B"undefined" is logged and returned
C"null" is logged and returned
DA runtime error occurs because GreetingService is not injected
Attempts:
2 left
💡 Hint
Think about how NestJS injects services marked with @Injectable() into controllers.
📝 Syntax
intermediate
1:30remaining
Which option correctly injects a service in NestJS?
You want to inject a service named AuthService into a controller. Which constructor syntax is correct?
Aconstructor(private readonly authService: AuthService) {}
Bconstructor(public readonly authService: AuthService) {}
Cconstructor(private authService: AuthService) {}
Dconstructor(authService: AuthService) {}
Attempts:
2 left
💡 Hint
Remember to use access modifiers and readonly for best practice.
🔧 Debug
advanced
2:30remaining
Why does this NestJS service injection fail at runtime?
Given this code, why does NestJS throw a runtime error about missing provider for LoggerService?
NestJS
import { Injectable, Controller } from '@nestjs/common';

@Injectable()
export class LoggerService {
  log(message: string) {
    console.log(message);
  }
}

@Controller()
export class AppController {
  constructor(private readonly loggerService: LoggerService) {}

  someMethod() {
    this.loggerService.log('Test');
  }
}
AAppController is not decorated with @Controller()
BLoggerService is missing the @Injectable() decorator
CLoggerService is not listed in the module's providers array
DLoggerService method log() is not public
Attempts:
2 left
💡 Hint
Check if the service is registered in the module where the controller is declared.
state_output
advanced
2:00remaining
What is the value of the injected service property after module initialization?
In this NestJS module, what will be the value of this.configService.apiKey inside AppService after the app starts?
NestJS
import { Injectable, Module } from '@nestjs/common';

@Injectable()
export class ConfigService {
  apiKey = '12345';
}

@Injectable()
export class AppService {
  constructor(public readonly configService: ConfigService) {}

  getApiKey() {
    return this.configService.apiKey;
  }
}

@Module({
  providers: [ConfigService, AppService],
  exports: [AppService]
})
export class AppModule {}
Aundefined
B"12345"
Cnull
DThrows a runtime error
Attempts:
2 left
💡 Hint
Check how ConfigService is injected and its property initialized.
🧠 Conceptual
expert
3:00remaining
Which statement best describes NestJS dependency injection scope behavior?
In NestJS, what happens when a service is provided with scope: Scope.REQUEST compared to the default singleton scope?
AThe service instance is shared globally across all requests
BThe service is instantiated once per controller instance
CThe service is instantiated once per module import
DA new instance of the service is created for each incoming request
Attempts:
2 left
💡 Hint
Think about how request-scoped services differ from singleton services.