Questions
11 of 29
1What is a pipe in NestJS and what are its two primary uses?
2How does ValidationPipe work with class-validator and class-transformer in NestJS?
3What is the difference between applying a pipe at the param level vs handler level vs globally in NestJS?
4How do you write a custom pipe for parsing and validating a MongoDB ObjectId param in NestJS?
5What are all the built-in pipes NestJS ships with?
6What is class-transformer and how does it complement class-validator in NestJS?
7How do you write a custom pipe that sanitizes a string input (trim and lowercase) in NestJS?
8How do you write a pipe that uses metatype to skip validation for primitive types the same way ValidationPipe does?
9What are the two ways to register a global pipe in NestJS and what is the difference?
10What are all the significant ValidationPipe options and what does each do in NestJS?
11How do you unit test a custom pipe in NestJS?
12Can a pipe be asynchronous in NestJS and how do you implement one?
13How do you write an async custom validator that checks uniqueness against a database in NestJS?
14How do you validate arrays and conditional fields with class-validator in NestJS?
15How do you use validation groups with class-validator to apply different rules for create vs update operations in NestJS?
16What does @Exclude() and @Expose() do and how do you use them to strip sensitive fields from responses?
17What is the excludeExtraneousValues option in class-transformer and how does it differ from whitelist in ValidationPipe?
18How do you make a custom pipe configurable by accepting constructor options in NestJS?
19How do you customise the error response format produced by ValidationPipe in NestJS?
20How do you handle file upload validation with ParseFilePipe and custom file validators in NestJS?
21What interface must a custom pipe implement and what does the transform() method receive?
22What is the difference between a transformation pipe and a validation pipe in NestJS?
23How do you pass options to a built-in pipe like ParseIntPipe or ParseUUIDPipe in NestJS?
24What is class-validator and how does it integrate with NestJS ValidationPipe?
25What is the @ValidateNested() decorator and when must you pair it with @Type()?
26How do you write a custom class-validator decorator in NestJS?
27What does the @Transform() decorator do in class-transformer and when should you use it?
28How does @Type() interact with discriminated unions and polymorphic DTOs in class-transformer?
29How do you write a pipe that validates and parses a date string from a route parameter in NestJS?
11 / 29

How do you unit test a custom pipe in NestJS?

Difficulty: 5/10
custom pipe implementation, jest unit testing, dependency injection

Call transform() directly — no HTTP stack needed. For stateless pipes, instantiate with new and test each branch. For pipes with injected services, use Test.createTestingModule() to provide mocks and retrieve the pipe via module.get().

Unit tests for stateless and service-dependent pipes
Pipe testing strategy:
  1. 1

    Call transform() directly — no need for a full HTTP request in unit tests.

  2. 2

    Test all branches: valid input, invalid input, boundary values, and missing input.

  3. 3

    For async pipes use await and resolves/rejects matchers from Jest.

  4. 4

    Mock injected services with useValue and jest.fn() to control return values per test.

  5. 5

    Test the exact exception type and message — BadRequestException vs NotFoundException matters.

Scenario Questions

0-2 years experience

  1. 1You have a simple validation pipe that trims whitespace and throws BadRequestException if the string is empty. How would you write a unit test for it using Jest?
  2. 2If your custom pipe injects a ConfigService, how do you mock that dependency in the unit test?
  3. 3What would happen if you forget to call the pipe's transform method inside your test case?

2-5 years experience

  1. 1We added a CSV‑parsing pipe and the unit test becomes flaky when the input contains newline characters. How would you debug and fix the test?
  2. 2Why might a pipe's unit test fail when the Nest testing module has global pipes enabled, and how would you resolve it?
  3. 3When refactoring the pipe to use a third‑party library, existing tests break. What trade‑offs do you consider when updating the tests versus the implementation?

5-8 years experience

  1. 1Our CI runs all pipe unit tests in parallel, but they consume a lot of time due to heavy mocking of external services. How would you redesign the testing approach to improve speed without losing coverage?
  2. 2Describe how you would structure a reusable test suite for a pipe that is shared across multiple modules, ensuring maintainability as the codebase grows.
  3. 3If a pipe throws different error types based on validation rules, how would you design your unit tests to cover all edge cases while keeping the suite DRY?

8+ years experience

  1. 1Our organization is migrating to a distributed architecture with shared libraries for common pipes. What strategy would you propose for standardizing unit testing of these shared pipes across teams, and how would you handle versioning and backward compatibility?
  2. 2How would you evaluate the cost‑benefit of moving from isolated pipe unit tests to contract tests that validate end‑to‑end request handling, considering long‑term maintenance?
  3. 3When legacy pipes are being deprecated, how do you ensure their unit tests remain useful for regression detection while encouraging migration to newer implementations?

Follow-up Questions

  • What challenges could appear if the pipe depends on request‑scoped data?
  • How would you test a pipe that performs asynchronous operations?
  • Where would you place these pipe tests in a CI pipeline to ensure quick feedback?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.