@alenon/grpc-mock-server

中文文档
3.2.9发布于 2026/05/09MITTypeScriptCommonJS构建来源已验证近 30 天下载 1.3万

gRPC mock server written on typescript

原始使用文档
文档目录23 节
  1. ✨ Features
  2. 📦 Installation
  3. 🚀 Quick Start
  4. 📚 Usage Examples
  5. Basic Example - Single Proto File
  6. Advanced Example - Multiple Proto Files
  7. Testing Example
  8. 📖 API Reference
  9. GrpcMockServer
  10. 🔍 Common Use Cases
  11. Custom Server Address
  12. Error Handling
  13. Method Chaining
  14. 🐛 Troubleshooting
  15. "Seems like the package name is wrong"
  16. "Seems like the service name is wrong"
  17. Import errors with multiple proto files
  18. Port already in use
  19. 📝 Examples
  20. 🤝 Contributing
  21. 📄 License
  22. 🔗 Links
  23. 💡 Tips

grpc-mock-server

A lightweight, TypeScript-first gRPC mock server designed for testing. Easily create mock gRPC services from your .proto files without complex setup.

NPM CI npm version License: MIT

✨ Features

  • 🚀 Zero Configuration - Get started in seconds with minimal setup
  • 📦 TypeScript Support - Built with TypeScript, includes type definitions
  • 🔧 Flexible - Support for single and multiple proto files with cross-package imports
  • 🧪 Test-Friendly - Perfect for unit and integration testing
  • 🎯 Simple API - Clean, intuitive interface
  • 🔄 Async/Await - Modern Promise-based API

📦 Installation

npm install @alenon/grpc-mock-server

Or with yarn:

yarn add @alenon/grpc-mock-server

🚀 Quick Start

Create a mock server in just a few lines:

import { GrpcMockServer } from '@alenon/grpc-mock-server';

// Create server instance
const server = new GrpcMockServer();

// Define your service implementations
const implementations = {
  GetUser: (call: any, callback: any) => {
    callback(null, { name: 'John Doe', id: '123' });
  }
};

// Add service from proto file
server.addService(
  './path/to/user.proto',
  'user',           // package name from proto
  'UserService',    // service name from proto
  implementations
);

// Start the server
await server.start();
console.log(`Server running at: ${server.serverAddress}`);

// Don't forget to stop when done!
await server.stop();

📚 Usage Examples

Basic Example - Single Proto File

This example demonstrates the simplest use case with a single proto file.

example.proto:

syntax = "proto3";

package com.alenon.example;

service ExampleService {
    rpc ex1 (ExampleRequest) returns (ExampleResponse) {}
}

message ExampleRequest {
    string msg = 1;
}

message ExampleResponse {
    string msg = 1;
}

example.ts:

import { GrpcMockServer } from '@alenon/grpc-mock-server';
import * as grpc from '@grpc/grpc-js';
import * as proto_loader from '@grpc/proto-loader';
import { ProtoUtils } from '@alenon/grpc-mock-server';

async function example() {
  const server = new GrpcMockServer();

  // Define implementations
  const implementations = {
    ex1: (call: any, callback: any) => {
      console.log('Received:', call.request.msg);
      callback(null, { msg: 'Hello from mock server!' });
    }
  };

  // Add service
  server.addService(
    __dirname + '/example.proto',
    'com.alenon.example',
    'ExampleService',
    implementations
  );

  // Start server
  await server.start();
  console.log(`Server listening at: ${server.serverAddress}`);

  // Create client and test
  const pkgDef = grpc.loadPackageDefinition(
    proto_loader.loadSync(__dirname + '/example.proto')
  );
  const proto = ProtoUtils.getProtoFromPkgDefinition('com.alenon.example', pkgDef);
  
  const client = new proto.ExampleService(
    server.serverAddress,
    grpc.credentials.createInsecure()
  );

  // Make request
  const response = await new Promise((resolve, reject) => {
    client.ex1({ msg: 'Hello!' }, (error: any, response: any) => {
      error ? reject(error) : resolve(response);
    });
  });

  console.log('Response:', response);
  
  await server.stop();
}

example().catch(console.error);

Advanced Example - Multiple Proto Files

When your proto files import from other packages, use an array of proto paths with includeDirs:

user.proto:

syntax = "proto3";

package user;

import "address.proto";

message User {
  string name = 1;
  common.Address address = 2;
}

service UserService {
  rpc GetUser (UserRequest) returns (UserResponse);
}

message UserRequest {
  string id = 1;
}

message UserResponse {
  User user = 1;
}

address.proto:

syntax = "proto3";

package common;

message Address {
  string postal_code = 1;
  string city = 2;
  string country = 3;
}

multi-proto-example.ts:

import { GrpcMockServer } from '@alenon/grpc-mock-server';

async function multiProtoExample() {
  const server = new GrpcMockServer();

  const implementations = {
    GetUser: (call: any, callback: any) => {
      callback(null, {
        user: {
          name: 'John Doe',
          address: {
            postal_code: '12345',
            city: 'San Francisco',
            country: 'USA'
          }
        }
      });
    }
  };

  // Use array of proto files with options
  server.addService(
    [
      __dirname + '/user.proto',
      __dirname + '/address.proto'
    ],
    'user',
    'UserService',
    implementations,
    {
      includeDirs: [__dirname],  // Required for imports
      keepCase: true,             // Preserve field names
      longs: String,              // Convert longs to strings
      enums: String,              // Convert enums to strings
      defaults: true,             // Include default values
      oneofs: true                // Handle oneof fields
    }
  );

  await server.start();
  // ... use server ...
  await server.stop();
}

Testing Example

Perfect for Jest or other testing frameworks:

import { GrpcMockServer, ProtoUtils } from '@alenon/grpc-mock-server';
import * as grpc from '@grpc/grpc-js';
import * as proto_loader from '@grpc/proto-loader';

describe('ExampleService Tests', () => {
  let server: GrpcMockServer;
  let client: any;

  beforeAll(async () => {
    server = new GrpcMockServer();
    
    server.addService(
      __dirname + '/example.proto',
      'com.alenon.example',
      'ExampleService',
      {
        ex1: (call: any, callback: any) => {
          callback(null, { msg: 'Test response' });
        }
      }
    );

    await server.start();

    // Setup client
    const pkgDef = grpc.loadPackageDefinition(
      proto_loader.loadSync(__dirname + '/example.proto')
    );
    const proto = ProtoUtils.getProtoFromPkgDefinition('com.alenon.example', pkgDef);
    client = new proto.ExampleService(
      server.serverAddress,
      grpc.credentials.createInsecure()
    );
  });

  afterAll(async () => {
    await server.stop();
  });

  it('should return response data', async () => {
    const response = await new Promise((resolve, reject) => {
      client.ex1({ msg: 'Test request' }, (error: any, response: any) => {
        error ? reject(error) : resolve(response);
      });
    });

    expect(response.msg).toBe('Test response');
  });
});

📖 API Reference

GrpcMockServer

Main class for creating and managing mock gRPC servers.

Constructor

new GrpcMockServer(serverAddress?: string)
  • serverAddress (optional): Server address in format host:port. Default: '127.0.0.1:50777'

Methods

addService(protoPath, pkgName, serviceName, implementations, protoLoadOptions?)

Adds a gRPC service to the mock server.

Parameters:

  • protoPath: string | string[] - Path to proto file(s). Use array for multiple files with imports.
  • pkgName: string - Package name as defined in your .proto file.
  • serviceName: string - Service name as defined in your .proto file.
  • implementations: object - Object mapping RPC method names to their implementations.
    • Each implementation is a function: (call: any, callback: any) => void
    • Call callback(null, response) for success
    • Call callback(error) for errors
  • protoLoadOptions (optional): Options for @grpc/proto-loader. Common options:
    • includeDirs: string[] - Directories to search for imported proto files
    • keepCase: boolean - Preserve field name casing (default: false)
    • longs: Function - How to handle long values (e.g., String)
    • enums: Function - How to handle enum values (e.g., String)
    • defaults: boolean - Include default values (default: false)
    • oneofs: boolean - Handle oneof fields (default: false)

Returns: GrpcMockServer (for method chaining)

start()

Starts the mock server.

Returns: Promise<GrpcMockServer>

stop()

Stops the mock server gracefully.

Returns: Promise<GrpcMockServer>

serverAddress

Property: string - The address where the server is listening.

server

Property: grpc.Server - Access to the underlying gRPC server instance.

🔍 Common Use Cases

Custom Server Address

const server = new GrpcMockServer('localhost:50051');

Error Handling

import * as grpc from '@grpc/grpc-js';

const implementations = {
  GetUser: (call: any, callback: any) => {
    if (!call.request.id) {
      callback({ code: grpc.status.INVALID_ARGUMENT, message: 'ID required' });
      return;
    }
    callback(null, { name: 'User', id: call.request.id });
  }
};

Method Chaining

await new GrpcMockServer()
  .addService(protoPath, pkgName, serviceName, impl1)
  .addService(protoPath2, pkgName2, serviceName2, impl2)
  .start();

🐛 Troubleshooting

"Seems like the package name is wrong"

  • Verify the pkgName matches exactly the package declaration in your .proto file
  • Check for typos and case sensitivity

"Seems like the service name is wrong"

  • Verify the serviceName matches exactly the service name in your .proto file
  • Check for typos and case sensitivity

Import errors with multiple proto files

  • Ensure includeDirs includes the directory containing imported proto files
  • Verify import paths in your proto files are correct relative to includeDirs
  • Use keepCase: true if your proto uses snake_case field names

Port already in use

  • Change the server address: new GrpcMockServer('127.0.0.1:50778')
  • Ensure previous server instances are stopped

📝 Examples

Check out the examples directory for complete working examples:

Run examples:

npm run example
npm run multi-proto-example

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

📄 License

MIT License - see LICENSE file for details.

💡 Tips

  • Always call stop() after tests to clean up resources
  • Use keepCase: true if your proto files use snake_case naming
  • For complex proto structures, use defaults: true and oneofs: true
  • The server uses insecure credentials by default (perfect for testing)

Made with ❤️ for the gRPC testing community

版本与发布记录

@alenon/grpc-mock-server 共发布 68 个版本,首次发布于 2020/06/04;过去 12 个月发布了 6 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.2.9latest2026/05/095 个月前
3.2.82026/04/186 个月前
3.2.72026/04/057 个月前
3.2.62026/02/078 个月前
3.2.52025/12/2510 个月前
3.2.42025/12/2510 个月前

关于 @alenon/grpc-mock-server 的常见问题

@alenon/grpc-mock-server 是什么?

gRPC mock server written on typescript

如何安装 @alenon/grpc-mock-server?

在项目目录运行 npm install @alenon/grpc-mock-server,或使用 pnpm add / yarn add / bun add。

@alenon/grpc-mock-server 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@alenon/grpc-mock-server 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@alenon/grpc-mock-server 可以商用吗?

@alenon/grpc-mock-server 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@alenon/grpc-mock-server 的下载量有多少?

根据 npm 官方下载统计,@alenon/grpc-mock-server 在 2026/10/06 – 2026/11/04 这 30 天内被下载 12,613 次,日均约 420 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@alenon/grpc-mock-server 还在维护吗?

最新版本 3.2.9 发布于 2026/05/09(5 个月前);至今共发布 68 个版本,其中过去 12 个月发布了 6 个;首次发布于 2020/06/04。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。