I can't find any explanation on how to test interceptors in NestJS.
Please help me to test the Interceptor using jest?
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();
}
}
I tried to write unit tests for this interceptor once but I didn't like it :/ Look: https://gist.github.com/micalevisk/33d793202541f044d8f5bccb81049b94