[{"id":"nestjsadvanced-001","courseId":"nestjs-advanced","domain":"modules-and-di","topic":"Lifecycle hook order","prompt":"AppModule imports FeatureModule, which imports DataModule. Each defines onModuleInit(). When the application starts, which module's onModuleInit() runs last?","hint":"Think about how far each module sits from the top of the import chain.","explain":"Module hooks are not fired as one flat list. Nest walks the import graph and finishes each module before starting the next, so the order depends on how far each module sits from the top of that graph.","source":"https://docs.nestjs.com/fundamentals/lifecycle-events.md","interaction":{"type":"single"},"options":[{"id":"a","text":"DataModule","whyNot":"It is the most deeply imported module, so it is handled first and its hook runs before the others, not last."},{"id":"b","text":"FeatureModule","whyNot":"It sits between the root and the deepest module, so its hook runs in the middle of the sequence."},{"id":"c","text":"AppModule","why":"The root module goes last, after every module it imports has been initialized."},{"id":"d","text":"All three run at the same time, so none is last","whyNot":"Nest handles each module in turn and awaits its hooks before it moves on to the next one."}],"answer":"c","explanation":"Nest runs onModuleInit() and onApplicationBootstrap() module by module, starting with the most deeply imported modules and ending with the root module, awaiting each module before moving on.","concept":"Nest runs onModuleInit() and onApplicationBootstrap() module by module, starting with the most deeply imported modules and ending with the root module, awaiting each module before moving on.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["Hook order follows distance from the root module in the import graph, not the order of declaration."]},{"id":"nestjsadvanced-002","courseId":"nestjs-advanced","domain":"modules-and-di","topic":"enableShutdownHooks","prompt":"The app calls enableShutdownHooks() and then receives SIGTERM. Put these teardown steps in the order Nest runs them.","hint":"Each stage waits for the previous one, and the last one runs only after the rest finish.","explain":"Teardown runs in stages that depend on each other: a stage starts only after the one before it settles, and a returned promise holds up everything after it. Work out which stage must come last.","source":"https://docs.nestjs.com/fundamentals/lifecycle-events.md","interaction":{"type":"sort","items":[{"text":"onModuleDestroy()","why":"Runs first, right after the termination signal arrives. It takes no arguments."},{"text":"beforeApplicationShutdown(signal)","why":"Starts only after every onModuleDestroy() handler has resolved or rejected."},{"text":"Existing connections close","why":"Once beforeApplicationShutdown() completes, app.close() closes all existing connections."},{"text":"onApplicationShutdown(signal)","why":"It is the last step of app.close(), called after the connections have closed."}]},"options":[],"answer":"o:abcd","explanation":"After a termination signal, Nest calls onModuleDestroy(), then beforeApplicationShutdown() once every destroy handler settles, then closes existing connections, and last calls onApplicationShutdown(). Shutdown hooks need enableShutdownHooks() unless app.close() is called explicitly.","concept":"After a termination signal, Nest calls onModuleDestroy(), then beforeApplicationShutdown() once every destroy handler settles, then closes existing connections, and last calls onApplicationShutdown(). Shutdown hooks need enableShutdownHooks() unless app.close() is called explicitly.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["Shutdown hooks run only if enableShutdownHooks() is called, or when app.close() is invoked explicitly."]},{"id":"nestjsadvanced-003","courseId":"nestjs-advanced","domain":"modules-and-di","topic":"forwardRef for circular dependencies","prompt":"CatsService and CommonService each inject the other through their constructors, and Nest cannot build them as written. What does wrapping an injection in forwardRef() change?","hint":"Think about what Nest must know about a constructor's parameter at the moment it reads it.","explain":"Nest needs each provider's details before it can build one, and a provider still waiting to be loaded can't supply them. Two providers that each need the other form a loop with no natural first step.","source":"https://docs.nestjs.com/fundamentals/circular-dependency.md","interaction":{"type":"single"},"options":[{"id":"a","text":"It makes both providers share one instance, so neither receives an empty value.","whyNot":"Sharing an instance is a scope choice; forwardRef() does not make the two providers share anything."},{"id":"b","text":"It lets Nest reference a class that is not defined yet, so the metadata is available.","why":"The wrapper lets Nest reference a class that does not exist yet when it reads the constructor, so the essential metadata is there."},{"id":"c","text":"It postpones building the providers until the first request arrives.","whyNot":"Circular providers are built at startup like any others; forwardRef() changes how a reference is read, not when building happens."},{"id":"d","text":"It marks the injected dependency as optional, so Nest passes undefined instead of failing.","whyNot":"forwardRef() is about reading the class reference; it does not make the injection optional."}],"answer":"b","explanation":"forwardRef() lets Nest reference a provider class that is not defined yet, so the metadata needed to instantiate circular providers is available. The order of instantiation is then indeterminate.","concept":"forwardRef() lets Nest reference a provider class that is not defined yet, so the metadata needed to instantiate circular providers is available. The order of instantiation is then indeterminate.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["forwardRef() fixes resolution, not design: circular providers still lose a guaranteed construction order."]},{"id":"nestjsadvanced-004","courseId":"nestjs-advanced","domain":"modules-and-di","topic":"Barrel files and circular imports","prompt":"A team puts every module and service behind index.ts barrel files. Which TWO of these statements match Nest's guidance on barrel files and circular dependencies?","hint":"Ask which import paths can loop back to the file doing the importing.","explain":"A file that re-exports several others gives them one shared import path. When files reach each other through that shared path, the import graph can loop, and a loop breaks construction.","source":"https://docs.nestjs.com/fundamentals/circular-dependency.md","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"Avoid barrel files for module and provider classes.","why":"Nest warns that barrel files can cause circular dependencies, so module and provider classes should be imported directly."},{"id":"b","text":"A cats/cats.controller file should not import cats.service through cats/index.ts, the barrel in its own directory.","why":"Importing a sibling file through the barrel in the same directory is the pattern Nest says to avoid."},{"id":"c","text":"Barrel files are recommended for module classes, since they shorten import paths.","whyNot":"Nest advises against barrel files for module classes, and shorter paths are not its reason for anything."},{"id":"d","text":"A barrel file can't cause a circular dependency, because it only re-exports names.","whyNot":"Nest states that circular dependencies can also be caused by using barrel files."},{"id":"e","text":"Barrel files are fine for provider classes; they only matter for module classes.","whyNot":"Nest says to avoid barrel files for provider classes as well as module classes."},{"id":"f","text":"Adding a barrel file for each folder is the recommended way to keep circular dependencies out.","whyNot":"Barrel files are the source of the problem Nest describes, so grouping every folder behind one is the opposite of its advice."}],"answer":"ab","explanation":"Nest says barrel files can cause circular dependencies, so module and provider classes should avoid them, and a file should not import its sibling through the barrel in its own directory.","concept":"Nest says barrel files can cause circular dependencies, so module and provider classes should avoid them, and a file should not import its sibling through the barrel in its own directory.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["Import module and provider classes directly by path so that no barrel file can close a loop."]},{"id":"nestjsadvanced-005","courseId":"nestjs-advanced","domain":"modules-and-di","topic":"ModuleRef get versus resolve","prompt":"A service calls moduleRef.resolve(TransientService) twice and needs both results to be the same instance. Which change achieves that?","hint":"Each call without shared context gets its own sub-tree; what could make two calls share one?","explain":"A scoped provider is resolved from a DI sub-tree, and each sub-tree has its own unique context identifier. An instance belongs to the sub-tree that produced it.","source":"https://docs.nestjs.com/fundamentals/module-ref.md","interaction":{"type":"single"},"options":[{"id":"a","text":"Pass one ContextIdFactory.create() identifier to both resolve() calls.","why":"Calls that share a context identifier are served from the same DI sub-tree, so they receive one instance."},{"id":"b","text":"Wrap both resolve() calls in Promise.all.","whyNot":"Promise.all only waits for both calls; each still gets its own sub-tree, so the two references differ."},{"id":"c","text":"Replace both calls with moduleRef.get(TransientService).","whyNot":"get() cannot retrieve transient providers, so this change does not work."},{"id":"d","text":"Call ContextIdFactory.create() separately inside each resolve() call.","whyNot":"Each new identifier opens a new sub-tree, which is exactly what keeps the instances apart."}],"answer":"a","explanation":"resolve() returns a unique instance per call from its own DI sub-tree. Passing the same context identifier from ContextIdFactory.create() to each call makes them share one sub-tree and one instance.","concept":"resolve() returns a unique instance per call from its own DI sub-tree. Passing the same context identifier from ContextIdFactory.create() to each call makes them share one sub-tree and one instance.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["get() cannot return transient or request-scoped providers; use resolve() with a shared context identifier."]}]