我找不到任何关于如何在 NestJS 中测试拦截器的解释。
请帮我用玩笑测试拦截器?
import { Injectable, NestInterceptor, ExecutionContext, CallHandler, RequestTimeoutException } from "@nestjs/common";
import { Observable, throwError, TimeoutError } from "rxjs";
import { catchError, timeout } from "rxjs/operators";
@Injectable()
export class TimeoutInterceptor implements NestInterceptor {
constructor(private readonly interval: number) {}
intercept(_context: ExecutionContext, next: CallHandler): Observable<any> {
if (this.interval > 0) {
return next.handle().pipe(
timeout(this.interval),
catchError((error) => {
if (error instanceof TimeoutError) {
return throwError(new RequestTimeoutException(`The operation timed out. `));
}
return throwError(error);
}),
);
}
return next.handle();
}
}