@connectrpc/connect-playwright

中文文档
0.6.1发布于 2025/11/07Apache-2.0TypeScriptESM + CJS零依赖近 30 天下载 3.2万

e2e utilities for use with Playwright and Connect

原始使用文档
文档目录3 节
  1. Installation
  2. Usage
  3. Caveats

@connectrpc/connect-playwright

Connect is a family of libraries for building type-safe APIs with different languages and platforms. Connect-ES brings them to TypeScript, the web browser, and to Node.js.

@connectrpc/connect-playwright provides utilities for writing end-to-end tests for a Connect-ES application using Playwright.

Installation

npm install @connectrpc/connect-playwright

Usage

@connectrpc/connect-playwright is designed to simplify writing Playwright tests for your Connect-ES application by providing an API to mock unary RPCs.

Unary Connect RPCs can be customized through the usage of the createMockRouter function. This function allows you to modify the behavior of one-to-many RPCs in your service.

Let's walk through an example using the following Protobuf file:

syntax = "proto3";
package example;

message GetUserRequest {
  int32 id = 1;
}

message GetUserResponse {
  int32 id = 1;
  string name = 2;
  string role = 3;
}

message UpdateUserRequest {
  int32 id = 1;
  string name = 2;
  string role = 3;
}

message UpdateUserResponse {}

message DeleteUserRequest {
  int32 id = 1;
}

message DeleteUserResponse {}

service UserService {
  rpc GetUser(GetUserRequest) returns (GetUserResponse) {}
  rpc UpdateUser(UpdateUserRequest) returns (UpdateUserResponse) {}
  rpc DeleteUser(DeleteUserRequest) returns (DeleteUserResponse) {}
}

The createMockRouter function accepts a Playwright BrowserContext and an options object for specifying the baseUrl that your Connect client would typically interact with in a non-test environment. This function call will return a MockRouter object that can be used to specify RPC behavior.

The MockRouter type offers two methods, which allows you to specify mocks at either the service level or the individual RPC level.

Service-Level Mocks

The service method provides the ability to mock an entire service in one invocation. It accepts two arguments: the service definition generated from your Protobuf file and then, one of two values:

  • An object containing all the desired mock implementations for your RPCs.
  • mock - A string that configures the call to simply return the type specified by the RPC, with all default values set. This is useful if you don’t care about the response but want it to succeed without hitting the backend.

Note: any RPC not specified will simply pass through to the actual implementation.

For example:

test("mock RPCs at service level", async ({ context }) => {
  const mock = createMockRouter(context, {
    baseUrl: "https://api.myproject.com",
  });

  await mock.service(UserService, {
    // Mock getUser to return a custom response.
    getUser() {
      return {
        id: 1,
        name: "Homer Simpson",
        role: "Safety Inspector",
      };
    },
  });
  // ...
});

Again, note that in the above example, the updateUser and deleteUser functions will be passed through to the actual implementation since they were not specified.

If you do not require any custom behavior and simply want to mock all RPCs in your service, you can do this with the shorthand:

await createMockRouter(context, {
  baseUrl: "https://api.myproject.com",
}).service(UserService, "mock");

RPC-Level Mocks

The rpc method provides the ability to mock a single RPC. It accepts three arguments: the service definition generated from your Protobuf file, the RPC name you would like to mock, and then, one of two values:

  • A function containing the desired mock implementation for the RPC.
  • mock - A string that configures the call to simply return the type specified by the RPC, with all default values set.

For example:

test("mock RPCs at rpc level", async ({ context }) => {
  const mock = createMockRouter(context, {
    baseUrl: "https://api.myproject.com",
  });

  // Mock getUser with a custom response
  await mock.rpc(UserService.method.getUser, () => {
    return {
      id: 1,
      name: "Homer Simpson",
      role: "Safety Inspector",
    };
  });

  // Just return a default-constructed DeleteUserResponse without hitting the actual RPC.
  await mock.rpc(UserService.method.deleteUser, "mock");
});

The same rule regarding unspecified RPCs applies and the updateUser function will be passed through to the actual implementation since it was not specified.

Note that both the service and rpc methods return a Promise, which resolves to the original MockRouter object. This allows you to utilize promise-chaining if you wish.

An example of using all of the above in practice can be found in the connect-playwright-example directory.

Caveats

Currently, only unary requests are able to be mocked. We hope to add streaming support soon.

版本与发布记录

@connectrpc/connect-playwright 共发布 9 个版本,首次发布于 2023/08/16;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.6.1latest2025/11/071 年前
0.6.02024/11/202 年前
0.5.0rc2024/10/182 年前
0.4.02024/09/202 年前
0.3.22024/01/252 年前
0.3.12023/10/073 年前

关于 @connectrpc/connect-playwright 的常见问题

@connectrpc/connect-playwright 是什么?

e2e utilities for use with Playwright and Connect

如何安装 @connectrpc/connect-playwright?

在项目目录运行 npm install @connectrpc/connect-playwright,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@playwright/test(^1.0.0)、@connectrpc/connect(^2.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@connectrpc/connect-playwright 支持 TypeScript 吗?

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

@connectrpc/connect-playwright 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@connectrpc/connect-playwright 可以商用吗?

@connectrpc/connect-playwright 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@connectrpc/connect-playwright 的下载量有多少?

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

@connectrpc/connect-playwright 还在维护吗?

最新版本 0.6.1 发布于 2025/11/07(1 年前);至今共发布 9 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/08/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。