NestJS Research & Evidence-Based Practice 3 — Questions and Answers
Question 1: Evidence-based NestJS testing practice recommends using which testing module to create an isolated module context for unit tests?
- TestingModule from @nestjs/testing (Correct answer)
- MockModule from jest-mock
- TestBed from @angular/core/testing
- TestContext from supertest
Correct answer: TestingModule from @nestjs/testing
Test.createTestingModule() from @nestjs/testing creates an isolated NestJS application context suitable for unit and integration testing.
Question 2: Based on NestJS e2e testing patterns, which HTTP testing library is most commonly used alongside supertest to make requests against the app?
- axios
- node-fetch
- supertest (Correct answer)
- got
Correct answer: supertest
Supertest is the standard library used in NestJS e2e tests to make HTTP requests against the running application instance.
Question 3: When researching best practices, which NestJS testing approach verifies that a service method calls a repository method with correct arguments without hitting the database?
- Integration testing with a real database
- Snapshot testing
- Unit testing with mocked providers (Correct answer)
- Load testing with k6
Correct answer: Unit testing with mocked providers
Unit testing with mocked providers (using jest.fn() or createMock()) isolates the service under test from its real dependencies.
Question 4: Documentation research reveals that NestJS provides which decorator to override a provider with a mock value in a testing module?
- {provide: Token, useValue: mock}
- {inject: Token, mock: mock}
- {provide: Token, useFactory: () => mock}
- Both A and C are correct (Correct answer)
Correct answer: Both A and C are correct
Both useValue and useFactory are valid override strategies in the testing module's providers array, serving different needs.
Question 5: Based on evidence from NestJS documentation, which Jest configuration option is set in jest.config.js to map TypeScript path aliases in test files?
- moduleNameMapper (Correct answer)
- transform
- testPathPattern
- globals
Correct answer: moduleNameMapper
moduleNameMapper maps TypeScript path aliases (like @app/*) to their actual file paths so Jest can resolve them during tests.
Question 6: Research into NestJS test coverage shows which command runs Jest with coverage reporting enabled?
- npm run test
- npm run test:e2e
- npm run test:cov (Correct answer)
- npm run test:watch
Correct answer: npm run test:cov
npm run test:cov runs Jest with the --coverage flag, generating a coverage report for all tested files.
Question 7: When studying NestJS testing documentation, which method on the TestingModule instance retrieves a provider by its injection token?
- module.resolve()
- module.get() (Correct answer)
- module.inject()
- module.create()
Correct answer: module.get()
module.get(Token) synchronously retrieves a singleton provider instance from the testing module's dependency injection container.
Evidence-based NestJS testing practice recommends using which testing module to create an isolated module context for unit tests?