title: How to test NestJs response interceptor
body: <p>I tried to follow <a href="https://stackoverflow.com/questions/57730120/how-to-test-nestjs-interceptor">this thread</a> but it I keep getting an error.</p>
  <p><strong>transform-response.interceptor.ts:</strong></p>
  <pre><code>import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
  import { Observable } from 'rxjs';
  import { map } from 'rxjs/operators';
  import { ApiResponseInterface } from '@walletxp/shared-interfaces';
  
  @Injectable()
  export class TransformResponseInterceptor&lt;T&gt;
    implements NestInterceptor&lt;T, ApiResponseInterface&lt;Record&lt;string, unknown&gt;&gt;&gt;
  {
    intercept(context: ExecutionContext, next: CallHandler): Observable&lt;ApiResponseInterface&lt;Record&lt;string, unknown&gt;&gt;&gt; {
      return next.handle().pipe(map((data) =&gt; ({ success: true, data })));
    }
  }
  
  </code></pre>
  <p>and for it's test, <strong>transform-response.interceptor.spec.ts:</strong></p>
  <pre><code>import { TransformResponseInterceptor } from './transform-response.interceptor';
  const interceptor = new TransformResponseInterceptor();
  
  const executionContext: any = {
    switchToHttp: jest.fn().mockReturnThis(),
    getRequest: jest.fn().mockReturnThis(),
  };
  
  const callHandler = {
    handle: jest.fn(),
  };
  
  describe('ResponseInterceptor', () =&gt; {
    it('should be defined', () =&gt; {
      expect(interceptor).toBeDefined();
    });
    describe('#intercept', () =&gt; {
      it('t1', async () =&gt; {
        (executionContext.switchToHttp().getRequest as jest.Mock&lt;any, any&gt;).mockReturnValueOnce({
          body: { data: 'mocked data' },
        });
        callHandler.handle.mockResolvedValueOnce('next handle');
        const actualValue = await interceptor.intercept(executionContext, callHandler);
        expect(actualValue).toBe('next handle');
        expect(executionContext.switchToHttp().getRequest().body).toEqual({
          data: 'mocked data',
          addedAttribute: 'example',
        });
        expect(callHandler.handle).toBeCalledTimes(1);
      });
    });
  });
  
  </code></pre>
  <p>My goal would be to mock the data returned from the controller and check if after it goes through the interceptor it equals the formatted data that I want.</p>