From 6dc7f9f7916feb075b3d5c3dd57a9c654459a40e Mon Sep 17 00:00:00 2001 From: imbytecat Date: Sat, 25 Apr 2026 13:31:34 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E5=90=AF=E7=94=A8=20bun=20test=20?= =?UTF-8?q?=E5=B9=B6=E8=A1=A5=20todo=20contract=20=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - package.json 加 "test": "bun test" - todo.contract.test.ts 给 starter 一个可复制的 colocated 测试样板 覆盖 valid input / missing field / wrong type 三种 case --- package.json | 1 + .../api/contracts/todo.contract.test.ts | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 src/server/api/contracts/todo.contract.test.ts diff --git a/package.json b/package.json index b4dd035..5b8ccd8 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "db:studio": "drizzle-kit studio", "dev": "bunx --bun vite dev", "fix": "biome check --write", + "test": "bun test", "typecheck": "tsc --noEmit" }, "dependencies": { diff --git a/src/server/api/contracts/todo.contract.test.ts b/src/server/api/contracts/todo.contract.test.ts new file mode 100644 index 0000000..23a3f78 --- /dev/null +++ b/src/server/api/contracts/todo.contract.test.ts @@ -0,0 +1,20 @@ +import { describe, expect, test } from 'bun:test' +import { createInsertSchema } from 'drizzle-zod' +import { generatedFieldKeys } from '@/server/db/fields' +import { todoTable } from '@/server/db/schema' + +describe('todo insert schema', () => { + const insertSchema = createInsertSchema(todoTable).omit(generatedFieldKeys) + + test('accepts a minimal valid input', () => { + expect(insertSchema.safeParse({ title: 'buy milk' }).success).toBe(true) + }) + + test('rejects missing title', () => { + expect(insertSchema.safeParse({}).success).toBe(false) + }) + + test('rejects non-string title', () => { + expect(insertSchema.safeParse({ title: 42 }).success).toBe(false) + }) +})