0
0
NestJSframework~20 mins

Mocking providers in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Mocking Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when a mocked service returns a fixed value?

Given a NestJS service mocked to always return 42 for a method call, what will the controller return when calling that method?

NestJS
class RealService {
  getValue() {
    return Math.random();
  }
}

const mockService = {
  getValue: () => 42
};

class Controller {
  constructor(private service) {}
  fetch() {
    return this.service.getValue();
  }
}

const controller = new Controller(mockService);
console.log(controller.fetch());
A42
BA random number between 0 and 1
Cundefined
DThrows a TypeError
Attempts:
2 left
💡 Hint

Think about what the mocked method returns instead of the real one.

📝 Syntax
intermediate
2:00remaining
Which option correctly mocks a provider in NestJS testing module?

Which code snippet correctly mocks a provider named DataService in a NestJS testing module?

NestJS
import { Test } from '@nestjs/testing';
import { DataService } from './data.service';

const mockDataService = { fetch: () => 'mocked data' };

async function setup() {
  const moduleRef = await Test.createTestingModule({
    providers: [
      DataService,
      // Mock provider here
    ],
  }).compile();
  return moduleRef;
}
A{ useValue: mockDataService }
B{ provide: DataService, useClass: mockDataService }
C{ provide: 'DataService', useValue: mockDataService }
D{ provide: DataService, useValue: mockDataService }
Attempts:
2 left
💡 Hint

Remember the syntax for replacing a provider with a mock object.

🔧 Debug
advanced
2:00remaining
Why does the mocked provider override the real one?

In this NestJS test setup, the mock provider does override the real service. What is the cause?

NestJS
const moduleRef = await Test.createTestingModule({
  providers: [
    RealService,
    { provide: RealService, useValue: mockService },
  ],
}).compile();
AThe real service is listed before the mock, so the mock is ignored
BNestJS merges providers and uses the last one, so this setup is correct
CThe mock provider must be listed before the real service to override it
DThe mock provider is ignored because useValue is invalid here
Attempts:
2 left
💡 Hint

Consider how NestJS handles multiple providers with the same token.

state_output
advanced
2:00remaining
What is the value of the call count after multiple calls to a mocked method?

Given a mocked provider method that tracks call count, what is the value of callCount after three calls?

NestJS
const mockService = {
  callCount: 0,
  fetch() {
    this.callCount++;
    return 'data';
  }
};

mockService.fetch();
mockService.fetch();
mockService.fetch();

console.log(mockService.callCount);
A3
B0
Cundefined
DThrows a ReferenceError
Attempts:
2 left
💡 Hint

Think about how the method updates the callCount property.

🧠 Conceptual
expert
2:00remaining
Which statement best describes the purpose of mocking providers in NestJS tests?

Why do developers mock providers in NestJS unit tests?

ATo automatically generate real service instances with default data
BTo improve application performance by using faster mock implementations in production
CTo isolate the unit under test by replacing dependencies with controlled mocks
DTo avoid writing tests for services by mocking their behavior
Attempts:
2 left
💡 Hint

Think about the goal of unit testing and how mocks help.