Volt v0.1 · TypeScript APIs on Bun · Get started
Docs/Guides/Testing

Testing

Test the same app used by the CRUD tutorial without opening a port.

Reuse the Tasks API

Start with the complete Tasks API tutorial saved as index.ts. createApp() gives each test a fresh task store. Install Bun types if your editor needs them:

bash
bun add --dev @types/bun

Complete test file

Save this as tasks.test.ts beside index.ts:

tasks.test.ts
import { expect, test } from 'bun:test';
import { createTestClient } from 'voltts';
import { createApp } from './index';

test('create, read, update, and delete a task', async () => {
  const client = createTestClient(createApp());
  const created = await client.post('/tasks', {
    body: { title: 'Learn Volt', done: false },
  });
  expect(created.status).toBe(201);
  const task = await created.json();
  expect((await client.get(`/tasks/${task.id}`)).status).toBe(200);
  const updated = await client.put(`/tasks/${task.id}`, {
    body: { title: 'Ship an API', done: true },
  });
  expect((await updated.json()).done).toBe(true);
  expect((await client.delete(`/tasks/${task.id}`)).status).toBe(204);
  expect((await client.get(`/tasks/${task.id}`)).status).toBe(404);
});

test('reject invalid input', async () => {
  const client = createTestClient(createApp());
  const response = await client.post('/tasks', { body: { title: 'x', done: false } });
  expect(response.status).toBe(400);
});

Run the tests

bash
bun test tasks.test.ts

createTestClient returns native Responses; inspect status, headers, and await response.json(). Its body option serializes plain objects as JSON, and query appends URL parameters. It does not start a server. Use a real listener for tests of network connections, WebSocket upgrades, or Bun's socket-level limits.