title: How mock an object created from a static method with a Mocked objet as param in Jest
body: <p>I wrote a Nestjs sample project,  The project source codes: <a href="https://github.com/hantsy/nestjs-graphql-sample" rel="nofollow noreferrer">https://github.com/hantsy/nestjs-graphql-sample</a> and tried to test a custom permissions guard.</p>
  <pre class="lang-js prettyprint-override"><code>@Injectable()
  export class HasPermissionsGuard implements CanActivate {
    constructor(private readonly reflector: Reflector) {}
    canActivate(
      context: ExecutionContext,
    ): boolean | Promise&lt;boolean&gt; | Observable&lt;boolean&gt; {
      const routePermissions = this.reflector.get&lt;PermissionType[]&gt;(
        HAS_PERMISSIONS_KEY,
        context.getHandler(),
      );
      if (!routePermissions || routePermissions.length == 0) {
        return true;
      }
      const { user } = GqlExecutionContext.create(context).getContext()
        .req as AuthenticatedRequest;
      return (
        user.permissions &amp;&amp;
        user.permissions.some((r) =&gt; routePermissions.includes(r))
      );
    }
  }
  </code></pre>
  <p>When I tried to use Jest to test the logic of <code>HasPermissionsGuard</code>,</p>
  <pre class="lang-js prettyprint-override"><code>  it('should return true if the `HasPermissions` decorator is set', async () =&gt; {
      const context = mock&lt;ExecutionContext&gt;();
      context.getHandler.mockReturnValue({} as any);
  
      // the following will failed. 
      const host = mock&lt;ExecutionContextHost&gt;();
      host.getArgByIndex.mockImplementation((idx: number) =&gt; {
        return {
          req: { user: { permissions: [PermissionType.WRITE_POSTS] } as any },
        } as any;
      });
  
      const ctx = mock&lt;GqlExecutionContext&gt;();
      ctx.getContext.mockReturnValue({
        req: { user: { permissions: [PermissionType.WRITE_POSTS] } as any },
      });
  
      reflecter.get
        .mockReturnValue([PermissionType.WRITE_POSTS])
        .calledWith(HAS_PERMISSIONS_KEY, context.getHandler());
  
      const result = await guard.canActivate(context);
  
      expect(result).toBeTruthy();
      expect(reflecter.get).toBeCalledTimes(1);
    });
  </code></pre>
  <p>I used <code>jest-mock-extended</code> and <code>jest</code>,  how to mock <code>GqlExecutionContext.create(context).getContext()</code>?</p>