UNPKG

@edge-runtime/jest-expect

Version:

Custom matchers for Jest's expect to help test Request/Response instances

48 lines (47 loc) 2.31 kB
export type StatusCode = 100 | 101 | 102 | 103 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 226 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 421 | 422 | 423 | 424 | 425 | 426 | 428 | 429 | 431 | 451 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 510 | 511; export type Status = 'Informational' | 'Successful' | 'Redirection' | 'Client Error' | 'Server Error'; /** `toHaveStatusCode` parameters */ export type StatusParams = [status: StatusCode | Status]; export type JSONType = Record<string, unknown> | unknown[] | string | number | boolean | null; /** `toHaveJSONBody` parameters */ export type JSONBodyParams = [body: JSONType]; interface SharedMatchers<R = unknown> { /** * @see * [@edge-runtime/jest-environment#toHaveJSONBody](https://edge-runtime.vercel.app/packages/jest-environment#tohavejsonbody) */ toHaveJSONBody(...args: JSONBodyParams): R; /** * @see * [@edge-runtime/jest-environment#toHaveTextBody](https://edge-runtime.vercel.app/packages/jest-environment#tohavetextbody) */ toHaveTextBody(body: string): R; } export interface ResponseMatchers<R = unknown> extends SharedMatchers<R> { /** * @description * Assert whether a response has a specific status code or status type. * @example * * expect(new Response("OK", { status: 200 })).toHaveStatus(200) * expect(new Response("OK", { status: 200 })).toHaveStatus("Successful") * expect(new Response("Internal Server Error"), {status: 500}).toHaveStatus("Server Error") * expect(new Response("OK")).not.toHaveStatus(201) * @see * [@edge-runtime/jest-environment#toHaveStatus](https://edge-runtime.vercel.app/packages/jest-environment#tohavestatus) */ toHaveStatus(...args: StatusParams): R; } export interface RequestMatchers<R = unknown> extends SharedMatchers<R> { } declare global { namespace jest { interface Expect extends RequestMatchers, ResponseMatchers { } interface Matchers<R> extends RequestMatchers<R>, ResponseMatchers<R> { } interface InverseAsymmetricMatchers extends RequestMatchers, ResponseMatchers { } } } export {};