Generate Bun test files following x4-mono conventions and patterns
Generate test files for x4-mono source files using Bun's built-in test runner.
The user provides a source file path. If no path given, ask which file to generate tests for.
apps/api/src/routers/*.ts)import { describe, test, expect } from 'bun:test';
import { createCallerFactory } from '../trpc';
import { appRouter } from '../routers';
import type { Context } from '../trpc';
import { createMockDb, createTestUser, TEST_USER_ID } from './helpers';
function createTestContext(overrides: Partial<Context> = {}): Context {
return {
db: createMockDb(),
user: null,
req: new Request('http://localhost:3002'),
...overrides,
};
}
const createCaller = createCallerFactory(appRouter);
For each procedure, test:
apps/api/src/middleware/*.ts)import { describe, test, expect } from 'bun:test';
// Test using app.request()
packages/shared/types/*.ts, packages/shared/utils/validators.ts)import { describe, test, expect } from 'bun:test';
// Import schemas directly
// Use .safeParse() for validation tests
// Use .parse() for default value tests
For each schema, test:
apps/api/src/lib/*.ts, packages/shared/utils/*.ts)import { describe, test, expect } from 'bun:test';
// Direct function import and test
apps/web/src/components/*.tsx)import { describe, test, expect } from 'bun:test';
// Uses @testing-library/react
apps/api/src/routers/foo.ts → apps/api/src/__tests__/foo.test.tsapps/api/src/lib/foo.ts → apps/api/src/__tests__/foo.test.tsapps/api/src/middleware/foo.ts → apps/api/src/__tests__/foo.test.tspackages/shared/utils/foo.ts → packages/shared/utils/foo.test.ts (co-located)packages/shared/types/foo.ts → packages/shared/types/foo.test.ts (co-located)packages/*/src/foo.ts → packages/*/src/__tests__/foo.test.tsbun:test (never jest)describe blocks grouped by function/procedure name// --- functionName ---toMatchObject for partial error matching: rejects.toMatchObject({ code: "NOT_FOUND" })createMockDb helper for database mocks in API testsNOT_FOUND, UNAUTHORIZED, FORBIDDEN, BAD_REQUESTapps/api/src/__tests__/helpers.tsbun test <test-file-path> to verify tests pass