0
0
NestJSframework~20 mins

Injectable decorator in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NestJS Injectable Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a service is decorated with @Injectable() in NestJS?

Consider a service class in NestJS decorated with @Injectable(). What is the main effect of this decorator on the service?

NestJS
import { Injectable } from '@nestjs/common';

@Injectable()
export class MyService {
  getMessage() {
    return 'Hello from service';
  }
}
AIt marks the class as a provider that can be injected into other classes by NestJS's dependency injection system.
BIt makes the class a middleware that intercepts requests before reaching controllers.
CIt automatically registers the class as a controller to handle HTTP requests.
DIt disables dependency injection for the class, making it standalone.
Attempts:
2 left
💡 Hint

Think about what NestJS needs to do to create and share instances of this class.

📝 Syntax
intermediate
2:00remaining
Which code snippet correctly uses @Injectable() with a scope in NestJS?

In NestJS, you can specify the scope of an injectable provider. Which of the following code snippets correctly sets the scope to Scope.REQUEST?

A
import { Injectable, Scope } from '@nestjs/common';

@Injectable({ scope: 'REQUEST' })
export class RequestService {}
B
import { Injectable } from '@nestjs/common';

@Injectable(Scope.REQUEST)
export class RequestService {}
C
import { Injectable, Scope } from '@nestjs/common';

@Injectable({ scope: Scope.REQUEST })
export class RequestService {}
D
import { Injectable } from '@nestjs/common';

@Injectable({ scope: Scope.REQUEST })
export class RequestService {}
Attempts:
2 left
💡 Hint

Check how the scope option is passed as an object property.

🔧 Debug
advanced
2:30remaining
Why does this NestJS service fail to inject into a controller?

Given the following code, why does NestJS throw an error that MyService is not a provider?

NestJS
import { Injectable } from '@nestjs/common';

export class MyService {
  getData() {
    return 'data';
  }
}

import { Controller, Get } from '@nestjs/common';

@Controller()
export class MyController {
  constructor(private readonly myService: MyService) {}

  @Get()
  get() {
    return this.myService.getData();
  }
}
ABecause the service method getData() is not marked as public.
BBecause the controller is missing the @Injectable() decorator.
CBecause the service is not imported into the controller file.
DBecause MyService is missing the @Injectable() decorator, so NestJS does not recognize it as a provider.
Attempts:
2 left
💡 Hint

Think about what NestJS requires to manage a class as a provider.

state_output
advanced
2:30remaining
What is the output when injecting a transient scoped service multiple times?

Consider this NestJS service with transient scope injected twice into a controller. What will be the output of the following code?

NestJS
import { Injectable, Scope } from '@nestjs/common';

@Injectable({ scope: Scope.TRANSIENT })
export class TransientService {
  id = Math.random();
}

import { Controller, Get } from '@nestjs/common';

@Controller()
export class TestController {
  constructor(
    private readonly service1: TransientService,
    private readonly service2: TransientService
  ) {}

  @Get()
  getIds() {
    return { id1: this.service1.id, id2: this.service2.id };
  }
}
ABoth id1 and id2 will be the same random number because the service is singleton by default.
Bid1 and id2 will be different random numbers because transient scope creates a new instance each injection.
CThe code will throw an error because transient scope services cannot be injected twice.
DBoth id1 and id2 will be undefined because the id property is not initialized.
Attempts:
2 left
💡 Hint

Recall what transient scope means for service instances.

🧠 Conceptual
expert
3:00remaining
Which option best describes the role of @Injectable() in NestJS's dependency injection system?

Choose the most accurate description of what the @Injectable() decorator does in NestJS.

AIt registers the class as a provider in the NestJS dependency injection container, enabling automatic instance creation and injection where needed.
BIt automatically binds the class to HTTP routes based on method names.
CIt disables the class from being injected anywhere to prevent circular dependencies.
DIt converts the class into a global singleton accessible without importing.
Attempts:
2 left
💡 Hint

Focus on how NestJS manages class instances and sharing.