Challenge - 5 Problems
NestJS Lifecycle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ lifecycle
intermediate2:00remaining
What is the output when using OnModuleInit in NestJS?
Consider this NestJS service implementing OnModuleInit. What will be logged when the module initializes?
NestJS
import { Injectable, OnModuleInit } from '@nestjs/common'; @Injectable() export class MyService implements OnModuleInit { onModuleInit() { console.log('Module initialized'); } }
Attempts:
2 left
💡 Hint
OnModuleInit runs once when the module is ready.
✗ Incorrect
The onModuleInit method runs once automatically when the module finishes loading. It is used for setup tasks.
❓ component_behavior
intermediate2:00remaining
What happens if you implement OnApplicationShutdown in a provider?
Given this provider implementing OnApplicationShutdown, what will happen when the app shuts down?
NestJS
import { Injectable, OnApplicationShutdown } from '@nestjs/common'; @Injectable() export class ShutdownService implements OnApplicationShutdown { onApplicationShutdown(signal?: string) { console.log(`App shutting down due to: ${signal}`); } }
Attempts:
2 left
💡 Hint
Shutdown hooks run when the app closes.
✗ Incorrect
OnApplicationShutdown runs once when the app is closing. The optional signal shows the reason.
🔧 Debug
advanced2:00remaining
Why does this NestJS lifecycle hook not run?
This service implements OnModuleDestroy but the method never runs. Why?
NestJS
import { Injectable, OnModuleDestroy } from '@nestjs/common'; @Injectable() export class MyService implements OnModuleDestroy { onModuleDestroy() { console.log('Module is being destroyed'); } } // The app never calls app.close()
Attempts:
2 left
💡 Hint
Lifecycle destroy hooks run only when app closes.
✗ Incorrect
OnModuleDestroy runs only when the app is closed via app.close(). Without calling close, it won't run.
📝 Syntax
advanced2:00remaining
Which option correctly implements OnApplicationBootstrap?
Choose the correct syntax for implementing OnApplicationBootstrap in a NestJS service.
Attempts:
2 left
💡 Hint
Method signature must be valid TypeScript.
✗ Incorrect
The correct method signature includes the return type void and no arrow function syntax.
🧠 Conceptual
expert3:00remaining
What is the order of NestJS lifecycle hooks during app startup and shutdown?
Arrange the lifecycle hooks in the order they are called during app startup and shutdown.
Attempts:
2 left
💡 Hint
Module init happens before app bootstrap; shutdown hooks run before destroy hooks.
✗ Incorrect
During startup, onModuleInit runs first, then onApplicationBootstrap. During shutdown, onApplicationShutdown runs before onModuleDestroy.