Askr documentation
Generated API snapshot

@askrjs/testing

Exports from the declarations published in @askrjs/testing. Signatures reflect the published artifact.

Exports

This entrypoint publishes 18 exports. Use the anchored symbol rows for direct links. Type-only exports are labeled separately from runtime values.

BodyRequestOptionstype

BodyRequestOptions: RequestOptionsBase & {
  method: "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS" | "post" | "put" | "patch" | "delete" | "options";
} & BodyMode

Options for a request that may carry a body, restricted to methods that support one.

createTestClienttype

createTestClient: (target: Injectable, options?: TestClientOptions) => TestClient

Create a {@link TestClient} bound to a target for repeated request injection. The returned client applies shared defaults (base URL, headers, cookie jar, redirect behavior) to every request made through it, and follows redirects automatically unless `redirect` is overridden.

const client = createTestClient(app, { baseUrl: "https://example.com", cookies: true });
const response = await client.get("/users");

createTestRequesttype

createTestRequest: (input: string | URL, options?: InjectOptions) => Request

Build a `Request` for testing from a path or URL and a set of options. Resolves `input` against `options.baseUrl` (defaulting to `https://askr.test/`), appends any `query` parameters, and serializes at most one of `body`, `json`, or `form` into the request body, setting an appropriate `content-type` header when one isn't already present. Throws a `TypeError` if more than one body mode is supplied, if `json` is `undefined`, or if a `GET`/`HEAD` request is given a body.

Formtype

Form: URLSearchParams | Iterable<readonly [string, FormValue]> | Record<string, FormValue | readonly FormValue[] | undefined>

URL-encoded form body data, accepted as `URLSearchParams`, an iterable of entries, or a plain record.

FormValuetype

FormValue: string | number | boolean

A single form field value, coerced to `string` when serialized.

GetHeadOptionstype

GetHeadOptions: RequestOptionsBase & {
  method?: "GET" | "HEAD" | "get" | "head";
} & NoBody

Options for a body-less `GET` or `HEAD` request.

injecttype

inject: { (target: Injectable, request: Request, options?: Pick<InjectOptions, "maxRedirects">): Promise<Response>; (target: Injectable, input: string | URL, options?: InjectOptions): Promise<Response>; }

Inject a single request into a target and return the resulting response, following redirects up to `maxRedirects` hops. Inject a request built from a path/URL and options into a target and return the resulting response, following redirects up to `maxRedirects` hops.

const response = await inject(app, "/users", { method: "GET" });

Injectabletype

Injectable: RequestTarget | RequestHandler

Anything that can receive an injected test request: a {@link RequestTarget} or a {@link RequestHandler}.

InjectOptionstype

InjectOptions: GetHeadOptions | BodyRequestOptions

Options accepted when injecting a request, covering both body-less and body-carrying methods.

Querytype

Query: URLSearchParams | Iterable<readonly [string, QueryValue]> | Record<string, QueryValue | readonly QueryValue[] | undefined>

Query string parameters, accepted as `URLSearchParams`, an iterable of entries, or a plain record.

QueryValuetype

QueryValue: string | number | boolean

A single query string value, coerced to `string` when serialized.

RequestHandlertype

RequestHandler: (request: Request) => Response | Promise<Response>

A function that handles a `Request` and produces a `Response`, synchronously or asynchronously.

RequestTargettype

RequestTarget: any

A target that can receive an injected request directly via a `fetch`-style method.

fetch
fetch(request: Request): Response | Promise<Response>;

TestClienttype

TestClient: any

A reusable HTTP client that injects requests into a target and follows redirects.

cookies
readonly cookies?: TestCookieJar;
request
request(path: string | URL, options?: InjectOptions): Promise<Response>;
get
get(path: string | URL, options?: Omit<GetHeadOptions, "method">): Promise<Response>;
head
head(path: string | URL, options?: Omit<GetHeadOptions, "method">): Promise<Response>;
post
post(path: string | URL, options?: Omit<BodyRequestOptions, "method">): Promise<Response>;
put
put(path: string | URL, options?: Omit<BodyRequestOptions, "method">): Promise<Response>;
patch
patch(path: string | URL, options?: Omit<BodyRequestOptions, "method">): Promise<Response>;
delete
delete(path: string | URL, options?: Omit<BodyRequestOptions, "method">): Promise<Response>;
options
options(path: string | URL, options?: Omit<BodyRequestOptions, "method">): Promise<Response>;

TestClientOptionstype

TestClientOptions: any

Options for constructing a {@link TestClient}.

baseUrl
baseUrl?: string | URL;
headers
headers?: HeadersInit;
cookies
Enable an automatically managed cookie jar (`true`), or supply an existing {@link TestCookieJar}.
redirect
redirect?: RequestRedirect;
maxRedirects
maxRedirects?: number;