NestJS Developer Skills Assessment — Questions and Answers
Question 1: How do you inject a non-class provider (e.g., a string token) in NestJS?
- @Value(TOKEN) decorator
- @Inject(TOKEN) in the constructor parameter (Correct answer)
- Automatic injection by type
- @InjectToken(TOKEN) decorator
Correct answer: @Inject(TOKEN) in the constructor parameter
@Inject(TOKEN) explicitly specifies the injection token to use when NestJS cannot infer it from the TypeScript type.
Question 2: Which provider option allows asynchronous initialization, such as reading from a database?
- useValue
- useExisting
- useClass
- useFactory with async function (Correct answer)
Correct answer: useFactory with async function
`useFactory` accepts an async function, so NestJS will await the resolved value before making the provider available for injection.
Question 3: In NestJS, what is an injection token used for?
- Uniquely identifying a provider in the DI container (Correct answer)
- Authenticating HTTP requests
- Namespacing module routes
- Labeling log messages
Correct answer: Uniquely identifying a provider in the DI container
An injection token (string, Symbol, or class) uniquely identifies a provider in NestJS's IoC container so the correct value is injected.
Question 4: In NestJS testing, what is the recommended way to test an `ExceptionFilter` that transforms `HttpException` into a custom response?
- Both B and C are valid approaches depending on whether you want unit or e2e coverage (Correct answer)
- Import the filter in a test module and call the route that throws
- Instantiate the filter and call `catch(exception, host)` with mock arguments
- Use `app.useGlobalFilters()` in an e2e test and assert on the response body
Correct answer: Both B and C are valid approaches depending on whether you want unit or e2e coverage
Exception filters can be unit tested by calling `catch()` directly with mock `ArgumentsHost`, or e2e tested by checking the full HTTP response — both approaches provide complementary coverage.
Question 5: How do you create a circular dependency between two NestJS providers without causing errors?
- Circular dependencies are impossible in NestJS
- Use @Inject() with lazy references
- Use forwardRef() in both @Inject() decorators (Correct answer)
- Use a shared Global module
Correct answer: Use forwardRef() in both @Inject() decorators
forwardRef() wraps the class reference to defer its resolution, allowing NestJS to break the circular dependency loop during instantiation.
Question 6: Which RxJS operator is commonly used in NestJS interceptors to transform the response?
- filter
- switchMap
- mergeMap
- map (Correct answer)
Correct answer: map
The map operator from RxJS is commonly used in interceptors to transform the response stream returned by the route handler.
Question 7: When using NestJS with TypeORM, what decorator defines a class as a database entity?
- @Schema()
- @Entity() (Correct answer)
- @Table()
- @Model()
Correct answer: @Entity()
@Entity() from TypeORM marks a class as a database entity, mapping it to a corresponding database table.
Question 8: How do you apply a pipe globally to all routes in NestJS?
- Register it in app.module.ts imports
- Apply @UsePipes() to every controller
- Use app.useGlobalPipes() in main.ts (Correct answer)
- Add it to the module providers array
Correct answer: Use app.useGlobalPipes() in main.ts
Calling app.useGlobalPipes() in main.ts registers a pipe that runs for every incoming request across all routes.
Question 9: In NestJS, what is the purpose of the ConfigModule?
- To load and manage environment variables and configuration across the application (Correct answer)
- To configure Swagger documentation
- To configure routing rules
- To set up database migrations
Correct answer: To load and manage environment variables and configuration across the application
ConfigModule (from @nestjs/config) loads .env files and provides a ConfigService to access environment variables throughout the app.
Question 10: Which class do NestJS guards need to implement?
- GuardInterface
- CanActivate (Correct answer)
- RequestFilter
- HttpGuard
Correct answer: CanActivate
Guards must implement the CanActivate interface, which requires a canActivate() method that returns a boolean or Observable<boolean>.
Question 11: What is the N+1 query problem in TypeORM, and how is it solved?
- Having N connections open — solved with connection pooling
- Calling findOne() N+1 times — solved with caching
- Running N queries then 1 aggregate — solved with GROUP BY
- Loading N related entities with N separate queries instead of 1 JOIN — solved with eager loading or QueryBuilder joins (Correct answer)
Correct answer: Loading N related entities with N separate queries instead of 1 JOIN — solved with eager loading or QueryBuilder joins
N+1 occurs when fetching a list of N entities and then loading each one's relation separately; use eager relations or explicit JOIN in QueryBuilder to fetch everything in one query.
Question 12: How do you inject a TypeORM repository into a NestJS service?
- @Inject(Repository<User>) private repo: Repository<User>
- @Repository(User) private repo
- @TypeOrm(User) private repo: Repository<User>
- @InjectRepository(User) private repo: Repository<User> (Correct answer)
Correct answer: @InjectRepository(User) private repo: Repository<User>
@InjectRepository(Entity) is the TypeORM-specific injection decorator that retrieves the entity's repository from the DI container.
Question 13: What is the purpose of the `validate()` method in a Passport strategy class?
- It runs input sanitization on query parameters
- It receives the decoded token payload and returns the user object to attach to the request (Correct answer)
- It validates the shape of the request body
- It validates environment variables on startup
Correct answer: It receives the decoded token payload and returns the user object to attach to the request
validate() is called after the token is verified; its return value is attached to req.user and made available to route handlers.
Question 14: What is the purpose of the `mockReturnValue()` method on a Jest mock function in NestJS tests?
- It asserts the mock was called with the specified value
- It resets the mock's call history and sets a new implementation
- It runs the real implementation once and then returns undefined
- It sets the value the mock function will return on every subsequent call (Correct answer)
Correct answer: It sets the value the mock function will return on every subsequent call
`mockReturnValue(val)` configures a `jest.fn()` mock to return `val` synchronously on every call, allowing you to control dependent service behavior in tests.
Question 15: How do you configure TypeORM for asynchronous configuration (e.g., from ConfigService) in NestJS?
- TypeOrmModule.forRootAsync() with useFactory (Correct answer)
- TypeOrmModule.configure() with async option
- TypeOrmModule.forRoot() with a factory
- DatabaseModule.withConfig() factory
Correct answer: TypeOrmModule.forRootAsync() with useFactory
TypeOrmModule.forRootAsync() accepts a useFactory (or useClass/useExisting) that can inject ConfigService and return the database options asynchronously.
Question 16: In a NestJS e2e test, how do you initialize the application before running tests?
- Call `Test.createTestingModule().compile()` without calling `init()`
- Call `NestFactory.create()` and `app.init()` inside `beforeAll()` (Correct answer)
- Use `jest.setup()` to bootstrap the app globally
- Import the AppModule directly and call its constructor
Correct answer: Call `NestFactory.create()` and `app.init()` inside `beforeAll()`
For e2e tests you use `NestFactory.create(AppModule)` followed by `app.init()` inside `beforeAll()` so the full application is available to all tests in the suite.
Question 17: What is the purpose of the `useValue` option in a custom provider definition?
- To provide a static value or mock object as a provider (Correct answer)
- To alias an existing provider
- To inject a factory function
- To define an async provider
Correct answer: To provide a static value or mock object as a provider
`useValue` registers a static value (such as a config object or mock) directly as a provider without instantiation.
Question 18: A NestJS app must comply with GDPR's 'right to erasure'. What database strategy best supports this in a Drizzle ORM setup?
- Hard deletes with cascading foreign keys
- Archiving rows to a cold storage table
- Soft deletes with a deleted_at column
- Encrypting PII columns with a per-user key then discarding the key (Correct answer)
Correct answer: Encrypting PII columns with a per-user key then discarding the key
Discarding a per-user encryption key renders PII unreadable (cryptographic erasure), satisfying right-to-erasure even when hard deletes are impractical.
Question 19: In NestJS, what is the difference between send() and emit() on a ClientProxy?
- There is no functional difference
- send() is synchronous; emit() is asynchronous
- send() is for HTTP; emit() is for WebSocket
- send() expects a response (request-response); emit() is fire-and-forget (event-based) (Correct answer)
Correct answer: send() expects a response (request-response); emit() is fire-and-forget (event-based)
send() implements the request-response pattern and returns an Observable with the response, while emit() publishes an event without waiting for a response.
Question 20: What does the `exports` array in @Module() control?
- Which providers can be used by other modules (Correct answer)
- The order of middleware execution
- Which guards are applied globally
- Which controllers are public
Correct answer: Which providers can be used by other modules
The `exports` array specifies which providers from the current module are available for other modules that import it.
Question 21: How should an NestJS professional approach a novel situation not covered by standard procedures?
- Follow the closest standard procedure exactly
- Improvise without documentation
- Apply foundational principles, assess risks, consult resources, and document the rationale for decisions (Correct answer)
- Refuse to proceed
Correct answer: Apply foundational principles, assess risks, consult resources, and document the rationale for decisions
This is fundamental to NestJS practice. Apply foundational principles, assess risks, consult resources, and document the rationale for decisions represents the professional standard for practical in the NestJS certification framework.
Question 22: How do you inject Prisma Client as a service in NestJS?
- Prisma auto-registers with NestJS when installed
- Create a PrismaService that extends PrismaClient, mark it @Injectable(), and inject it by class type (Correct answer)
- Import PrismaModule.forRoot() and use @Prisma() decorator
- Use @InjectPrisma() decorator from @nestjs/prisma
Correct answer: Create a PrismaService that extends PrismaClient, mark it @Injectable(), and inject it by class type
The idiomatic pattern is creating a PrismaService extending PrismaClient, decorating it with @Injectable(), and providing it in AppModule so it can be injected anywhere.
Question 23: A NestJS service caches sensitive user data in Redis without an expiry. What risk does this create?
- Redis automatically evicts data using LRU, causing cache misses
- Stale sensitive data persists indefinitely, increasing exposure if the cache is breached (Correct answer)
- JWT tokens stored in Redis become invalid after a restart
- NestJS CacheModule cannot connect without a TTL
Correct answer: Stale sensitive data persists indefinitely, increasing exposure if the cache is breached
Without a TTL, sensitive records remain in the cache long after they are no longer needed, enlarging the window for unauthorized access.
Question 24: Which NestJS package provides first-class TypeORM integration?
- @nestjs/typeorm (Correct answer)
- @nestjs/entity
- @nestjs/orm
- @nestjs/database
Correct answer: @nestjs/typeorm
@nestjs/typeorm provides TypeOrmModule with forRoot() and forFeature() methods for integrating TypeORM into the NestJS DI system.
Question 25: What does `useExisting` do in a NestJS custom provider?
- Imports a provider from another module
- Creates an alias that points to an already-registered provider (Correct answer)
- Creates a new instance of an existing class
- Marks a provider as optional
Correct answer: Creates an alias that points to an already-registered provider
`useExisting` creates a provider alias so two different tokens resolve to the same underlying provider instance.
Question 26: Which NestJS decorator is used to define a class as a module?
- @Module() (Correct answer)
- @Controller()
- @Component()
- @Injectable()
Correct answer: @Module()
The @Module() decorator marks a class as a NestJS module that organizes related providers, controllers, and imports.
Question 27: What is an Interceptor in NestJS primarily used for?
- Routing requests to different controllers
- Validating request parameters
- Managing database transactions
- Binding extra logic before/after method execution and transforming results (Correct answer)
Correct answer: Binding extra logic before/after method execution and transforming results
Interceptors wrap route handler execution, allowing you to add logging, transform responses, or handle errors around the handler call.
Question 28: What does the `synchronize: true` TypeORM option do and why is it dangerous in production?
- It synchronizes database replicas
- It automatically alters the DB schema to match entities — risking data loss on production (Correct answer)
- It keeps entity files in sync with migration files
- It syncs multiple databases together
Correct answer: It automatically alters the DB schema to match entities — risking data loss on production
`synchronize: true` auto-runs ALTER TABLE statements on startup to match entity definitions, which can drop columns or tables with production data.
Question 29: What does the 'imports' array in @Module() do?
- Imports TypeScript files
- Registers middleware
- Makes exported providers from other modules available in the current module (Correct answer)
- Declares new providers
Correct answer: Makes exported providers from other modules available in the current module
The 'imports' array brings in other modules, making their exported providers available for injection.
Question 30: Which pattern does NestJS primarily use for organizing application logic?
- MVC
- Microkernel
- MVVM
- Modular architecture with DI (Correct answer)
Correct answer: Modular architecture with DI
NestJS uses a modular architecture combined with dependency injection (DI) to organize and decouple application components.
Question 31: What does a website's back end include?
- Code
- Programming
- Data (Correct answer)
- Design
Correct answer: Data
The backend of a website encompasses the server-side logic, databases, and application programming interfaces (APIs) that power the application. Its core responsibility is to store, organize, and process data, which is then delivered to the frontend for display and user interaction. Therefore, 'data' is a fundamental component of a website's backend.
NestJS Developer Skills Assessment
Evaluates proficiency in the NestJS Node.js framework, covering core architecture, dependency injection, decorators, database integration, and advanced patterns for building scalable server-side applications.
Exam Rules
- You can skip questions and return to them later
- Flag questions for review before submitting
- No feedback shown until you submit the entire exam
- Unanswered questions count as wrong — answer everything
- 10 pretest questions are mixed in and don't affect your score
- Timer auto-submits when time runs out
- Your progress is auto-saved every 30 seconds