Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
@connectrpc/connect-playwright
Advanced tools
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.
npm install @connectrpc/connect-playwright
@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.
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:
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");
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:
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, UserService.methods.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, UserService.methods.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.
Currently, only unary requests are able to be mocked. We hope to add streaming support soon.
FAQs
e2e utilities for use with Playwright and Connect
The npm package @connectrpc/connect-playwright receives a total of 664 weekly downloads. As such, @connectrpc/connect-playwright popularity was classified as not popular.
We found that @connectrpc/connect-playwright demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.