Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

133
Views
Nest.js testing cannot override Provider

I have a problem with overriding provider in nest.js application for testing. My stats.controller.spec.ts:

import { StatsService } from './services/stats.service';
import { StatsController } from './stats.controller';

describe('StatsController', () => {
  let controller: StatsController;

  const mockStatsService = {};

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      controllers: [StatsController],
      providers: [StatsService],
    })
      .overrideProvider(StatsService)
      .useValue(mockStatsService)
      .compile();

    controller = module.get<StatsController>(StatsController);
  });

  it('should be defined', () => {
    expect(controller).toBeDefined();
  });
});

My stats.controller.ts:

import { Controller, Get } from '@nestjs/common';
import { StatsService } from './services/stats.service';

@Controller('stats')
export class StatsController {
  constructor(private statsService: StatsService) {}

  @Get('weekly')
  getWeeklyStats() {
    return this.statsService.getWeeklyStats();
  }

  @Get('monthly')
  getMonthlyStats() {
    return this.statsService.getMonthlyStats();
  }
}

And my stats.service.ts:

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Trip } from 'src/trips/trip.entity';
import { from, map } from 'rxjs';
import { DatesService } from 'src/shared/services/dates.service';

@Injectable()
export class StatsService {
  constructor(
    @InjectRepository(Trip) private tripRepository: Repository<Trip>,
    private datesServices: DatesService,
  ) {}
 //some code here
}

And after running test I get following errors: Cannot find module 'src/trips/trip.entity' from 'stats/services/stats.service.ts' I would really appreciate some help.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The error is unrelated to if you're using a custom provider or overrideProvider. Jest by default doesn't understand absolute imports by default, and you need to use the moduleNameMapper option to tell Jest how to resolve src/ imports. Usually something like

{
  "moduleNameMapper": {
    "^src/(.*)$": "<rootDir>/$1"
  }
}

Assuming rootDir has been set to src

about 4 years ago · Juan Pablo Isaza Report

0

You can override the provider by using the below code in nest js:

import { StatsService } from './services/stats.service';
import { StatsController } from './stats.controller';

describe('StatsController', () => {
  let controller: StatsController;

  const mockStatsService = {};

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      controllers: [StatsController],
      providers: [{
        provide: StatsService,
       useValue: mockStatsService 
      }],
    })
      .compile();

    controller = module.get<StatsController>(StatsController);
  });

  it('should be defined', () => {
    expect(controller).toBeDefined();
  });
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!