title: Mocking HTTP interceptors in Nest JS
body: <p>I have a core module that provides an authentication interceptor in Nest js.
  Here is the core module.</p>
  
  <pre><code>@Module({
      imports: [
      providers: [{
          provide: APP_INTERCEPTOR,
          useClass: LoggerInterceptor
      }
    ]
  })
  export class ConfModule {
  }
  </code></pre>
  
  <p>This conf module is been imported by the app module. </p>
  
  <pre><code>@Module({
      imports: [
          ConfModule
      ]
  })
  export class AppModule {
  }
  </code></pre>
  
  <p>Here is my <code>LoggerInterceptor</code> class</p>
  
  <pre><code>@Injectable()
  export class LoggerInterceptor implements NestInterceptor {
      constructor(private readonly reflector: Reflector, private readonly connection: Connection) {
      }
  
      async intercept(context: ExecutionContext, next: CallHandler): Observable&lt;any&gt; {
          // tslint:disable-next-line:no-console
          console.log(context.switchToHttp().getRequest().body);
          await this.connection.createEntityManager().save(context.switchToHttp().getRequest().body);
          return next.handle();
      }
  }
  </code></pre>
  
  <p>I am currently writing an E2E test and will want to override the logger interceptor. Here is my MockLoggerInterceptor </p>
  
  <pre><code>export class MockLoggerInterceptor extends LoggerInterceptor {
      reflex: Reflector;
      conn: Connection;
  
      constructor(reflector: Reflector, connection: Connection) {
          super(reflector, connection);
          this.reflex = reflector;
          this.conn = connection;
      }
  
      // @ts-ignore
      async intercept(context: ExecutionContext, next: CallHandler): Observable&lt;any&gt; {
          // tslint:disable-next-line:no-console
          console.log(context.switchToHttp().getRequest().body);
          await this.conn.createEntityManager().save(context.switchToHttp().getRequest().body);
          return next.handle();
      }
  
  }
  </code></pre>
  
  <p>Here is my test Suite</p>
  
  <pre><code>describe('PermissionController', () =&gt; {
      let applicationContext: INestApplication;
      let connection: Connection;
      let testUtils: TestUtils;
      let appHeader: App;
  
      beforeAll(async () =&gt; {
          const moduleRef: TestingModule = await Test.createTestingModule({
              imports: [AppModule]
          }).overrideInterceptor(LoggerInterceptor).useClass(MockLoggerInterceptor).compile();
  
          applicationContext = moduleRef.createNestApplication();
          connection = getConnection();
          await applicationContext.init();
          testUtils = new TestUtils(connection);
          appHeader = await testUtils.getAuthorisedApp();
  
      });
      it('Test of permission can be created', async () =&gt; {
          await request(applicationContext.getHttpServer())
              .post('/permissions')
              .set({
                  'X-APP-CODE': appHeader.code,
                  'X-APP-TOKEN': appHeader.token,
                  'Authorisation': appHeader.token
              })
              .send(
                  {
                      permissionName: 'newPermission'
  
                  }
              ).expect(201);
      });
  
      afterAll(async () =&gt; {
          await connection.close();
          await applicationContext.close();
  
      });
  });
  </code></pre>
  
  <p>My test still uses the conf module logger instead of the test logger. Apparently there are other use cases but this is the best I can give.</p>