Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading