Wiki
the map of any codebase

Module · test

test Module

Responsibility

The test module contains the integration and unit test suite for the ky HTTP client. Tests are organized into topic-specific files (e.g. base-url.ts, body-size.ts, browser.ts, bytes.ts, context.ts) that exercise individual features of the library against a real local HTTP server. Each test file spins up an HTTP test server via createHttpTestServer, registers route handlers, and asserts on ky requests issued against server.url.

Most tests follow a consistent pattern: construct a server, register one or more route handlers with server.get(...) / server.post(...) / server.all(...), perform requests with ky, and assert the response body or recorded server state. A few files (browser.ts) instead bootstrap an ESM-aware test server to run ky inside a browser context.

base-url.ts

Tests resolution of the baseUrl option against various input forms (string, URL, Request, protocol-relative URLs).

  • server (line 6) — the per-test HTTP server created with createHttpTestServer() that registers handlers for /, /foo, /bar, /foo/bar, and /api/v1/users.
  • serverHost (line 7) — new URL(server.url).host, used to construct protocol-relative URLs (//${serverHost}/foo/bar) for baseUrl resolution tests.
  • server (line 51) — a second server instance used by a later test case in the same file.
  • receivedBaseUrl (line 52) — captures the path observed by the server to verify baseUrl was applied correctly.

The assertions cover: baseUrl: false (unsupported but tolerated), empty-string baseUrl, absolute baseUrl with URL and Request inputs, relative path inputs against an absolute baseUrl, and protocol-relative inputs.

body-size.ts

Exercises how ky computes and forwards request body sizes for different payload types. The declarations are local fixtures representing each body kind:

  • buffer / array / view (lines 27–48) — ArrayBuffer, typed arrays, and DataView payloads.
  • full / sub (lines 53–54) — full buffers versus sub-views to verify byte-range handling.
  • blob / file (lines 64–74) — Blob and File payloads.
  • parameters / stream (lines 79–89) — URLSearchParams and streaming bodies.
  • formData, formData1, formData2, size, size1, size2 (lines 94–122) — FormData payloads with multiple measured sizes for comparison.

browser.ts

Bootstraps a browser-based test harness using a custom ESM test server.

  • DIST_DIR (line 23) — path to the built ky distribution served to the browser.
  • createEsmTestServer (line 24) — factory for an HTTP server capable of serving ESM modules.
  • KY_SCRIPT (line 34) — the inline script that imports ky in the page.
  • addKyScriptToPage (line 41) — helper that injects KY_SCRIPT into a page under test.
  • server (line 46) — typed as ExtendedHttpTestServer, shared across browser test cases.

bytes.ts

  • supportsBytes (line 6) — feature-detection constant gating tests that depend on the Response#bytes() method.

context.ts

Tests ky.extend and per-request context propagation (e.g. shared options merged across instances).

  • server (line 8), server (line 64), server (line 98) — independent createHttpTestServer(t) instances scoped to separate tests; the AVA t argument is passed so the server is torn down with the test.
  • requestCount (line 9) — counter used to assert how many requests reached the server.
  • context (line 19) — a context object asserted to flow through ky hooks/options.
  • baseApi (line 69) and extendedApi (line 84) — a base ky instance and a child created via baseApi.extend(...), used to verify option inheritance. The server at line 64 responds to GET / with {success: true} for these assertions.

Declarations

test/base-url.ts
constserver:6
constserverHost:7
constserver:51
constreceivedBaseUrl:52
test/body-size.ts
constbuffer:27
constarray:32
constarray:37
constarray:42
constbuffer:47
constview:48
constfull:53
constsub:54
constblob:64
constblob:69
constfile:74
constparameters:79
constparameters:84
conststream:89
constformData:94
constformData:99
constsize:101
constformData1:106
constsize1:108
constformData2:110
constsize2:113
constformData:119
constsize:122
test/browser.ts
constDIST_DIR:23
constcreateEsmTestServer:24
constKY_SCRIPT:34
constaddKyScriptToPage:41
constserver:46
test/bytes.ts
constsupportsBytes:6
test/context.ts
constserver:8
constrequestCount:9
constcontext:19
constserver:64
constbaseApi:69
constextendedApi:84
constserver:98
constrequestCount:99
constcontext:109
constbeforeRequestCallCount:110
constbeforeRetryCallCount:111
constserver:137
constserver:150
constbaseApi:155
constextendedApi:163
constcontext:175
test/fetch.ts
constfixture:5
test/formdata-searchparams.ts
constserver:6
consturl:9
constbody:11
constformData:25
constwasProgressCalled:29
constlastProgress:30
constresponse:32
constserver:58
constrequestCount:60
constreceivedBoundaries:61

Cited sources· 4