Fetch
Matchers for fetch related objects, such as Response, Request, and Headers.
toBeOK()
Use .toBeOK() to assert that a Response object has an OK status (status code in the range 200-299).
it("is an OK response", () => {
const response = new Response(null, { status: 200 });
expect(response).toBeOK();
});
toHaveStatus(expected)
Use .toHaveStatus(expected) to assert that a Response object has the expected status code.
it("has the expected status code", () => {
const response = new Response(null, { status: 404 });
expect(response).toHaveStatus(404);
});
toHaveStatusGroup(expected)
Use .toHaveStatusGroup(expected) to assert that a Response object has a specific status group (1-5, 1xx-5xx, "informational", "successful", "redirection", "client error", "server error").
it("has the expected status group", () => {
const response = new Response(null, { status: 404 });
expect(response).toHaveStatusGroup(4);
// or
expect(response).toHaveStatusGroup("4xx");
// or
expect(response).toHaveStatusGroup("client error");
});
toHaveHeader(name, value?)
Use .toHaveHeader(name, value?) to assert that a Response, Request, or Headers object has the specified header. If value is provided, it will also check that the header value matches.
it("has the expected header", () => {
const response = new Response(null, {
headers: { "Content-Type": "application/json" },
});
expect(response).toHaveHeader("Content-Type", "application/json");
});
toHaveMethod(expected)
Use .toHaveMethod(expected) to assert that a Request object has the expected HTTP method.
it("has the expected HTTP method", () => {
const request = new Request("https://example.com", { method: "POST" });
expect(request).toHaveMethod("POST");
});
toHaveURL(expected)
Use .toHaveURL(expected) to assert that a Response or Request object has the expected URL.
it("has the expected URL", () => {
const request = new Request("https://example.com/");
expect(request).toHaveURL("https://example.com/");
});
toHaveTextBody(expected)
Use .toHaveTextBody(expected) to assert that a Response or Request object has the expected body text.
it("has the expected body text", async () => {
const response = new Response("Hello, world!");
await expect(response).toHaveTextBody("Hello, world!");
});
This assertion is asynchronous, so must be awaited.
toHaveJSONBody(expected)
Use .toHaveJSONBody(expected) to assert that a Response or Request object has the expected JSON body, using deep equality.
.toHaveJSONBodyStrict(expected) is also available to use strict deep equality.
it("has the expected JSON body", async () => {
const response = Response.json({ message: "Hello, world!" });
await expect(response).toHaveJSONBody({ message: "Hello, world!" });
});
This assertion is asynchronous, so must be awaited.
toHaveFormDataBody(expected)
Use .toHaveFormDataBody(expected) to assert that a Response or Request object has the expected FormData body, using formDataEquality.
it("has the expected FormData body", async () => {
const formData = new FormData();
formData.append("key", "value");
const response = new Response(formData);
await expect(response).toHaveFormDataBody(formData);
});
This assertion is asynchronous, so must be awaited.
toBeRedirected()
Use .toBeRedirected() to assert that fetch followed a redirect and returned a response with its redirected property set to true. This example requires an internet connection.
it("follows a redirect", async () => {
const response = await fetch("https://httpbin.org/redirect/1");
expect(response).toBeRedirected();
});
toHaveResponseType(expected)
Use .toHaveResponseType(expected) to assert that a Response object has the expected type.
it("has the expected response type", () => {
const response = new Response();
expect(response).toHaveResponseType("default");
});
toHaveSearchParam(name, value?)
Use .toHaveSearchParam(name, value?) to assert that a URLSearchParams, URL, Response, or Request object has the specified search parameter. If value is provided, it will also check that the parameter value matches.
it("has the expected search parameter", () => {
const url = new URL("https://example.com?foo=bar");
expect(url).toHaveSearchParam("foo", "bar");
});
toBeAborted()
Use .toBeAborted() to assert that an AbortSignal is aborted, or that a Request object has an aborted signal.
it("is aborted", () => {
const controller = new AbortController();
expect(controller.signal).not.toBeAborted();
controller.abort();
expect(controller.signal).toBeAborted();
});