我遇到了全局防护、拦截器和过滤器的问题,它们在运行时有效,但在 e2e 测试期间不起作用。
这是守卫设置的示例。对于所有中间件(如项目)来说,问题都是相同的,除非我在本地为每个控制器等指定它们,否则它们不会触发。
我的顶级模块:
@Module({
imports: [
JwtModule.register({
secret: JWT_PASS,
global: true,
}),
BoothModule,
SessionTypeModule,
],
providers: [
{
provide: APP_GUARD,
useClass: AuthenticationGuard,
},
],
})
export class AppModule {}
一名警卫:
@Injectable()
export class AuthenticationGuard implements CanActivate {
private readonly log = new Logger('AuthenticationGuard');
constructor(private readonly jwtService: JwtService) {}
async canActivate(context: ExecutionContext): Promise<boolean> {
console.log('triggered?!');
....
return true;
}
控制台日志有效,测试因保护逻辑失败而失败。
beforeAll(async () => {
const moduleFixture = await Test.createTestingModule({
imports: [
AppModule,
ClientsModule.register([
{
name: '<name>',
transport: Transport.GRPC,
options: {
url: 'localhost:5001',
package: '<package>',
protoPath: '<path>',
credentials: GRPC.ChannelCredentials.createInsecure(),
},
},
]),
],
}).compile();
app = moduleFixture.createNestApplication();
app.useGlobalInterceptors(new ExceptionInterceptor());
app.connectMicroservice<MicroserviceOptions>({
transport: Transport.GRPC,
options: {
url: 'localhost:5001',
package: SPEEDPHOTO_PACKAGE_NAME,
protoPath: BOOTH_PROTO,
},
});
await app.startAllMicroservices();
await app.init();
const clientGrpc: ClientGrpc = app
.getMicroservices()[0]
.get(BOOTH_SERVICE_NAME);
client = clientGrpc.getService<BoothServiceClient>(BOOTH_SERVICE_NAME);
});
它仅在测试期间失败。