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 withcreateHttpTestServer()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) forbaseUrlresolution 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 verifybaseUrlwas 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, andDataViewpayloads.full/sub(lines 53–54) — full buffers versus sub-views to verify byte-range handling.blob/file(lines 64–74) —BlobandFilepayloads.parameters/stream(lines 79–89) —URLSearchParamsand streaming bodies.formData,formData1,formData2,size,size1,size2(lines 94–122) —FormDatapayloads 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 builtkydistribution 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 importskyin the page.addKyScriptToPage(line 41) — helper that injectsKY_SCRIPTinto a page under test.server(line 46) — typed asExtendedHttpTestServer, shared across browser test cases.
bytes.ts
supportsBytes(line 6) — feature-detection constant gating tests that depend on theResponse#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) — independentcreateHttpTestServer(t)instances scoped to separate tests; the AVAtargument 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 throughkyhooks/options.baseApi(line 69) andextendedApi(line 84) — a basekyinstance and a child created viabaseApi.extend(...), used to verify option inheritance. The server at line 64 responds toGET /with{success: true}for these assertions.
Declarations
| const | server | :6 |
| const | serverHost | :7 |
| const | server | :51 |
| const | receivedBaseUrl | :52 |
| const | buffer | :27 |
| const | array | :32 |
| const | array | :37 |
| const | array | :42 |
| const | buffer | :47 |
| const | view | :48 |
| const | full | :53 |
| const | sub | :54 |
| const | blob | :64 |
| const | blob | :69 |
| const | file | :74 |
| const | parameters | :79 |
| const | parameters | :84 |
| const | stream | :89 |
| const | formData | :94 |
| const | formData | :99 |
| const | size | :101 |
| const | formData1 | :106 |
| const | size1 | :108 |
| const | formData2 | :110 |
| const | size2 | :113 |
| const | formData | :119 |
| const | size | :122 |
| const | DIST_DIR | :23 |
| const | createEsmTestServer | :24 |
| const | KY_SCRIPT | :34 |
| const | addKyScriptToPage | :41 |
| const | server | :46 |
| const | supportsBytes | :6 |
| const | server | :8 |
| const | requestCount | :9 |
| const | context | :19 |
| const | server | :64 |
| const | baseApi | :69 |
| const | extendedApi | :84 |
| const | server | :98 |
| const | requestCount | :99 |
| const | context | :109 |
| const | beforeRequestCallCount | :110 |
| const | beforeRetryCallCount | :111 |
| const | server | :137 |
| const | server | :150 |
| const | baseApi | :155 |
| const | extendedApi | :163 |
| const | context | :175 |
| const | fixture | :5 |
| const | server | :6 |
| const | url | :9 |
| const | body | :11 |
| const | formData | :25 |
| const | wasProgressCalled | :29 |
| const | lastProgress | :30 |
| const | response | :32 |
| const | server | :58 |
| const | requestCount | :60 |
| const | receivedBoundaries | :61 |