All quizzesMedium
Modules & Sharing — Series 2
Preview — 3 of 10 questions
CacheService holds an in-memory Map. What do CatsController and DogsController actually share?
javascript
@Module({ providers: [CacheService], controllers: [CatsController] })
export class CatsModule {}
@Module({ providers: [CacheService], controllers: [DogsController] })
export class DogsModule {}ANothing changes — Nest deduplicates identical provider classes into one application-wide instance
BThe second registration is rejected at startup with a duplicate-provider error
COnly the first module to initialise gets a working instance; the second receives undefined
DNothing — each module gets its own CacheService instance, so the two controllers read and write completely separate Maps
Can a provider inside CatsModule inject LoggerService?
javascript
@Module({ providers: [LoggerService], exports: [LoggerService] })
export class LoggerModule {}
@Module({ imports: [LoggerModule] })
export class CoreModule {}
@Module({ imports: [CoreModule] })
export class CatsModule {} // injects LoggerService → ?AYes — LoggerService is exported, and exports propagate down the entire import chain automatically
BNo — imports are not transitive. CoreModule would have to add LoggerModule to its own exports (re-exporting it) for CatsModule to see it
CYes, but only if LoggerService is also declared @Global()
DNo, and the only possible fix is to move LoggerService into CatsModule's own providers
How many ConfigService instances exist at runtime?
javascript
@Module({ providers: [ConfigService], exports: [ConfigService] })
export class ConfigModule {}
@Module({ imports: [ConfigModule] })
export class CatsModule {}
@Module({ imports: [ConfigModule] })
export class DogsModule {}AOne — Nest caches each module by its class reference, so both importers resolve to the same module instance and therefore the same provider instance
BTwo — one per importing module, mirroring how providers are scoped
CThree — one per importing module plus one for the module itself
DIt depends on the order the modules appear in the root module's imports array
Sign up free to play
Answer all 10 questions (7 more), see explanations for every answer, and track your score.