Home > other >  Nest.js testing cannot override Provider
Nest.js testing cannot override Provider

Time:11-29

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.

CodePudding user response:

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

CodePudding user response:

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();
  });
});
  • Related