pnpm generate command.
Scaffolding Overview
Auto Generation
model.test.ts creation Basic structure included
Bootstrap Included
Auto test environment setup Immediately executable
CRUD Tests
Basic test skeleton Customizable
Type Safe
Model integration Auto-completion support
Test File Generation
Command
pnpm generate
{model}.test.ts files are automatically generated.
Generation location:
api/src/models/
βββ user.model.ts
βββ user.model.test.ts # Auto-generated
βββ post.model.ts
βββ post.model.test.ts # Auto-generated
Generated File Structure
// api/src/models/user.model.test.ts
import { bootstrap, test } from "sonamu/test";
import { expect, vi } from "vitest";
import { UserModel } from "./user.model";
// Initialize test environment
bootstrap(vi);
test("Create user", async () => {
// Write test code
const [userId] = await UserModel.save([{
username: "john",
email: "john@example.com",
password: "password123",
}]);
const user = await UserModel.findById("A", userId);
expect(user.id).toBeGreaterThan(0);
expect(user.username).toBe("john");
});
test("Get user", async () => {
// Create test data
const [userId] = await UserModel.save([{
username: "jane",
email: "jane@example.com",
password: "password123",
}]);
const user = await UserModel.findById("A", userId);
// Test retrieval
const found = await UserModel.findById("A", user.id);
expect(found.id).toBe(user.id);
expect(found.username).toBe("jane");
});
Running Tests
Run Single File
# Run only specific model test
pnpm vitest user.model.test.ts
Run All Tests
# Run all tests
pnpm test
# Or
pnpm vitest
Watch Mode
# Auto re-run on file changes
pnpm vitest --watch
Test Writing Guide
CRUD Test Pattern
import { NotFoundException } from "sonamu";
import { bootstrap, test } from "sonamu/test";
import { expect, vi } from "vitest";
import { PostModel } from "./post.model";
bootstrap(vi);
// Create
test("Create post", async () => {
const [postId] = await PostModel.save([{
title: "Test Post",
content: "Test Content",
author_id: 1,
}]);
const post = await PostModel.findById("A", postId);
expect(post.id).toBeGreaterThan(0);
expect(post.title).toBe("Test Post");
});
// Read
test("Get post", async () => {
// Create test data
const [postId] = await PostModel.save([{
title: "Test Post",
content: "Test Content",
author_id: 1,
}]);
const post = await PostModel.findById("A", postId);
// Retrieve
const found = await PostModel.findById("A", post.id);
expect(found.id).toBe(post.id);
expect(found.title).toBe("Test Post");
});
// Update
test("Update post", async () => {
// Create test data
const [postId] = await PostModel.save([{
title: "Original Title",
content: "Original Content",
author_id: 1,
}]);
const post = await PostModel.findById("A", postId);
// Update
await PostModel.getPuri("w").table("posts").where("id", post.id).update({
title: "Updated Title",
});
// Verify
const updated = await PostModel.findById("A", post.id);
expect(updated.title).toBe("Updated Title");
expect(updated.content).toBe("Original Content"); // Not changed
});
// Delete
test("Delete post", async () => {
// Create test data
const [postId] = await PostModel.save([{
title: "Test Post",
content: "Test Content",
author_id: 1,
}]);
const post = await PostModel.findById("A", postId);
// Delete
await PostModel.del([post.id]);
// Verify
// The generated findById throws NotFoundException for a missing row
await expect(PostModel.findById("A", post.id)).rejects.toThrow(NotFoundException);
});
Relationship Tests
test("User and post relationship", async () => {
// Create user
const [userId] = await UserModel.save([{
username: "author",
email: "author@example.com",
password: "password",
}]);
const user = await UserModel.findById("A", userId);
// Create post
const [postId] = await PostModel.save([{
title: "User's Post",
content: "Content",
author_id: user.id,
}]);
const post = await PostModel.findById("A", postId);
// Verify relationship
expect(post.author_id).toBe(user.id);
// Querying with Subset "C" includes author info
const fullPost = await PostModel.findById("A", post.id);
expect(fullPost.author?.username).toBe("author");
});
Error Tests
test("Duplicate email validation", async () => {
// Create first user
await UserModel.save([{
username: "user1",
email: "duplicate@example.com",
password: "password",
}]);
// Try to create with same email
await expect(
UserModel.save([{
username: "user2",
email: "duplicate@example.com",
password: "password",
}]),
).rejects.toThrow("Email already exists");
});
test("Get non-existent post", async () => {
await expect(PostModel.findById("A", 999999)).rejects.toThrow(NotFoundException);
});
Structuring Tests
Grouping Multiple Tests
import { bootstrap, test } from "sonamu/test";
import { expect, vi } from "vitest";
import { describe } from "vitest";
import { UserModel } from "./user.model";
bootstrap(vi);
describe("User creation", () => {
test("Normal creation", async () => {
const [userId] = await UserModel.save([{
username: "john",
email: "john@example.com",
password: "password",
}]);
const user = await UserModel.findById("A", userId);
expect(user.id).toBeGreaterThan(0);
});
test("Duplicate email validation", async () => {
await UserModel.save([{
username: "user1",
email: "duplicate@example.com",
password: "password",
}]);
await expect(
UserModel.save([{
username: "user2",
email: "duplicate@example.com",
password: "password",
}]),
).rejects.toThrow();
});
});
describe("User retrieval", () => {
test("Get by ID", async () => {
const [userId] = await UserModel.save([{
username: "test",
email: "test@example.com",
password: "password",
}]);
const user = await UserModel.findById("A", userId);
const found = await UserModel.findById("A", user.id);
expect(found.id).toBe(user.id);
});
test("Get non-existent user", async () => {
await expect(UserModel.findById("A", 999999)).rejects.toThrow();
});
});
Best Practices
1. Test Isolation
Each test should run independently.// β
Correct: Each test is independent
test("Test 1", async () => {
const [userId] = await UserModel.save([{
/* ... */
}]);
const user = await UserModel.findById("A", userId);
// Auto rollback after test ends
});
test("Test 2", async () => {
// Starts with clean DB state
const [userId] = await UserModel.save([{
/* ... */
}]);
const user = await UserModel.findById("A", userId);
});
// β Wrong: Dependencies between tests
let sharedUserId: number;
test("Test 1", async () => {
const [userId] = await UserModel.save([{
/* ... */
}]);
const user = await UserModel.findById("A", userId);
sharedUserId = user.id; // β Shared with other tests
});
test("Test 2", async () => {
const user = await UserModel.findById("A", sharedUserId); // β Fails!
});
2. Clear Test Names
// β
Correct
test("Error thrown when creating user with duplicate email", async () => {
// ...
});
// β Wrong
test("Test 1", async () => {
// ...
});
3. Arrange-Act-Assert Pattern
test("Update post", async () => {
// Arrange
const [postId] = await PostModel.save([{
title: "Original",
content: "Content",
author_id: 1,
}]);
const post = await PostModel.findById("A", postId);
// Act
await PostModel.getPuri("w").table("posts").where("id", post.id).update({
title: "Updated",
});
// Assert
const updated = await PostModel.findById("A", post.id);
expect(updated.title).toBe("Updated");
});
Cautions
Cautions when writing tests: 1. Donβt modify auto-generated files: Will be overwritten on
regeneration 2. Independent tests: No dependencies between tests 3. Transaction based:
Auto rollback after each test 4. Async required: All tests must be async functions 5. Clear
names: Clearly state what the test verifies
Next Steps
Test Structure
bootstrap, test, testAs
Test Helpers
Fixtures and utilities
Vitest Docs
Vitest guide
Model System
Understanding Models