Skip to content

API ReferencecreateTestClient

createTestClient

An in-memory client for tests: typed like your API, answered by mocks, recording every call — no HTTP.

1 min readdirect mode

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, .raw and .with() as createCaller; streams and channels answer as their mocks say
  • mock — 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, delay
  • history — every call, in order, with its group, method, variables, result or error, and whether it was a stream or a channel
  • reset — clears the mocks and the history
  • resetMocks — clears the mocks only
  • resetHistory — 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.

  • 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