Client
Registering a route handler
Registering a route handler allows you to intercept requests and modify the response in various ways via the Route class passed as an argument to the handler.
- TypeScript
- JavaScript
// Matching URL's with glob patterns
client.route("**/api", (route) => {
return route.fulfill(...);
});
// Matching URL's with regular expressions
client.route(/^.*?\/api$/, (route) => {
return route.fulfill(...);
});
// Matching URL's with a custom function
client.route((url: URL) => {
return url.pathname.startsWith("/api");
}, (route) => {
return route.fulfill(...);
});
// Matching URL's with glob patterns
client.route("**/api", (route) => {
return route.fulfill(...);
});
// Matching URL's with regular expressions
client.route(/^.*?\/api$/, (route) => {
return route.fulfill(...);
});
// Matching URL's with a custom function
client.route((url) => {
return url.pathname.startsWith("/api");
}, (route) => {
return route.fulfill(...);
});
Mock n times
The route handler will be removed once its been executed n times.
- TypeScript
- JavaScript
// Once the route handler is matched 3 times it will be removed from the client
client.route("**/api", (route) => {
return route.fulfill(...);
}, { times: 3 });
// Once the route handler is matched 3 times it will be removed from the client
client.route("**/api", (route) => {
return route.fulfill(...);
}, { times: 3 });
Mock client and server side requests (default behaviour)
RouteOptions.type
defaults to "both"
when not explicitly set.
- TypeScript
- JavaScript
// The route handler will handle requests that originate from the client or server side
client.route("**/api", (route) => {
return route.fulfill(...);
}, { type: "both" });
// The route handler will handle requests that originate from the client or server side
client.route("**/api", (route) => {
return route.fulfill(...);
}, { type: "both" });
Only mock server side requests
Setting RouteOptions.type
to "server-only"
will only mock server side requests. This is useful if you differentiate between client and server side requests. For example, authentication strategies can be different between client and server side.
- TypeScript
- JavaScript
// The route handler will handle requests that originate from the server only
client.route("**/api", (route) => {
return route.fulfill(...);
}, { type: "server-only" });
// The route handler will handle requests that originate from the server only
client.route("**/api", (route) => {
return route.fulfill(...);
}, { type: "server-only" });
Only mock client side requests
Setting RouteOptions.type
to "client-only"
will only mock client side requests. This is useful if you differentiate between client and server side requests. For example, authentication strategies can be different between client and server side.
This is the equivalent of Playwright's page.route
or Cypress' cy.intercept
. This is a convenient interface that allows you to mock requests across the stack without having to call different functions.
- TypeScript
- JavaScript
// The route handler will handle requests that originate from the client (browser) only
client.route("**/api", (route) => {
return route.fulfill(...);
}, { type: "client-only" });
// The route handler will handle requests that originate from the client (browser) only
client.route("**/api", (route) => {
return route.fulfill(...);
}, { type: "client-only" });
Unregistering a route handler
If you want to disable a route handler, you can use the unroute
method:
- TypeScript
- JavaScript
const routeHandlerId = client.route("**/api", (route) => {
return route.fulfill(...);
});
client.unroute(routeHandlerId);
const routeHandlerId = client.route("**/api", (route) => {
return route.fulfill(...);
});
client.unroute(routeHandlerId);
Unregistering all route handlers
Used to unregister all route handlers registered on the client.
- TypeScript
- JavaScript
client.unrouteAll();
client.unrouteAll();
Waiting for a request
When you need to wait for a request to be made, you can use client.waitForRequest
.
Use client.waitForRequest
to assert on the request being sent on the server.
- TypeScript
- JavaScript
const requestPromise = client.waitForRequest("**/api");
// Run code that triggers the request, i.e. loading a page
// Await the request
const request = await requestPromise;
// Assert on the request
expect(request.headers.get("Authorization")).toBeDefined();
const requestPromise = client.waitForRequest();
// Run code that triggers the request, i.e. loading a page
// Await the request
const request = await requestPromise;
// Assert on the request
expect(request.headers.get("Authorization")).toBeDefined();
Waiting for a request on the client only or the server only
Similar to Client.route you can scope waiting for requests to the client, server, or both. The default behavior is to wait for requests on both the client and the server. The resolved request is the first request to be made.
- TypeScript
- JavaScript
const requestPromise = client.waitForRequest("**/api", {
// Allows you to scope waiting for requests to the client, server, or both.
// "server-only" | "client-only" | "both"
type: "client-only",
});
// Run code that triggers the request, i.e. loading a page
// Await the request
const request = await requestPromise;
// Assert on the request
expect(request.headers.get("Authorization")).toBeDefined();
const requestPromise = client.waitForRequest();
// Run code that triggers the request, i.e. loading a page
// Await the request
const request = await requestPromise;
// Assert on the request
expect(request.headers.get("Authorization")).toBeDefined();