Interview Questions
A focused set of NestJS interview questions with concise, senior-level answers. Use them to check your depth across the framework’s core pillars, from the request lifecycle to testing strategy.
Core Concepts
What is NestJS and what problem does it solve? A progressive Node.js framework that adds an opinionated, Angular-inspired architecture (modules, controllers, providers, DI) on top of an HTTP platform like Express or Fastify. It brings structure, testability, and convention to otherwise unopinionated Node development.
Which platforms can NestJS run on?
By default it uses Express. You can switch to Fastify with NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter()) for higher throughput, without changing application code, because Nest abstracts the HTTP layer.
Describe the NestJS request lifecycle. Middleware → guards → interceptors (pre) → pipes → route handler → interceptors (post) → exception filters. If a guard rejects the request, downstream stages never run.
What is the role of a controller versus a provider? A controller handles HTTP routing and shaping requests/responses; it should be thin. A provider (service, repository) holds business logic and is injected into controllers. This separation keeps logic testable in isolation.
Dependency Injection
How does dependency injection work in NestJS?
Nest has an IoC container that builds a dependency graph at startup. Classes marked @Injectable() and registered in a module’s providers are resolved by type/token and injected via the constructor. You never instantiate them manually.
What is a provider token?
The key Nest uses to look up a provider. It is usually the class itself, but can be a string or Symbol for non-class providers. Non-class tokens are injected with @Inject('TOKEN').
Explain the custom provider types.
useClass binds a token to a class; useValue injects a constant or mock; useFactory computes a provider at runtime (may be async and have its own inject deps); useExisting aliases an existing provider.
What are provider scopes and when would you change them?
DEFAULT (singleton, shared), REQUEST (per-request instance), and TRANSIENT (new instance per consumer). Singletons are the default for performance; use request scope only for per-request state, knowing it forces dependents to become request-scoped too.
Modules
What metadata does the @Module decorator accept?
controllers, providers, imports, and exports. Providers are private to their module unless exported.
How do you share a provider between modules?
Add it to the declaring module’s exports, then imports that module wherever needed. Importers receive the same cached singleton instance.
What is a dynamic module and why use it?
A module configured at runtime via a static method (commonly forRoot/forFeature) returning a DynamicModule. It lets consumers pass options, e.g. a connection string, while the module decides its providers.
What does the @Global() decorator do, and when should you avoid it? It makes a module’s exports available app-wide without explicit imports. Avoid overusing it, it hides dependencies and makes the graph harder to reason about. Prefer explicit imports.
Pipes, Guards & Interceptors
What is a pipe?
A class that transforms or validates a handler’s arguments before execution. ValidationPipe plus class-validator DTOs is the canonical use; built-ins like ParseIntPipe coerce types.
What is a guard and how does it differ from middleware?
A guard returns a boolean (or throws) to decide whether a request may proceed, ideal for auth. Unlike middleware, guards have access to the ExecutionContext, so they know which handler/class is the target and can read metadata set by decorators like @Roles().
What is an interceptor?
A class that wraps handler execution using RxJS, allowing logic before and after the handler: logging, response transformation, caching, and timeouts. It can map or extend the returned Observable.
In what order do guards, interceptors, and pipes run? Guards first, then interceptors (pre-handler), then pipes, then the handler, then interceptors (post-handler). Exception filters catch anything thrown along the way.
Testing
How do you unit test a NestJS provider?
Use the @nestjs/testing Test.createTestingModule() to build an isolated module, providing mocks via useValue for dependencies, then compile() and resolve the unit under test with module.get().
What is the difference between unit and e2e tests in Nest?
Unit tests instantiate a provider/controller with mocked dependencies in a testing module. E2E tests bootstrap the full application (often via supertest) and exercise real HTTP routes end to end, validating wiring, pipes, guards, and serialization together.