NestJS NestJS Testing & Debugging 1 — Questions and Answers
Question 1: Which testing framework does NestJS use by default for unit and integration tests?
- Mocha
- Jest (Correct answer)
- Jasmine
- Vitest
Correct answer: Jest
NestJS uses Jest as its default testing framework, which is pre-configured when you scaffold a project with the Nest CLI.
Question 2: What is the primary purpose of `Test.createTestingModule()` in NestJS?
- To start the HTTP server for testing
- To create an isolated module context for testing (Correct answer)
- To mock all providers automatically
- To run all test suites in parallel
Correct answer: To create an isolated module context for testing
`Test.createTestingModule()` creates an isolated NestJS module environment that mirrors the real DI container, allowing you to test components in isolation.
Question 3: Which package must be imported to use `Test.createTestingModule()` in a NestJS test file?
- @nestjs/common
- @nestjs/testing (Correct answer)
- @nestjs/core
- @nestjs/platform-express
Correct answer: @nestjs/testing
`Test` and `TestingModule` are exported from the `@nestjs/testing` package, which is the dedicated testing utility package for NestJS.
Question 4: Which method must be called after `Test.createTestingModule()` to finalize the module for use in tests?
- .init()
- .bootstrap()
- .compile() (Correct answer)
- .build()
Correct answer: .compile()
`.compile()` resolves the module's dependency injection tree and returns a `TestingModule` instance ready for retrieving providers and controllers.
Question 5: How do you retrieve a provider instance from a compiled `TestingModule` in NestJS?
- module.inject(ServiceClass)
- module.resolve(ServiceClass)
- module.get(ServiceClass) (Correct answer)
- module.find(ServiceClass)
Correct answer: module.get(ServiceClass)
`module.get(ServiceClass)` retrieves a singleton instance of the specified provider from the module's DI container.
Question 6: What is the purpose of using `jest.fn()` when defining a mock provider in NestJS unit tests?
- To spy on real implementations without replacing them
- To create a stub function that records calls and returns undefined by default (Correct answer)
- To automatically generate mocks from class metadata
- To wrap an async function in a promise
Correct answer: To create a stub function that records calls and returns undefined by default
`jest.fn()` creates a mock function that tracks invocations, arguments, and return values, allowing you to assert on how it was called without running real logic.
Question 7: When writing a NestJS unit test for a controller that depends on a service, what is the recommended approach for providing the service?
- Import the real service module and let NestJS inject it
- Use `useClass` to provide an alternate real implementation
- Provide a mock object using `useValue` with jest mock functions (Correct answer)
- Use `useFactory` to instantiate the real service without a database
Correct answer: Provide a mock object using `useValue` with jest mock functions
Using `useValue` with a mock object (containing `jest.fn()` stubs) isolates the controller under test from real service logic and external dependencies.
Which testing framework does NestJS use by default for unit and integration tests?