NestJS Interview Questions
Q: What is NestJS and what problem does it solve?
NestJS is a Node.js framework for building scalable server-side applications. It solves the lack of structure in vanilla Express by providing Angular-inspired architecture: modules, controllers, services, DI, and decorators. It's opinionated about organization but flexible in underlying transport (HTTP, WebSockets, microservices).
Q: What is Dependency Injection in NestJS?
NestJS has a built-in IoC container. Classes decorated with @Injectable() are managed by the container. When you inject a service via the constructor, NestJS instantiates and provides it automatically. This decouples implementations from consumers, making testing easy (inject mocks) and sharing services across modules simple.
Q: What is the request lifecycle in NestJS?
Incoming request → Middleware → Guards → Interceptors (pre) → Pipes → Route handler → Interceptors (post) → Exception filters (on error) → Response.
Q: What is the difference between a Guard and Middleware?
Middleware runs before routing and has no knowledge of which handler will be executed. Guards have access to the ExecutionContext (route handler metadata, decorators, class) — making them suitable for authorization logic that depends on what route is being accessed. Guards return true/false to allow/deny; middleware just calls next().
Q: What is an Interceptor and when would you use one?
Interceptors wrap the route handler execution using RxJS Observables. They can run code before and after the handler, transform the response, extend behavior, or handle errors. Common uses: response transformation (wrap in { data: ... }), logging request/response times, caching, serialization.
Q: How do you handle validation in NestJS?
Using the ValidationPipe (global or per-route) combined with class-validator decorators on DTOs. When whitelist: true is set, unknown properties are stripped. transform: true auto-converts plain objects to DTO class instances. For custom validation, implement ValidatorConstraint.
Q: What is the difference between forRoot() and forFeature() in TypeORM/NestJS?
forRoot() is called once in AppModule to configure the database connection (global). forFeature([Entity]) is called in each feature module to register specific entities and make their repositories injectable in that module's scope.
Q: How do you test NestJS services?
import { Test, TestingModule } from '@nestjs/testing';
describe('UsersService', () => {
let service: UsersService;
const mockRepo = {
findOneBy: jest.fn(),
save: jest.fn(),
};
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
UsersService,
{ provide: getRepositoryToken(User), useValue: mockRepo },
],
}).compile();
service = module.get<UsersService>(UsersService);
});
it('should throw NotFoundException if user not found', async () => {
mockRepo.findOneBy.mockResolvedValue(null);
await expect(service.findOne('bad-id')).rejects.toThrow(NotFoundException);
});
});Keep your own version of these notes — editable, searchable, and organised by your stack.
Start free