Testing
Pixelflare has two sets of tests:
- Unit tests for the API, written with Vitest. They're quick, need no Cloudflare account, and run in CI on every pull request
- End-to-end tests that upload, serve, transform and delete real images on a running instance
Unit Tests
The unit tests live in packages/api/src/__tests__, in folders that mirror the source: routes/, services/, lib/, middleware/, queue/ and mcp/.
pnpm test # every package's tests
pnpm --filter @pixflare/api test # just the API
pnpm --filter @pixflare/api test:coverage # with a coverage reportTo run one file, or one test by name, use Vitest from the API package:
cd packages/api
npx vitest run src/__tests__/routes/tags.test.ts
npx vitest run -t "creates a tag"Writing Unit Tests
Put each test in the matching folder, so src/services/tags.ts is tested by src/__tests__/services/tags.test.ts. The @ alias points at src, so imports look the same as in the code.
Tests never talk to Cloudflare. test-helpers.ts gives you fake bindings and data, like createMockEnv(), createMockDb(), createMockAuth(), createMockLogger() and createMockImage().
Since routes only call services, and services do the work, each layer is tested on its own:
- Route tests mount the router on a fresh Hono app, mock the services, and check the responses
- Service tests mock the database, and check the logic
A route test looks something like this:
import { OpenAPIHono } from '@hono/zod-openapi';
import { describe, it, expect, vi } from 'vitest';
import { createMockEnv, createMockAuth, createMockLogger } from '@/__tests__/utils/test-helpers';
import { errorHandler } from '@/middleware/error-handler';
import { tagsRouter } from '@/routes/api/tags';
// Swap out the database, auth and services
vi.mock('@/lib/db', () => ({ getDb: vi.fn(() => ({})) }));
vi.mock('@/middleware/auth', () => ({ requireScopes: () => async (_c: any, next: any) => next() }));
vi.mock('@/services/images', () => ({ ensureUser: vi.fn() }));
const mockListTags = vi.fn();
vi.mock('@/services/tags', () => ({ listTags: (...args: any[]) => mockListTags(...args) }));
describe('GET /tags', () => {
it('lists your tags', async () => {
const app = new OpenAPIHono<any>();
app.use('*', async (c, next) => {
c.set('auth', createMockAuth());
c.set('logger', createMockLogger());
await next();
});
app.route('/tags', tagsRouter);
app.onError(errorHandler as any);
mockListTags.mockResolvedValue([{ tag_slug: 'nature', count: 5 }]);
const res = await app.fetch(new Request('http://localhost/tags'), createMockEnv());
expect(res.status).toBe(200);
});
});New API behaviour should come with a test. Bug fixes are best with one too, which fails before the fix and passes after.
End-to-End Tests
The e2e suite runs a full image lifecycle against a real Pixelflare instance. It generates test images, uploads them, requests them through every URL pattern, variant and format, checks the files that come back are exactly right, then deletes everything again.
To run it:
- Create an API key with read, write and delete access, from Settings → API keys on the instance you're testing
- Install the suite's dependencies, which also creates
packages/e2e/.env:bashmake e2e-setup - In
packages/e2e/.env, setPIXFLARE_API_TOKENto your key, andPIXFLARE_API_URLto your instance - Run it:bash
make e2e
The .env file also turns on optional checks, like custom domains, custom transforms, encryption and AI classification, so you only test what your instance has.
WARNING
The suite creates and deletes real images, albums and tags. They're all prefixed with e2e-test, but use a test account rather than one with images you care about.
Other Checks
CI also lints, type checks, builds and validates everything, see CI/CD Pipeline. To run the same checks before you push:
pnpm hold-my-beer