0
0
NestJSframework~20 mins

Feature modules in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NestJS Feature Modules Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when importing a feature module with providers?

Consider a NestJS feature module that provides a service. What happens when this module is imported into the root module?

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

@Injectable()
export class GreetingService {
  getGreeting() {
    return 'Hello from Feature Module!';
  }
}

@Module({
  providers: [GreetingService],
  exports: [GreetingService],
})
export class FeatureModule {}

@Module({
  imports: [FeatureModule],
})
export class AppModule {}

// In a controller inside AppModule
constructor(private greetingService: GreetingService) {}

getHello() {
  return this.greetingService.getGreeting();
}
AThe GreetingService is not available in AppModule and will cause a runtime error.
BThe GreetingService is only available inside FeatureModule and cannot be injected elsewhere.
CThe FeatureModule must be declared in controllers to use GreetingService.
DThe AppModule can use GreetingService and get 'Hello from Feature Module!' as output.
Attempts:
2 left
💡 Hint

Think about how providers are shared when exported from a module.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a feature module with a controller and service?

Identify the correct NestJS feature module syntax that includes a controller and a service.

A
import { Module } from '@nestjs/common';
import { MyController } from './my.controller';
import { MyService } from './my.service';

@Module({
  controllers: (MyController),
  providers: (MyService),
})
export class MyFeatureModule {}
B
import { Module } from '@nestjs/common';
import { MyController } from './my.controller';
import { MyService } from './my.service';

@Module({
  controllers: MyController,
  providers: MyService,
})
export class MyFeatureModule {}
C
import { Module } from '@nestjs/common';
import { MyController } from './my.controller';
import { MyService } from './my.service';

@Module({
  controllers: [MyController],
  providers: [MyService],
})
export class MyFeatureModule {}
D
import { Module } from '@nestjs/common';
import { MyController } from './my.controller';
import { MyService } from './my.service';

@Module({
  controller: [MyController],
  provider: [MyService],
})
export class MyFeatureModule {}
Attempts:
2 left
💡 Hint

Check the property names and array syntax in the @Module decorator.

🔧 Debug
advanced
2:00remaining
Why does injecting a service from a feature module cause a runtime error?

Given a feature module that provides a service but the service injection fails with 'Nest can't resolve dependencies' error, what is the likely cause?

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

@Injectable()
export class DataService {
  getData() {
    return 'data';
  }
}

@Module({
  providers: [DataService],
  // exports: [DataService], // This line is missing
})
export class DataModule {}

@Module({
  imports: [DataModule],
})
export class AppModule {}

// In a controller inside AppModule
constructor(private dataService: DataService) {}
ADataService must be declared in AppModule providers to be injectable.
BDataService is not exported from DataModule, so it is not available for injection in AppModule.
CDataService should be marked as @Global() to be injectable.
DDataService requires a factory provider to be injectable.
Attempts:
2 left
💡 Hint

Check if the service is exported from the module that provides it.

state_output
advanced
2:00remaining
What is the value of a service property after importing multiple feature modules?

Consider two feature modules each providing a service with a counter property initialized to 0. If both modules are imported into AppModule and the services are injected separately, what are the counter values after incrementing each once?

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

@Injectable()
export class CounterService {
  counter = 0;
  increment() {
    this.counter++;
  }
}

@Module({
  providers: [CounterService],
  exports: [CounterService],
})
export class ModuleA {}

@Module({
  providers: [CounterService],
  exports: [CounterService],
})
export class ModuleB {}

@Module({
  imports: [ModuleA, ModuleB],
})
export class AppModule {}

// In a controller
constructor(
  private counterA: CounterService,
  private counterB: CounterService
) {}

someMethod() {
  this.counterA.increment();
  this.counterB.increment();
  return { a: this.counterA.counter, b: this.counterB.counter };
}
A{ a: 1, b: 1 } because each module provides its own instance of CounterService.
B{ a: 2, b: 2 } because CounterService is singleton across modules.
C{ a: 1, b: 0 } because only counterA increments.
D{ a: 0, b: 0 } because services are not instantiated.
Attempts:
2 left
💡 Hint

Think about how NestJS creates service instances per module.

🧠 Conceptual
expert
2:00remaining
Which statement best describes the role of feature modules in NestJS?

Choose the most accurate description of feature modules in NestJS.

AFeature modules organize related components and services, encapsulating functionality to promote modularity and reusability.
BFeature modules are only used to lazy-load parts of the application and cannot export providers.
CFeature modules replace the root module and must contain all controllers and services.
DFeature modules automatically share all their providers globally without explicit exports.
Attempts:
2 left
💡 Hint

Consider how NestJS encourages modular design.