# @askrjs/testing

> Published API exports for @askrjs/testing.

Source: [https://askrjs.com/docs/reference/api/testing/root](https://askrjs.com/docs/reference/api/testing/root)

Status: stable. Packages: @askrjs/testing.

**Published packages are authoritative.** Examples may lag behind a published contract. When guidance differs, verify the exports and TypeScript declarations in your installed package, then file an issue.

## Exports

This entrypoint publishes 18 exports from the declarations shipped by @askrjs/testing.

### `BodyRequestOptions`

```ts
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.

### `createTestClient`

```ts
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.

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

### `createTestCookieJar`

```ts
createTestCookieJar: () => TestCookieJar
```

Create an in-memory {@link TestCookieJar} backed by `tough-cookie`, suitable
for use as the `cookies` option of a {@link TestClient}.

### `createTestRequest`

```ts
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.

### `Form`

```ts
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.

### `FormValue`

```ts
FormValue: string | number | boolean
```

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

### `GetHeadOptions`

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

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

### `inject`

```ts
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.

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

### `Injectable`

```ts
Injectable: RequestTarget | RequestHandler
```

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

### `InjectOptions`

```ts
InjectOptions: GetHeadOptions | BodyRequestOptions
```

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

### `Query`

```ts
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.

### `QueryValue`

```ts
QueryValue: string | number | boolean
```

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

### `RequestHandler`

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

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

### `RequestTarget`

```ts
RequestTarget: any
```

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

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

### `TestClient`

```ts
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>;

### `TestClientOptions`

```ts
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;

### `TestCookie`

```ts
TestCookie: any
```

A cookie as read back from a {@link TestCookieJar}.

- `name`: name: string;

- `value`: value: string;

- `domain`: domain?: string;

- `path`: path?: string;

- `expires`: expires?: Date | "Infinity";

- `httpOnly`: httpOnly: boolean;

- `secure`: secure: boolean;

- `sameSite`: sameSite?: "strict" | "lax" | "none";

### `TestCookieJar`

```ts
TestCookieJar: any
```

A cookie jar used to persist and replay cookies across injected requests.

- `setCookie`: setCookie(cookie: string, url: string | URL): Promise<void>;

- `getCookies`: getCookies(url: string | URL): Promise<TestCookie[]>;

- `clear`: clear(): Promise<void>;

## Documentation navigation

[Previous](https://askrjs.com/docs/reference/api/server/testing/index.md) | [Next](https://askrjs.com/docs/reference/api/themes/root/index.md)
