Response assertion matchers for vitest
npm install @mcansh/vitest-response-matchers
// tsconfig.json
{
"compilerOptions": {
"types": ["@mcansh/vitest-response-matchers/client"]
}
}
// vitest.setup.ts
import "@mcansh/vitest-response-matchers";
toHaveStatus
Check if the response has a specific status code.
let response = new Response("Hello World!");
expect(response).toHaveStatus(200);
toHaveHeader
Check if the response has a specific header.
let response = new Response("Hello World!", {
headers: {
"x-custom-header": "value",
},
});
expect(response).toHaveHeader("x-custom-header", "value");
toHaveCookies
Check if the response has a specific cookie.
let response = new Response("Hello World!", {
headers: {
"Set-Cookie": "name=value; Path=/",
},
});
expect(response).toHaveCookies(["name=value; Path=/"]);
toHaveStatusText
Check if the response has a specific status text.
let response = new Response("Hello World!");
expect(response).toHaveStatusText("OK");
toHaveStrictStatusText
Check if the response has a specific status text according to the HTTP specification. (uses node:http)
let response = new Response("Hello World!", {
status: 400,
statusText: "nah",
});
expect(response).toHaveStrictStatusText("OK"); // fails
toMatchResponse
Check if the response matches another response.
let response = new Response("Hello World!");
expect(response).toMatchResponse(response);
expect(response).toMatchResponse({ status: 200, statusText: "OK" });
toHaveTextBody
Check if the response has a specific text body.
let response = new Response("Hello World!");
expect(response).toHaveTextBody("Hello World!");
toHaveJsonBody
Check if the response has a specific JSON body.
let response = new Response(JSON.stringify({ foo: "bar" }));
expect(response).toHaveJsonBody({ foo: "bar" });
toThrowResponse
Check if a function throws a Response
function throwResponse() {
throw new Response("Hello World!");
}
expect(() => throwResponse()).toThrowResponse(new Response("Hello World!"));