Nest.js 和 Jest 测试框架:无法解析 RootTestModule 上下文中 AuthGuard 的依赖关系

问题描述 投票:0回答:1

我正在使用 Jest 框架来测试我的 Nest.js 应用程序。这是

recovery.controller.spec.ts
文件:

import { Test, TestingModule } from '@nestjs/testing';
import { RecoveryController } from './recovery.controller';

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

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      controllers: [RecoveryController]
    }).compile();

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

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

现在,当我执行此测试时,我收到以下错误消息:

Error: Nest can't resolve dependencies of the AuthGuard (?). Please make sure that the argument JwtService at index [0] is available in the RootTestModule context.

老实说,我就是无法修复它。应用程序工作得很好,控制器和服务都没有错误。我一直在尝试将

RecoveryService
添加到该模块的提供和导入中,但没有帮助。另外,当我将
RecoveryService
添加到提供列表时,错误消息更改为以下内容:

Error: Nest can't resolve dependencies of the RecoveryService (?, CryptographicService, UsersService). Please make sure that the argument ConfirmationHashService at index [0] is available in the RootTestModule context.

有人可以解释一下错误在哪里吗?下面我将提供相关模块和服务的源代码,如果这可能有帮助的话:

recovery.module.ts

@Module({
  controllers: [RecoveryController],
  providers: [RecoveryService],
  imports: [
    JwtModule.registerAsync({
      useFactory: async (configService: ApiConfigService) => ({
        secret: configService.jwtAuthConfig.secret
      }),
      inject: [ApiConfigService]
    }),
    ConfirmationHashModule,
    UsersModule
  ]
})
export class RecoveryModule {}

recovery.service.ts
的构造函数:

constructor(
  private readonly confirmationHashService: ConfirmationHashService,
  private readonly cryptographicService: CryptographicService,
  private readonly usersService: UsersService
) {}

auth.module.ts

@Module({
  controllers: [AuthController],
  providers: [AuthService],
  imports: [
    forwardRef(() => UsersModule),
    forwardRef(() => RolesModule),
    SequelizeModule.forFeature([Session, UserSettings]),
    JwtModule.registerAsync({
      useFactory: async (configService: ApiConfigService) => ({
        secret: configService.jwtAuthConfig.secret
      }),
      inject: [ApiConfigService]
    })
  ],
  exports: [AuthService, JwtModule]
})
export class AuthModule {}

auth.service.ts
的构造函数:

constructor(
  private readonly cryptographicService: CryptographicService,
  private readonly jwtService: JwtService,
  private readonly configService: ApiConfigService,
  private readonly phoneService: PhoneService,
  private readonly timeService: TimeService,
  @Inject(forwardRef(() => RolesService))
  private readonly rolesService: RolesService,
  @Inject(forwardRef(() => EmailService))
  private readonly emailService: EmailService,
  @Inject(forwardRef(() => UsersService))
  private readonly usersService: UsersService,
  @InjectModel(Session) private readonly sessionRepository: typeof Session
) {}

提前感谢大家的帮助!

testing jestjs nestjs
1个回答
0
投票

因此,您在测试规范文件中创建的模块仅引用您的控制器 - 该控制器的任何依赖项都需要进行模拟。

您可能想要的(至少对于 AuthGuard)是

const module: TestingModule = await Test.createTestingModule({
  controllers: [RecoveryController]
})
.overrideGuard(AuthGuard)
.useValue({ canActivate: jest.fn(() => true) })
.compile();
© www.soinside.com 2019 - 2024. All rights reserved.