NestJS NestJS Testing & Debugging 2 — Questions and Answers
Question 1: Which library does NestJS recommend for making HTTP requests in end-to-end (e2e) tests?
- axios
- node-fetch
- supertest (Correct answer)
- got
Correct answer: supertest
NestJS e2e test scaffolds use `supertest`, which allows you to send HTTP requests directly to an Express/Fastify app without starting a real network server.
Question 2: In a NestJS e2e test, how do you initialize the application before running tests?
- Call `NestFactory.create()` and `app.init()` inside `beforeAll()` (Correct answer)
- Call `Test.createTestingModule().compile()` without calling `init()`
- Import the AppModule directly and call its constructor
- Use `jest.setup()` to bootstrap the app globally
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 3: What should be called in `afterAll()` at the end of a NestJS e2e test suite?
- app.destroy()
- app.shutdown()
- app.close() (Correct answer)
- app.terminate()
Correct answer: app.close()
`app.close()` gracefully shuts down the NestJS application and triggers `OnModuleDestroy` lifecycle hooks, preventing open handles from keeping Jest running.
Question 4: How do you override a provider in a NestJS `TestingModule` with a custom mock?
- module.providers.replace(RealService, MockService)
- builder.overrideProvider(RealService).useValue(mockService) (Correct answer)
- module.set(RealService, mockService)
- jest.mock('./real.service')
Correct answer: builder.overrideProvider(RealService).useValue(mockService)
`overrideProvider()` is a method on the `TestingModuleBuilder` that lets you swap a registered provider with a mock before calling `.compile()`.
Question 5: What does `jest.spyOn(service, 'methodName')` allow you to do in a NestJS test?
- Replace the method permanently with a mock function
- Wrap the real method to track calls while still executing the original (Correct answer)
- Throw an error whenever the method is called
- Automatically generate parameter types from TypeScript metadata
Correct answer: Wrap the real method to track calls while still executing the original
`jest.spyOn()` wraps an existing method so you can assert on calls and optionally override the return value, while the real implementation still runs unless you explicitly mock it.
Question 6: When testing a NestJS service that uses a repository (TypeORM), what is the correct token to use with `getRepositoryToken(Entity)` in the test providers array?
- The Entity class reference itself
- The string 'EntityRepository'
- The result of `getRepositoryToken(Entity)` from `@nestjs/typeorm` (Correct answer)
- The string injected via `@InjectRepository()`
Correct answer: The result of `getRepositoryToken(Entity)` from `@nestjs/typeorm`
`getRepositoryToken(Entity)` from `@nestjs/typeorm` returns the DI token NestJS uses internally, which must match exactly when providing a mock repository.
Question 7: Which Jest configuration option in `jest.config.js` tells Jest how to handle TypeScript files in a NestJS project?
- transform: { '^.+\\.ts$': 'babel-jest' }
- transform: { '^.+\\.(t|j)s$': 'ts-jest' } (Correct answer)
- preset: 'ts-node'
- moduleFileExtensions: ['ts', 'js']
Correct answer: transform: { '^.+\\.(t|j)s$': 'ts-jest' }
NestJS projects configure Jest to use `ts-jest` as the transformer for `.ts` and `.js` files so TypeScript source is compiled before tests run.
Which library does NestJS recommend for making HTTP requests in end-to-end (e2e) tests?