TestGen·AI3 free · BYOK unlimited
Unit tests in seconds
Paste any function or module. AI generates complete, runnable unit tests using the right framework for your language. No account needed.
See it in action
Source code → complete test suite
Paste any function, class, or module and get runnable tests in seconds.
utils/cart.ts
// utils/cart.ts
export interface CartItem {
id: string;
name: string;
price: number;
quantity: number;
}
export function calculateTotal(items: CartItem[]): number {
return items.reduce((sum, item) => sum + item.price * item.quantity, 0);
}
export function applyDiscount(total: number, discountPct: number): number {
if (discountPct < 0 || discountPct > 100) {
throw new Error("Discount must be between 0 and 100");
}
return total * (1 - discountPct / 100);
}
export function removeItem(items: CartItem[], id: string): CartItem[] {
return items.filter((item) => item.id !== id);
}utils/cart.test.tsAI generated
// utils/cart.test.ts
import { calculateTotal, applyDiscount, removeItem, CartItem } from "./cart";
const mockItems: CartItem[] = [
{ id: "1", name: "Widget", price: 9.99, quantity: 2 },
{ id: "2", name: "Gadget", price: 24.99, quantity: 1 },
];
describe("calculateTotal", () => {
it("sums price * quantity for all items", () => {
expect(calculateTotal(mockItems)).toBeCloseTo(44.97);
});
it("returns 0 for an empty cart", () => {
expect(calculateTotal([])).toBe(0);
});
it("handles a single item with quantity > 1", () => {
const items: CartItem[] = [{ id: "x", name: "X", price: 5, quantity: 3 }];
expect(calculateTotal(items)).toBe(15);
});
});
describe("apply…3 free generations remaining
More AI developer tools