gRPC mock server for tests with typescript definitions.
Installation:
npm i @alenon/grpc-mock-server
Usage example:
private static readonly PROTO_PATH: string = __dirname + "example.proto";
private static readonly PKG_NAME: string = "com.alenon.example";
private static readonly SERVICE_NAME: string = "ExampleService";
...
const implementations = {
ex1: (call: any, callback: any) => {
const response: any =
new this.proto.ExampleResponse.constructor({msg: "the response message"});
callback(null, response);
},
};
this.server.addService(PROTO_PATH, PKG_NAME, SERVICE_NAME, implementations);
this.server.start();
Migrating from 1.x to 3.x:
-
Change grpc dependecy from this
import * as grpc from "grpc";
by this:import * as grpc from "@grpc/grpc-js";
-
Handle
start
andstop
functions which are now asynchronous and return Promises:
this.server = new GrpcMockServer();
// async / await
try {
await this.server.start();
console.log('do work...');
await this.server.stop();
} catch (error) {
console.log(error);
}
// standard promises
this.server.start()
.then(() => console.log('do work...'))
.then(() => this.server.stop())
.catch((error) => console.log(error));