Import
ts
import { createTestClient } from "@noverax/courier/testing"Signature
createTestClient takes the API as a type argument and returns a client typed from it, the mock that answers its endpoints, the history of every call, and resets:
ts
import type { DefinedApi } from "@noverax/courier/define"
import type { TestClient } from "@noverax/courier/testing"
declare function createTestClient<TApi extends DefinedApi = DefinedApi>(): TestClient<TApi>Properties
client— a typed client, with the same dot-notation,.rawand.with()ascreateCaller; streams and channels answer as their mocks saymock—mock(group, method), checked against the API, returns a builder typed by the endpoint's response and variables:resolves,returns,rejects,stream,channel,once,times,delayhistory— every call, in order, with itsgroup,method,variables,resultorerror, and whether it was a stream or a channelreset— clears the mocks and the historyresetMocks— clears the mocks onlyresetHistory— clears the history only
Basic Usage
tests/users.test.ts
import { beforeEach, expect, it } from "vitest"
import { createTestClient } from "@noverax/courier/testing"
import type { api } from "../lib/api"
const { client, mock, history, reset } = createTestClient<typeof api>()
beforeEach(() => reset())
it("lists the users", async () => {
mock("users", "getAll").resolves([{ id: 1, name: "Ada" }])
const users = await client.users.getAll()
expect(users).toEqual([{ id: 1, name: "Ada" }])
expect(history[0]?.method).toBe("getAll")
})A call to an endpoint with no mock throws, naming the mock to write: [courier/testing] No mock for "users.getAll". Call mock("users", "getAll").resolves(data) first.
Related
- Test Client — the whole guide: history entries,
findRequests, testing errors - Mocking — every builder method, and the order mocks answer in
- Mock utilities —
createMockClient, with scenarios, network simulation and recording a real API