Run, write, and debug LLM Gateway unit and e2e tests — pnpm test:unit, pnpm test:e2e, TEST_MODELS scoping, the gateway test harness, seeded accounts and test tokens, pinning a provider with x-no-fallback, e2e proxies, and test fixtures or cleanup. Use when running or adding *.spec.ts or *.e2e.ts files, reproducing a provider failure locally, or when tests fail only locally or intermittently.
1,773 stars
0 votes
0 copies
2 views
Added September 29, 2026
ai-agentsbashnodetestingapidatabase
Works with
cli
api
Security analysis
B88/100
mediumUses curl or wget to download content
mediumInstalls packages at runtime which could introduce malicious dependencies
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Testing?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/theopenco-testing)
---
name: testing
description: Run, write, and debug LLM Gateway unit and e2e tests — pnpm test:unit, pnpm test:e2e, TEST_MODELS scoping, the gateway test harness, seeded accounts and test tokens, pinning a provider with x-no-fallback, e2e proxies, and test fixtures or cleanup. Use when running or adding *.spec.ts or *.e2e.ts files, reproducing a provider failure locally, or when tests fail only locally or intermittently.
---
# Testing
Run commands from the repository root.
- `pnpm test:unit` runs `*.spec.ts` files in parallel; each vitest worker gets its own database clone and Redis logical database (`REDIS_DB`). Every new ioredis client takes `db: Number(process.env.REDIS_DB) || 0`.
- `pnpm test:e2e` runs `*.e2e.ts` files sequentially.
- Run separate suites and e2e files one at a time: they share ports, databases, and process state.
## Before the first run
- Fresh worktree: `pnpm install` with the pinned pnpm (`packageManager` in `package.json`).
- Isolated database: start the worktree's own stack with the `local-stack` skill, export `TEST_DATABASE_URL`, run `pnpm push-test`. A run against the shared stack races other worktrees' schema pushes and seeds.
- Run vitest from the repo root, never a package dir (`vitest.config.mts` loads `./vitest/global-setup.ts`): `pnpm test:unit`, or `pnpm exec vitest run <path>` after `pnpm build:core` (which `test:unit` runs itself).
- Typecheck per package (`pnpm exec tsc --noEmit -p <app-or-package>/tsconfig.json`; no package has a `typecheck` script), never workspace-wide.
## Local credentials
- `test-token` authenticates against the local API and gateway. `test-token-no-retention` belongs to a seeded org with `retentionLevel: "none"`.
- Seeded users log in with their email as password, e.g. `admin@example.com` / `admin@example.com`. Accounts come from `packages/db/src/seed.ts`: `admin@example.com` (owns "Test Organization", "Test No Retention Organization", and a DevPass Pro workspace), `enterprise@example.com`, `developer@example.com` (project-scoped RBAC), and bulk demo users.
## Reproduce a provider failure
Pin the provider and disable fallback, or a healthy provider masks the error:
```bash
curl -N http://localhost:4001/v1/chat/completions \
-H "Authorization: Bearer test-token" -H "x-no-fallback: true" \
-d '{"model":"<provider>/<model>","stream":true,"messages":[{"role":"user","content":"hi"}]}'
```
The gateway caches responses, errors included, keyed on the request body; vary
the prompt when retesting.
## E2E
`*.e2e.ts` is only for tests that call real upstream providers. Tests with a
local or mocked upstream are `*.spec.ts`, even across the full gateway path.
Parameterized chat coverage lives in `apps/gateway/src/chat-*.e2e.ts` (cases run
concurrently unless `CONCURRENT_TESTS=false`); isolated tests in
`apps/gateway/src/api-individual.e2e.ts`.
Options:
- `TEST_MODELS` — comma-separated `provider/model-id` list; overrides `test: "skip"`, so metadata-driven assertions still apply. Add `:region` (e.g. `alibaba/deepseek-v3.2:cn-beijing`) to scope to one region; a bare entry covers every region.
- `FULL_MODE` — include free models and per-effort cases.
- `LOG_MODE` — log responses.
- `TEST_WEB_SEARCH` — run `chat-websearch.e2e.ts`; each case bills a real search, so scope it with `TEST_MODELS`.
- `CONTEXT_ROUTING_TEST` — run `chat-context-routing.e2e.ts`: ~350k-token prompts against every keyed deployment of its models, and bare-id routing past short-window deployments. Costs several dollars per run; run that file directly rather than the whole suite.
For mapping-only changes, the only relevant result is a run scoped to those
mappings (`TEST_MODELS="alibaba/glm-5.2" FULL_MODE=true pnpm test:e2e`). Run the
whole command, not individual `*.e2e.ts` files, and add only mappings whose run
is fully green after the last fix. Failures outside the selected mappings do not
affect acceptance.
To run through a proxy, export the provider's base-URL var (`LLM_OPENAI_BASE_URL`,
…) with its `LLM_*_API_KEY`; `beforeAllHook` stamps it onto the seeded provider
key. An `http://` base URL also needs `ALLOW_INSECURE_PROVIDER_URLS=true`.
e2e does not run in CI; run it locally with your own credentials.
## Writing tests
- `createGatewayApiTestHarness()` (`apps/gateway/src/test-utils/gateway-api-test-harness.ts`) deletes and re-seeds all data before every test; the shared org is `retentionLevel: "retain"`, `plan: "pro"`, `credits: "100.00"`. Mutate shared state freely without restoring it, and set any value a test depends on explicitly or rely on those defaults.
- Gateway unit tests load the root `.env`, which holds real provider keys. A test that needs a provider without credentials saves and deletes `LLM_<PROVIDER>_API_KEY` (and `_BASE_URL`), restoring them in `finally`.
- API specs load it too (`apps/api/src/index.ts` imports `dotenv/config`), so code that calls a third party with env credentials reaches the real service unless the spec stubs it. `vitest/unit-worker-setup.ts` blanks the Resend variables for every unit test; a spec that exercises Resend sets its own with `vi.stubEnv` and stubs `fetch`.
- Build credential-shaped fixtures at runtime (`["sk", "live", "..."].join("_")`) so push protection accepts the branch. Assert on booleans, never equality with a credential: CI test artifacts are public and unmasked.
- Delete cascade-linked tables sequentially, children before parents. `deleteAll()` keeps the catalogue tables, so a spec that seeds `model`/`model_provider_mapping` rows removes them itself, with ids distinct from the real catalogue.
- Source-loading Node subprocesses using `--import tsx` set `TSX_TSCONFIG_PATH` to the owning app's `tsconfig.json`; the root config does not resolve app `@/` aliases.
- Fake Resend's contact lookup, create, update and delete, and a contact's topic and segment reads, in `apps/api` specs with `fakeResendContacts` (`apps/api/src/testing-resend.ts`) on `globalThis.fetch`. Topic and segment writes are faked in `public-newsletter.spec.ts` only.
- Keep logs and scratch files under `.context/`; `/tmp` is shared with other agents.