'mocking callback in parameter

I have model setup like this

export class MyClass {
  grpcClient: MyGRPCClient;


  constructor(config: MyGRPCClientConfig) {
    this.grpcClient = new MyGRPCClient(
        config.serverUrl,
        grpc.credentials.createInsecure(),
    );
  }


  public methodA(): Promise<Array<String>> {
    return new Promise<Array<String>>((resolve, reject) => {
      this.grpcClient.methodGrpc(
          new MethodGrpcRequest(),
          (error, response) => { // <-- how to mock this (error, response) ?
            if (error) {
              reject(error);
            } else {
              resolve(response.getResult());
            }
          },
      );
    });
  }
}

In my test setup I want to mock grpcClient's outbound call so that when methodA() gets called, a new promise is returned and grpcClient's methodGrpc is mocked with what response I supply as part of test.



Solution 1:[1]

you can try something like this- jest.mock('./MyGRPCClient',()=>({ methodGrpc: jest.fn() }));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mohit jain