@thisisagile/easy-test
Version:
Straightforward library for testing microservices built with @thisisagile/easy
60 lines (59 loc) • 2.07 kB
JavaScript
import {
match
} from "../chunk-LN24S2PC.mjs";
import "../chunk-6EKWTLNQ.mjs";
import "../chunk-O475TEG2.mjs";
// src/matchers/HttpMatchers.ts
var toHaveStatus = (res, code) => match(res).undefined((r) => r, "Response is unknown.").undefined(
(r) => r?.status?.id,
() => "Response does not have a status code"
).not(
(r) => r.status.id === code,
(r) => `Response does not have code '${code}', but has code '${r.status.id}' instead.`
).else(`Response does have status code '${code}'.`);
var toBeOk = (res) => toHaveStatus(res, 200);
var toBeCreated = (res) => toHaveStatus(res, 201);
var toHaveNoContent = (res) => toHaveStatus(res, 204);
var toBeBadRequest = (res) => toHaveStatus(res, 400);
var toBeUnauthorized = (res) => toHaveStatus(res, 401);
var toBeForbidden = (res) => toHaveStatus(res, 403);
var toBeNotFound = (res) => toHaveStatus(res, 404);
var toBeConflict = (res) => toHaveStatus(res, 409);
var toBeInternalServerError = (res) => toHaveStatus(res, 500);
var toBeBadGateway = (res) => toHaveStatus(res, 502);
var toBeOkWithItems = (res, length) => match(res).undefined((r) => r.status?.id, "Response did not have a status").not(
(r) => r.status.id === 200,
(r) => `Response did not have status '200'. It had status '${r.status.id}' instead.`
).undefined((r) => r?.body?.data?.items, `Response did not have any items.`).not(
(r) => (r?.body?.data?.itemCount ?? 0) >= length,
(r) => `Response did not have at least ${length} items. It only had ${r?.body?.data?.itemCount ?? 0} items.`
).else(`Response had status 200 and at least ${length} items`);
expect.extend({
toBeOk,
toBeOkWithItems,
toBeCreated,
toHaveNoContent,
toBeNotFound,
toBeBadRequest,
toBeUnauthorized,
toBeForbidden,
toBeConflict,
toBeInternalServerError,
toHaveStatus,
toBeBadGateway
});
export {
toBeBadGateway,
toBeBadRequest,
toBeConflict,
toBeCreated,
toBeForbidden,
toBeInternalServerError,
toBeNotFound,
toBeOk,
toBeOkWithItems,
toBeUnauthorized,
toHaveNoContent,
toHaveStatus
};
//# sourceMappingURL=HttpMatchers.mjs.map