Getting dependencies error using Nest with Jest

I have this test:

import {AuthController} from './auth.controller';
import {getRepositoryToken} from '@nestjs/typeorm';
import {Test, TestingModule} from '@nestjs/testing';
import {ResetService} from '../service/reset.service'
import {LocalStrategy} from '../strategy/local.strategy';
import {UserRepository} from '@lib/database/identity/repository/user.repository';

describe('Auth Controller', () => {
  let controller: AuthController;
  
  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      controllers: [AuthController],
        providers: [
            {
                provide: ResetService,
                useValue: {},
            },
            {
                provide: LocalStrategy,
                useValue: {},
            },
            {
                provide: getRepositoryToken(UserRepository),
                useValue: {
                    find: jest.fn(),
                    insert: jest.fn(),
                }
            }
        ]
    }).compile();
    
    controller = module.get<AuthController>(AuthController);
  });
  
  it('should be defined', () => {
    expect(controller).toBeDefined();
  });
});

An the following auth.module:

import {Module} from '@nestjs/common';
import {MailingModule} from '@lib/mailing';
import {CronService} from './cron/cron.service';
import {ScheduleModule} from '@nestjs/schedule';
import {ResetService} from './service/reset.service';
import {AuthController} from './auth/auth.controller';
import {LocalStrategy} from './strategy/local.strategy';
import {SecurityModule} from '@lib/security/security.module';
import {AliveCheckModule} from '@lib/alive-check/alive-check.module';
import {UserRepository} from '@lib/database/identity/repository/user.repository';
import {TypeOrmModule} from '@nestjs/typeorm';

@Module({
    imports: [
        MailingModule,
        SecurityModule,
        AliveCheckModule,
        ScheduleModule.forRoot()
    ],
    providers: [
        CronService,
        ResetService,
        LocalStrategy,
        UserRepository
    ],
    controllers: [
        AuthController
    ]
})
export class AuthModule {
}

in auth.controller.ts

@ApiBearerAuth()
@ApiTags('auth')
@Controller('')
export class AuthController {
    
    constructor(
        private readonly resetSrv: ResetService,
        private readonly localStr: LocalStrategy,
        @Inject(UserRepository.TOKEN)
        private readonly userRepo: UserRepository,
        @Inject(TokenRepository.TOKEN)
        private readonly tokenRepo: TokenRepository,
        @Inject(ChallengeRepository.TOKEN)
        private readonly challengeRepo: ChallengeRepository
    ) {

I keep getting the following error:

Error: Nest can’t resolve dependencies of the AuthController
(ResetService, LocalStrategy, ?, TokenRepository,
ChallengeRepository). Please make sure that the argument
UserRepository at index [2] is available in the RootTestModule
context.

Potential solutions:

  • If UserRepository is a provider, is it part of the current RootTestModule?
  • If UserRepository is exported from a separate @Module, is that module imported within RootTestModule? @Module({
    imports: [ /* the Module containing UserRepository */ ] })

Can you please help me?

>Solution :

Rather than use getRepositoryToken, as you would if you were using TypeormModule.forFeature(), you should use provide: UserRepository.TOKEN, to match what you have in the @Inject(). This ensures that the injection tokens line up properly

Leave a Reply