0
0
NestJSframework~20 mins

Constructor injection in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Constructor Injection Mastery
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 using constructor injection?

Consider this NestJS service that injects a repository via constructor injection. What will be logged when getData() is called?

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

@Injectable()
export class DataService {
  constructor(private readonly repo: { find: () => string }) {}

  getData() {
    return this.repo.find();
  }
}

const fakeRepo = { find: () => 'data found' };
const service = new DataService(fakeRepo);
console.log(service.getData());
ASyntaxError
B"undefined"
C"data found"
DTypeError: this.repo.find is not a function
Attempts:
2 left
💡 Hint

Constructor injection assigns the passed object to the private readonly property repo.

📝 Syntax
intermediate
2:00remaining
Which option correctly injects a service using NestJS constructor injection?

Choose the correct syntax for injecting MyService into AppService using constructor injection.

Aconstructor(private readonly myService: MyService) {}
Bconstructor(private myService: MyService) {}
Cconstructor(myService: MyService) { this.myService = myService; }
Dconstructor(public myService: MyService) {}
Attempts:
2 left
💡 Hint

Use private readonly to declare and assign the injected service in one step.

🔧 Debug
advanced
2:00remaining
Why does this NestJS service throw an error when using constructor injection?

Given this service, why does NestJS throw a runtime error about missing dependencies?

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

@Injectable()
export class UserService {
  constructor(private readonly repo) {}

  getUser() {
    return this.repo.findUser();
  }
}
AThe constructor parameter lacks a type, so NestJS cannot resolve the dependency.
BThe repo property should be public, not private readonly.
CThe @Injectable decorator is missing from the repo class.
DThe getUser method must be async to use the repo.
Attempts:
2 left
💡 Hint

Check if NestJS can identify the dependency type from the constructor.

state_output
advanced
2:00remaining
What is the value of the injected service property after instantiation?

In this NestJS controller, what will be the value of this.authService.isLoggedIn after the constructor runs?

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

class AuthService {
  isLoggedIn = false;
}

@Controller()
export class AuthController {
  constructor(private readonly authService: AuthService) {
    this.authService.isLoggedIn = true;
  }
}
Aundefined
Bfalse
CThrows ReferenceError
Dtrue
Attempts:
2 left
💡 Hint

The constructor sets isLoggedIn to true explicitly.

🧠 Conceptual
expert
2:00remaining
Which statement best describes constructor injection in NestJS?

Choose the most accurate description of how constructor injection works in NestJS.

AConstructor injection requires manually creating instances and passing them to the constructor when creating a class.
BNestJS uses the constructor parameter types to automatically resolve and inject dependencies registered in its module system.
CDependencies must be injected via setter methods after the class is instantiated.
DNestJS only supports property injection, not constructor injection.
Attempts:
2 left
💡 Hint

Think about how NestJS uses TypeScript metadata to inject services.