NestJS Professional Standards & Competencies 3 — Questions and Answers
Question 1: What is the professional standard for logging in production NestJS applications?
- Use console.log throughout the codebase
- Inject the built-in Logger or a structured logger like Winston with appropriate log levels (Correct answer)
- Disable all logging to improve performance
- Log only in the main bootstrap file
Correct answer: Inject the built-in Logger or a structured logger like Winston with appropriate log levels
Structured logging with configurable log levels enables observability without flooding production logs with debug noise.
Question 2: Which NestJS feature should be used to document and enforce API contracts for REST APIs consumed by external teams?
- README files
- @nestjs/swagger with OpenAPI decorators (Correct answer)
- Inline code comments in controllers
- Postman collections checked into git
Correct answer: @nestjs/swagger with OpenAPI decorators
@nestjs/swagger generates live OpenAPI specs from decorators, keeping documentation synchronized with the actual implementation.
Question 3: A NestJS microservice must guarantee message delivery even if the consumer is temporarily unavailable. What pattern should be applied?
- Retry the HTTP call three times
- Use a message broker with durable queues such as RabbitMQ or Kafka (Correct answer)
- Store messages in a local SQLite file
- Use WebSockets for real-time delivery
Correct answer: Use a message broker with durable queues such as RabbitMQ or Kafka
Durable message queues persist messages until they are successfully consumed, providing at-least-once delivery guarantees.
Question 4: When writing unit tests for a NestJS service that depends on TypeORM repositories, what is the recommended approach?
- Connect to a real test database for every unit test
- Use getRepositoryToken() with a mock repository object in the Test module (Correct answer)
- Skip testing service logic that touches the database
- Use end-to-end tests exclusively
Correct answer: Use getRepositoryToken() with a mock repository object in the Test module
Providing mock repositories via getRepositoryToken() isolates the service under test without a real database connection.
Question 5: What does applying the Single Responsibility Principle look like in a NestJS codebase?
- One NestJS module handles all business logic, database access, and HTTP routing
- Controllers handle HTTP concerns, services contain business logic, and repositories manage data access (Correct answer)
- Services call other services recursively to reuse code
- Providers are merged into one file to minimize imports
Correct answer: Controllers handle HTTP concerns, services contain business logic, and repositories manage data access
Separating HTTP, business, and data-access concerns into distinct layers keeps each class focused and independently testable.
Question 6: Which strategy should a NestJS team adopt to prevent breaking changes when updating shared library packages?
- Always use the latest version of every package immediately
- Pin exact versions, use a lockfile, and test upgrades in a staging environment (Correct answer)
- Avoid using third-party packages entirely
- Update packages only when bugs occur
Correct answer: Pin exact versions, use a lockfile, and test upgrades in a staging environment
Pinning versions and using a lockfile ensures reproducible builds and controlled upgrades with validation before production.
Question 7: In NestJS, what is the professional purpose of the `@UseGuards()` decorator applied at the controller level versus the route level?
- Controller-level guards are faster than route-level guards
- Controller-level guards apply to all routes in the controller, while route-level guards apply only to a specific handler (Correct answer)
- Route-level guards override global guards; controller-level ones do not
- They are functionally identical and the choice is cosmetic
Correct answer: Controller-level guards apply to all routes in the controller, while route-level guards apply only to a specific handler
Controller-level @UseGuards() protects the entire controller, reducing repetition, while route-level placement allows per-endpoint overrides.
What is the professional standard for logging in production NestJS applications?